bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] Special Comparisons: uninitialized array index


From: Andrew J. Schorr
Subject: Re: [bug-gawk] Special Comparisons: uninitialized array index
Date: Mon, 7 Aug 2017 11:07:13 -0400
User-agent: Mutt/1.5.21 (2010-09-15)

On Sun, Aug 06, 2017 at 05:59:59PM -0700, Daniel Pettet wrote:
> 2.  Array indices should always be treated as strings - including nil
> (undefined) values.  The following code illustrates the issue.
> 
> BEGIN {
>     a[i] = "null"                       # i is initially undefined
>     for (i in a) {                      # i is null string
>         print length(i), a[i]           # 0 null
>         print i==0, i==""               # 1 1  should be  0 1
>     }
>     print a[""]                         # null
>     print a[0]                          #
> }
> 
> The nil (uninitialized) value compares true with zero and the null string
> but the null string should compare false with zero.
> 
> print  v == 0           # 1     v is not set
> print  v == ""          # 1
> print  0 == ""          # 0
> 
> This implies that i in 'print i==0, i==""' is undefined and not the null
> string.  i should be set by the array loop to null.

This is easy to see in the master branch where we now have a typeof() function:

bash-4.2$ gawk 'BEGIN {
>     a[i] = "null"
>     for (i in a)
>         print length(i), a[i], typeof(i)       
> }
> '
0 null unassigned

The fix is less obvious to me. One could patch str_array.c:str_lookup as
follows:

--- a/str_array.c
+++ b/str_array.c
@@ -165,7 +165,7 @@ str_lookup(NODE *symbol, NODE *subs)
         * "Array indices are always strings."
         * ....
         */
-       if (subs->stfmt != STFMT_UNUSED) {
+       if (subs->stfmt != STFMT_UNUSED || subs == Nnull_string) {
                /* The string was generated using CONVFMT. */
                NODE *tmp;

But I'm not sure that's the best solution. Perhaps it should be attacked
on the extraction side in the str_list function, but that would probably
lead to memory leaks. Arnold -- what do you think?

Regards,
Andy



reply via email to

[Prev in Thread] Current Thread [Next in Thread]