diff --git a/bugs-fixed/README b/bugs-fixed/README index e834a29..9c644f9 100644 --- a/bugs-fixed/README +++ b/bugs-fixed/README @@ -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.) + diff --git a/bugs-fixed/negative-nf.awk b/bugs-fixed/negative-nf.awk new file mode 100644 index 0000000..6caeee4 --- /dev/null +++ b/bugs-fixed/negative-nf.awk @@ -0,0 +1 @@ +BEGIN { NF = -5; } diff --git a/bugs-fixed/negative-nf.ok b/bugs-fixed/negative-nf.ok new file mode 100644 index 0000000..71c8604 --- /dev/null +++ b/bugs-fixed/negative-nf.ok @@ -0,0 +1,2 @@ +./a.out: cannot set NF to a negative value + source line number 1 diff --git a/bugs-fixed/nf-self-assign.awk b/bugs-fixed/nf-self-assign.awk new file mode 100644 index 0000000..6ae29ee --- /dev/null +++ b/bugs-fixed/nf-self-assign.awk @@ -0,0 +1,6 @@ +BEGIN { + $0="a b c"; + OFS=","; + NF = NF; + print; +} diff --git a/bugs-fixed/nf-self-assign.bad b/bugs-fixed/nf-self-assign.bad new file mode 100644 index 0000000..3774da6 --- /dev/null +++ b/bugs-fixed/nf-self-assign.bad @@ -0,0 +1 @@ +a b c diff --git a/bugs-fixed/nf-self-assign.ok b/bugs-fixed/nf-self-assign.ok new file mode 100644 index 0000000..b2ffb02 --- /dev/null +++ b/bugs-fixed/nf-self-assign.ok @@ -0,0 +1 @@ +a,b,c diff --git a/lib.c b/lib.c index f09aeb2..f9cb6ca 100644 --- a/lib.c +++ b/lib.c @@ -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); diff --git a/run.c b/run.c index bf84b76..ce30e93 100644 --- a/run.c +++ b/run.c @@ -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);