awk icon indicating copy to clipboard operation
awk copied to clipboard

$0=$2 "glitch"

Open drawkula opened this issue 3 years ago • 2 comments

In a freshly cloned and built source:

$ git branch 
* master
$ git log -n1 --oneline 
2402014 (HEAD -> master, origin/staging, origin/master, origin/HEAD) Eliminate file management memory leak
$ echo 1: 2 | ./a.out 'NF==2 && $0=$2'
$ echo 1: 2 | ./a.out 'NF==2 && $0=$2 "" '
2

GAWK and MAWK print 2 without the appended "" too.

drawkula avatar May 29 '22 00:05 drawkula

interesting. thanks for spotting this.

plan9 avatar May 29 '22 03:05 plan9

In run.c::assign(), assigning to $0 from $F, a field, where F >= 2, produces an incoherent cell.

The assignment occurs in two steps, first the string value and then the float. When the string value is assigned to $0, setsval invalidates the fields. If FS hasn't changed, after getfval rebuilds the fields, NF = 1 and F >= 2, therefore $F is definitely uninitialized. The result is a float val of 0.0, producing a boolean false in the pattern expression.

Coercing a string comparison gives the expected result because the incoherent cell has the correct string value, which is not empty and evaluates to true.

diff --git a/run.c b/run.c
index df616fc..277e5c7 100644
--- a/run.c
+++ b/run.c
@@ -1133,8 +1133,9 @@ Cell *assign(Node **a, int n)	/* a[0] = a[1], a[0] += a[1], etc. */
 		if (x == y && !(x->tval & (FLD|REC)) && x != nfloc)
 			;	/* self-assignment: leave alone unless it's a field or NF */
 		else if ((y->tval & (STR|NUM)) == (STR|NUM)) {
+			yf = getfval(y);
 			setsval(x, getsval(y));
-			x->fval = getfval(y);
+			x->fval = yf;
 			x->tval |= NUM;
 		}
 		else if (isstr(y))

Compiles without error. Passes the test suite without error.

Nice find, @drawkula. Looks like that's been lurking in the code for at least 37 years.

Take care, Miguel

mpinjr avatar May 29 '22 06:05 mpinjr

Thanks for the report @drawkula and especial thanks to @mpinjr for the fix. I will push to the repo shortly. Closing the issue.

arnoldrobbins avatar Nov 23 '23 17:11 arnoldrobbins