Re: Awk split()/array bug in 7.5
On 2024-05-30 14:56:50 -0600, Todd C. Miller wrote: > This is not a bug. An awk associative array is effectively a hash > table so when you iterate over it like this you are not guaranteed > to get things in any particular order. In fact, our awk, mawk and > gawk all produce different output when given that snippet. Also, various languages that used to use predictable hash orders now randomize the key order to avoid Algorithmic Complexity Attacks that may allow an attacker to supply input crafted to consume lots of CPU memory or both. http://man.openbsd.org/man1/perlsec.1#Algorithmic_Complexity_Attacks
Re: Awk split()/array bug in 7.5
Hi, Upon finding out about this "quirk" in the awk language, if you are now horrified that you have loads of code that relies on this order, there is a temporary solution. Check out the WHINY_USERS environment variable: https://linux.die.net/man/1/mawk In mawk and (apparently) undocumented in gawk. It ensures arrays are sorted when iterating through in this way. Hope this helps, Karsten
Re: Awk split()/array bug in 7.5
Todd, Thanks for the explanation. Jeff On Thu, 30 May 2024 at 21:56, Todd C. Miller wrote: > > On Thu, 30 May 2024 21:42:08 +0100, Jeff Penn wrote: > > > I spotted the following issue, which is also present in FreeBSD. > > > > $ awk -V > > awk version 20240122 > > $ awk 'BEGIN {split("A B C", ABC, " ");for (x in ABC) {print x}}' > > 2 > > 3 > > 1 > > This is not a bug. An awk associative array is effectively a hash > table so when you iterate over it like this you are not guaranteed > to get things in any particular order. In fact, our awk, mawk and > gawk all produce different output when given that snippet. > > If you want consistent ordering you need to use the other form of > for() where you iterate over the array with keys in ascending (or > descending) order. > > - todd
Re: Awk split()/array bug in 7.5
On Thu, 30 May 2024 21:42:08 +0100, Jeff Penn wrote: > I spotted the following issue, which is also present in FreeBSD. > > $ awk -V > awk version 20240122 > $ awk 'BEGIN {split("A B C", ABC, " ");for (x in ABC) {print x}}' > 2 > 3 > 1 This is not a bug. An awk associative array is effectively a hash table so when you iterate over it like this you are not guaranteed to get things in any particular order. In fact, our awk, mawk and gawk all produce different output when given that snippet. If you want consistent ordering you need to use the other form of for() where you iterate over the array with keys in ascending (or descending) order. - todd