bug-gawk
[Top][All Lists]
Advanced

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

Re: gawk-5.2.2: fatal: attempt to use scalar parameter `a' as an array


From: arnold
Subject: Re: gawk-5.2.2: fatal: attempt to use scalar parameter `a' as an array
Date: Sun, 04 Jun 2023 07:44:44 -0600
User-agent: Heirloom mailx 12.5 7/5/10

Hello.

Thank you for submitting a report.

What you've reported is not a bug per se.  Gawk is merely behaving differently
than you expect.

Gawk tracks the type of a global variable dynamically, not based on whether
the source code uses subscrpting or not.

        function add(a, s,    n) {
            n = length(a)
            a[++n] = s
            return n
        }

When 'a' comes in to add() from

        BEGIN {
            add(A, "aaa")

It refers to the global variable 'A'. That variable has never been given
a type, so what add() does with it will determine what the type
becomes. The first thing it does is:

        n = length(a)

When length() is called on an untyped variable, said untyped variable is
coerced into being a scalar, since that is historical practice in awk.
Then:

        a[++n] = s

tries to treat 'a' as an array, and gawk generates the fatal message.

However, if

        delete A

runs before the call to add(), 'A' has already been forced into being
an array, since delete is only for arrays.  When add() is called,
'a' refers to 'A', which is an array, not untyped. Thus the call
to length() makes no change to the type, and the subscripting
operation succeeds.

I hope this explanation clarifies the behavior for you.

Thanks,

Arnold

"Rajeev V. Pillai" <rajeev_v_pillai@yahoo.com> wrote:

> Configuration Information [Automatically generated, do not change]:
> Machine: x86_64
> OS: freebsd13.2
> Compiler: clang
> Compilation CFLAGS: -Wall -O2 -flto -fpie -fstack-protector-all 
> -fomit-frame-pointer -march=native -pipe -DNDEBUG
> uname output: FreeBSD x202e 13.2-RELEASE FreeBSD 13.2-RELEASE MYKERNEL amd64
> Machine Type: x86_64-unknown-freebsd13.2
>
> Gawk Version: 5.2.2
>
> Attestation 1:
>     I have read https://www.gnu.org/software/gawk/manual/html_node/Bugs.html.
>     Yes
>
> Attestation 2:
>     I have not modified the sources before building gawk.
>     True
>
> Description:
>     Given this program:
>
> ```
> function add(a, s,    n) {
>     n = length(a)
>     a[++n] = s
>     return n
> }
>
> BEGIN {
>     # delete A    # turn scalar -> array (for gawk & mawk)
>     add(A, "aaa")
>     add(A, "b")
>     print length(A)
> }
> ```
>
>     gawk-5.2.2 says:
>
> ```
> $ gawk -f array.awk
> gawk: array.awk:3: fatal: attempt to use scalar parameter `a' as an array
> $
> ```
>
>     Uncommenting the `delete A` fixes the issue.
>
> Repeat-By:
>     As above.



reply via email to

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