bug-gnu-utils
[Top][All Lists]
Advanced

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

Re: bugs in passing uninitialized array to a function


From: Aharon Robbins
Subject: Re: bugs in passing uninitialized array to a function
Date: Sun, 30 Aug 2009 22:43:58 +0300

Greetings. Re this:

> Date: Sat, 29 Aug 2009 20:04:03 -0700 (PDT)
> From: Greg Johnson <address@hidden>
> Subject: bugs in passing uninitialized array to a function
> To: address@hidden
>
> I am using gawk version 3.1.7.
>
> The attached programs illustrate what look to me like two bugs
> in the handling of uninitialized variables to functions that treat
> them as arrays.
>
> Greg Johnson
>
>       
> # bug?  on uninitialized array, length(a) prints as 3, then the loop
> # behaves differently, iterating once.  so, length() behaves differently
> # on two calls to the same variable, which was not changed.
>
> function prt1(a, len)
> {
>     print "length:  " length(a)
>
>     for (i = 1; i <= length(a); i++)
>         printf "<" i "," a[i] "> "
>
>     print "\n"
> }
>
> BEGIN {
>     prt1(zzz)
> }
>
>
> # shouldn't an uninitialized array have length zero?
> # length is printed as 1, and the loop iterates once.
>
> function prt(a, len)
> {
>     len = length(a)
>     print "length:  " len
>
>     for (i = 1; i <= len; i++)
>         printf "<" i "," a[i] "> "
>
>     print "\n"
> }
>
> BEGIN {
>     prt(zzz)
> }

It's a bug. The following patch fixes both cases.

Thanks for reporting it.

Arnold
---------------------------------------------------------------------------
Sun Aug 30 22:40:12 2009  Arnold D. Robbins  <address@hidden>

        * builtin.c (do_length): Handle the case where Node_var_new
        was passed in as a parameter via a function call parameter.
        Thanks to Greg Johnson <address@hidden> for reporting
        the bug.

Index: builtin.c
===================================================================
RCS file: /d/mongo/cvsrep/gawk-stable/builtin.c,v
retrieving revision 1.36
diff -u -r1.36 builtin.c
--- builtin.c   4 Aug 2009 03:08:51 -0000       1.36
+++ builtin.c   30 Aug 2009 19:35:36 -0000
@@ -466,6 +466,13 @@
                if (do_posix)
                        goto normal;    /* will die as fatal error */
 
+               if (array_var->type == Node_var_new) {
+                       if (do_lint)
+                               lintwarn(_("length: untyped parameter argument 
will be forced to scalar"));
+
+                       return tmp_number(0.0);
+               }
+
                return tmp_number((AWKNUM) array_var->table_size);
        } else {
 normal:




reply via email to

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