From 5579176f4a885bb83119bb49598c357ce8db2343 Mon Sep 17 00:00:00 2001 From: Ori Bernstein Date: Sat, 25 Jun 2022 18:58:55 +0000 Subject: [PATCH] awk: initialize records fully in recinit() when using records in BEGIN, we would read from the field table before we read into it; this ensures that the fields are an empty string before we start touching their contents. --- sys/src/cmd/awk/lib.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sys/src/cmd/awk/lib.c b/sys/src/cmd/awk/lib.c index 2c5a60465..5044d3123 100644 --- a/sys/src/cmd/awk/lib.c +++ b/sys/src/cmd/awk/lib.c @@ -54,11 +54,13 @@ static Cell dollar1 = { OCELL, CFLD, nil, "", 0.0, FLD|STR|DONTFREE }; void recinit(unsigned int n) { + assert(n > 0); record = (char *) malloc(n); fields = (char *) malloc(n); fldtab = (Cell **) malloc((nfields+1) * sizeof(Cell *)); if (record == nil || fields == nil || fldtab == nil) FATAL("out of space for $0 and fields"); + record[0] = '\0'; fldtab[0] = (Cell *) malloc(sizeof (Cell)); *fldtab[0] = dollar0; fldtab[0]->sval = record; @@ -108,7 +110,7 @@ int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */ firsttime = 0; initgetrec(); } - dprint( ("RS=<%s>, FS=<%s>, AARGC=%g, FILENAME=%s\n", + dprint( ("RS=<%s>, FS=<%s>, AARGC=%g, FILENAME=%s\n", *RS, *FS, *AARGC, *FILENAME) ); if (isrecord) { donefld = 0;