[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: numeric instead of string comparison with array indices that look li
From: |
Andrew J. Schorr |
Subject: |
Re: numeric instead of string comparison with array indices that look like numbers |
Date: |
Fri, 19 Apr 2024 08:28:31 -0400 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
Hi Ed,
Thanks for the report. I see the same issue in the master branch on
Linux:
bash-5.1$ ./gawk 'BEGIN{a["10"]; for (i in a) {if (i < 2) print i}}'
But the bug is not present in an older gawk 5.1.1ish version that I have lying
around. So there was a regression at some point...
Regards,
Andy
On Fri, Apr 19, 2024 at 06:05:14AM -0500, Ed Morton wrote:
> Configuration Information [Automatically generated, do not change]:
> Machine: x86_64
> OS: cygwin
> Compiler: gcc
> Compilation CFLAGS: -ggdb -O2 -pipe -Wall -Werror=format-security
> -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector-strong
> --param=ssp-buffer-size=4
> -fdebug-prefix-map=/cygdrive/d/a/scallywag/gawk/gawk-5.3.0-1.x86_64/build=/usr/src/debug/gawk-5.3.0-1
>
> -fdebug-prefix-map=/cygdrive/d/a/scallywag/gawk/gawk-5.3.0-1.x86_64/src/gawk-5.3.0=/usr/src/debug/gawk-5.3.0-1
> -DNDEBUG
> uname output: CYGWIN_NT-10.0-22631 TournaMart_2023 3.5.3-1.x86_64
> 2024-04-03 17:25 UTC x86_64 Cygwin
> Machine Type: x86_64-pc-cygwin
>
> Gawk Version: 5.3.0
>
> 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:
> gawk is [presumably] doing numeric instead of string
> comparisons when
> using an array index (always a string) on one side of the
> comparison
> and a number on the other. This behavior can be modified just by
> calling `typeof()` on the array index before the comparison.
>
> Repeat-By:
> In the following, `"10"` is always a string and we're always
> comparing
> `"10" < 2` but note there's no output from the second script that's
> using a variable populated from the array index and we can cause it
> to produce the expected output just by adding a call to `typeof()`
> in the third script:
>
> 1) $ awk 'BEGIN{ i="10"; if(i < 2) print i }'
> 10
> $
>
> 2) $ awk 'BEGIN{ a["10"]; for (i in a) { if(i < 2) print i } }'
> $
>
> 3) $ awk 'BEGIN{ a["10"]; for (i in a) { print typeof(i);
> if(i < 2) print i } }'
> string
> 10
> $