Merge pull request #20 from melloc/nf-self-assign

Fix issues with NF self-assignment and setting NF to negative values
This commit is contained in:
onetrueawk 2019-01-21 14:20:37 -05:00 committed by GitHub
commit bf2585c9fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 16 additions and 2 deletions

View File

@ -51,3 +51,4 @@ array passed as the second argument, then split() would previously read
from the freed memory and possibly produce incorrect results (depending
on the system's malloc()/free() behaviour.)

View File

@ -0,0 +1 @@
BEGIN { NF = -5; }

View File

@ -0,0 +1,2 @@
./a.out: cannot set NF to a negative value
source line number 1

View File

@ -0,0 +1,6 @@
BEGIN {
$0="a b c";
OFS=",";
NF = NF;
print;
}

View File

@ -0,0 +1 @@
a b c

View File

@ -0,0 +1 @@
a,b,c

2
lib.c
View File

@ -393,6 +393,8 @@ void newfld(int n) /* add field n after end of existing lastfld */
void setlastfld(int n) /* set lastfld cleaning fldtab cells if necessary */
{
if (n < 0)
FATAL("cannot set NF to a negative value");
if (n > nfields)
growfldtab(n);

4
run.c
View File

@ -1123,8 +1123,8 @@ Cell *assign(Node **a, int n) /* a[0] = a[1], a[0] += a[1], etc. */
y = execute(a[1]);
x = execute(a[0]);
if (n == ASSIGN) { /* ordinary assignment */
if (x == y && !(x->tval & (FLD|REC))) /* self-assignment: */
; /* leave alone unless it's a field */
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)) {
setsval(x, getsval(y));
x->fval = getfval(y);