Merge pull request #16 from melloc/assign-expr

Fix issues with assigning during concatenation
This commit is contained in:
onetrueawk 2019-01-21 14:17:19 -05:00 committed by GitHub
commit 905d260104
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 14 deletions

View File

@ -24,11 +24,18 @@ and also if CONVFMT changed.
7. unary-plus: Unary plus on a string constant returned the string. 7. unary-plus: Unary plus on a string constant returned the string.
Instead, it should convert the value to numeric and give that value. Instead, it should convert the value to numeric and give that value.
8. missing-precision: When using the format string "%*s", the precision 8. concat-assign-same: Concatenation previously evaluated both sides of the
expression before doing its work, which, since assign() evaluates to the cell
being assigned to, meant that expressions like "print (a = 1) (a = 2)" would
print "22" rather than "12".
9. missing-precision: When using the format string "%*s", the precision
argument was used without checking if it was present first. argument was used without checking if it was present first.
9. fmt-overflow: The buffer used for OFMT/CONVFMT conversions was written 10. missing-precision: When using the format string "%*s", the precision
argument was used without checking if it was present first.
11. fmt-overflow: The buffer used for OFMT/CONVFMT conversions was written
to with sprintf(), which meant that some conversions could write past the to with sprintf(), which meant that some conversions could write past the
end. end.

View File

@ -0,0 +1,4 @@
BEGIN {
print (a = 1) (a = 2) (a = 3) (a = 4) (a = 5);
print (a = 1), (a = 2), (a = 3), (a = 4), (a = 5);
}

View File

@ -0,0 +1,2 @@
22345
1 2 3 4 5

View File

@ -0,0 +1,2 @@
12345
1 2 3 4 5

23
run.c
View File

@ -1178,25 +1178,26 @@ Cell *cat(Node **a, int q) /* a[0] cat a[1] */
{ {
Cell *x, *y, *z; Cell *x, *y, *z;
int n1, n2; int n1, n2;
char *s; char *s = NULL;
int ssz = 0;
x = execute(a[0]); x = execute(a[0]);
n1 = strlen(getsval(x));
adjbuf(&s, &ssz, n1 + 1, recsize, 0, "cat1");
(void) strncpy(s, x->sval, ssz);
y = execute(a[1]); y = execute(a[1]);
getsval(x); n2 = strlen(getsval(y));
getsval(y); adjbuf(&s, &ssz, n1 + n2 + 1, recsize, 0, "cat2");
n1 = strlen(x->sval); (void) strncpy(s + n1, y->sval, ssz - n1);
n2 = strlen(y->sval);
s = (char *) malloc(n1 + n2 + 1);
if (s == NULL)
FATAL("out of space concatenating %.15s... and %.15s...",
x->sval, y->sval);
strcpy(s, x->sval);
strcpy(s+n1, y->sval);
tempfree(x); tempfree(x);
tempfree(y); tempfree(y);
z = gettemp(); z = gettemp();
z->sval = s; z->sval = s;
z->tval = STR; z->tval = STR;
return(z); return(z);
} }