NetBSD Problem Report #52623

From dholland@macaran.eecs.harvard.edu  Mon Oct 16 02:03:05 2017
Return-Path: <dholland@macaran.eecs.harvard.edu>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 4E0617A17E
	for <gnats-bugs@gnats.NetBSD.org>; Mon, 16 Oct 2017 02:03:05 +0000 (UTC)
Message-Id: <20171016004741.7BB176E289@macaran.eecs.harvard.edu>
Date: Sun, 15 Oct 2017 20:47:41 -0400 (EDT)
From: dholland@eecs.harvard.edu
Reply-To: dholland@netbsd.org
To: gnats-bugs@NetBSD.org
Subject: nonsense error message in awk
X-Send-Pr-Version: 3.95

>Number:         52623
>Category:       bin
>Synopsis:       nonsense error message in awk
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Oct 16 02:05:00 +0000 2017
>Last-Modified:  Mon Oct 16 17:30:01 +0000 2017
>Originator:     David A. Holland
>Release:        NetBSD 8.99.1 (20170809)
>Organization:
>Environment:
System: NetBSD macaran 8.99.1 NetBSD 8.99.1 (MACARAN) #42: Wed Aug 9 22:31:11 EDT 2017 dholland@macaran:/usr/src/sys/arch/amd64/compile/MACARAN amd64
Architecture: x86_64
Machine: amd64
>Description:

Accidentally writing index[array] instead of array[index] in awk
causes a nonsense error message.

>How-To-Repeat:

   awk -F: < /etc/passwd '{ a[$1] = $3; } END { for (k in a) print k[a]; }'
   awk: can't assign to k; it's an array name.
     input record number 123, file 
     source line number 1

>Fix:

>Audit-Trail:
From: Robert Elz <kre@munnari.OZ.AU>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: bin/52623: nonsense error message in awk
Date: Mon, 16 Oct 2017 14:15:07 +0700

     Date:        Mon, 16 Oct 2017 02:05:00 +0000 (UTC)
     From:        dholland@eecs.harvard.edu
     Message-ID:  <20171016020500.E6C587A269@mollari.NetBSD.org>

   | Accidentally writing index[array] instead of array[index] in awk
   | causes a nonsense error message.

 Not really nonsense...

   |    awk -F: < /etc/passwd '{ a[$1] = $3; } END { for (k in a) print k[a]; }'

 When that is compiled, awk can see from the reference to k[a] that k is
 an array.   The syntax is all OK, so the compile succeeds.

 When it is executed, and we get to the "for (k in a)" we need to assign
 the indexes of array a to k, which can't be done, as k is an array.

 Hence:
   |    awk: can't assign to k; it's an array name.

 What would you have it do differently?

 kre

From: David Holland <dholland-bugs@netbsd.org>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: bin/52623: nonsense error message in awk
Date: Mon, 16 Oct 2017 17:28:30 +0000

 On Mon, Oct 16, 2017 at 07:20:01AM +0000, Robert Elz wrote:
  >>  awk -F: < /etc/passwd '{ a[$1] = $3; } END { for (k in a) print k[a]; }'
  >  
  >  When that is compiled, awk can see from the reference to k[a] that k is
  >  an array.   The syntax is all OK, so the compile succeeds.

 It's doing type inference?

  >  When it is executed, and we get to the "for (k in a)" we need to assign
  >  the indexes of array a to k, which can't be done, as k is an array.
  >  
  >  Hence:
  >>    awk: can't assign to k; it's an array name.
  >  
  >  What would you have it do differently?

 I would expect it to, on the first iteration, assign k to the first
 value in a[], then choke at the print where it tries to use an array
 to index an array expression and/or tries to index an array as a
 scalar.

 e.g.

    awk: can't read value of a; it's an array name.

 -- 
 David A. Holland
 dholland@netbsd.org

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2014 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.