[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    bug in busybox awk handling of empty fields
From:       M Rubon <rubonmtz () gmail ! com>
Date:       2023-12-15 5:07:00
Message-ID: CAJ24mqConK-gANm-MdaJa+aD3Tv7L6rH1zPwtMqQyZUFG-2XOw () mail ! gmail ! com
[Download RAW message or body]

Busybox awk handles references to empty (not provided in the input)
fields differently during the first line of input, as compared to
subsequent lines.

root@clean2305:~# (echo a ; echo b) | awk '$2 != 0'    #wrong
b

No field $2 value is provided in the input.  When awk references field
$2 for the "a" line, it is seen to have a different behaviour than
when it is referenced for the "b" line.

Problem in BusyBox v1.36.1 embedded in OpenWrt 23.05.0
Same problem also in 21.02 versions of OpenWrt
Same problem in BusyBox v1.37.0.git

I get the correct expected output from Ubuntu gawk and Debian mawk,
and from my fix.
will@dev:~$ (echo a ; echo b) | awk '$2 != 0'  #correct
a
b
will@dev:~/busybox$ (echo a ; echo b ) | ./busybox awk '$2 != 0'  #fixed
a
b

I built and poked into the source code at editors/awk.c  The function
fsrealloc(int size) is core to allocating, initializing, reallocating,
and reinitializing fields, both real input line fields and imaginary
fields that the script references but do not exist in the input.

When fsrealloc() needs more field space than it has previously
allocated, it initializes those new fields differently than how they
are later reinitialized for the next input line.  This works fine for
fields defined in the input, like $1, but does not work the first time
when there is no input for that field (e.g. field $99)

My one-line fix simply makes the initialization and clrvar()
reinitialization use the same value for .type.  I am not sure if there
are regression tests to run, but I have not done those.

I'm not sure if I understand why clrvar() is not setting .type to a
default constant value, but in any case I have left that untouched.

I am a happy busybox user.  Thank you very much for your work on the system!

M


diff --git a/editors/awk.c b/editors/awk.c
index bc95c4155..60b8276e7 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -1981,7 +1981,7 @@ static void fsrealloc(int size)
                /* ^^^ did Fields[] move? debug aid for L.v getting
"upstaged" by R.v in evaluate() */

                for (; i < maxfields; i++) {
-                       Fields[i].type = VF_SPECIAL;
+                       Fields[i].type = VF_SPECIAL | VF_DIRTY;
                        Fields[i].string = NULL;
                }
        }
_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic