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.
This commit is contained in:
Ori Bernstein 2022-06-25 18:58:55 +00:00
parent 5ee86cf824
commit 5579176f4a

View file

@ -54,11 +54,13 @@ static Cell dollar1 = { OCELL, CFLD, nil, "", 0.0, FLD|STR|DONTFREE };
void recinit(unsigned int n) void recinit(unsigned int n)
{ {
assert(n > 0);
record = (char *) malloc(n); record = (char *) malloc(n);
fields = (char *) malloc(n); fields = (char *) malloc(n);
fldtab = (Cell **) malloc((nfields+1) * sizeof(Cell *)); fldtab = (Cell **) malloc((nfields+1) * sizeof(Cell *));
if (record == nil || fields == nil || fldtab == nil) if (record == nil || fields == nil || fldtab == nil)
FATAL("out of space for $0 and fields"); FATAL("out of space for $0 and fields");
record[0] = '\0';
fldtab[0] = (Cell *) malloc(sizeof (Cell)); fldtab[0] = (Cell *) malloc(sizeof (Cell));
*fldtab[0] = dollar0; *fldtab[0] = dollar0;
fldtab[0]->sval = record; fldtab[0]->sval = record;
@ -108,7 +110,7 @@ int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */
firsttime = 0; firsttime = 0;
initgetrec(); 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) ); *RS, *FS, *AARGC, *FILENAME) );
if (isrecord) { if (isrecord) {
donefld = 0; donefld = 0;