code: plan9front

Download patch

ref: 5579176f4a885bb83119bb49598c357ce8db2343
parent: 5ee86cf824c5591aa92118c0cd9d71b005e789d0
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Jun 25 14:58:55 EDT 2022

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.

--- a/sys/src/cmd/awk/lib.c
+++ b/sys/src/cmd/awk/lib.c
@@ -54,11 +54,13 @@
 
 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 @@
 		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;