disallow $ in printf formats

This commit is contained in:
Brian Kernighan 2018-08-27 08:52:34 -04:00
parent 0f4e1ba922
commit 22aff9e657
4 changed files with 14 additions and 8 deletions

7
FIXES
View File

@ -25,6 +25,13 @@ THIS SOFTWARE.
This file lists all bug fixes, changes, etc., made since the AWK book This file lists all bug fixes, changes, etc., made since the AWK book
was sent to the printers in August, 1987. was sent to the printers in August, 1987.
Aug 27, 2018:
Disallow '$' in printf formats; arguments evaluated in order
and printed in order.
Added some casts to silence warnings on debugging printfs.
(Thanks, Arnold.)
Aug 23, 2018: Aug 23, 2018:
A long list of fixes courtesy of Arnold Robbins, A long list of fixes courtesy of Arnold Robbins,
to whom profound thanks. to whom profound thanks.

Binary file not shown.

9
run.c
View File

@ -114,7 +114,7 @@ int adjbuf(char **pbuf, int *psiz, int minlen, int quantum, char **pbptr,
if (rminlen) if (rminlen)
minlen += quantum - rminlen; minlen += quantum - rminlen;
tbuf = (char *) realloc(*pbuf, minlen); tbuf = (char *) realloc(*pbuf, minlen);
dprintf( ("adjbuf %s: %d %d (pbuf=%p, tbuf=%p)\n", whatrtn, *psiz, minlen, *pbuf, tbuf) ); dprintf( ("adjbuf %s: %d %d (pbuf=%p, tbuf=%p)\n", whatrtn, *psiz, minlen, (void *) *pbuf, (void *) tbuf) );
if (tbuf == NULL) { if (tbuf == NULL) {
if (whatrtn) if (whatrtn)
FATAL("out of memory in %s", whatrtn); FATAL("out of memory in %s", whatrtn);
@ -859,6 +859,9 @@ int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like co
FATAL("format item %.30s... ran format() out of memory", os); FATAL("format item %.30s... ran format() out of memory", os);
if (isalpha((uschar)*s) && *s != 'l' && *s != 'h' && *s != 'L') if (isalpha((uschar)*s) && *s != 'l' && *s != 'h' && *s != 'L')
break; /* the ansi panoply */ break; /* the ansi panoply */
if (*s == '$') {
FATAL("'$' not permitted in awk formats");
}
if (*s == '*') { if (*s == '*') {
x = execute(a); x = execute(a);
a = a->nnext; a = a->nnext;
@ -874,7 +877,6 @@ int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like co
if (fmtwd < 0) if (fmtwd < 0)
fmtwd = -fmtwd; fmtwd = -fmtwd;
adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format4"); adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format4");
switch (*s) { switch (*s) {
case 'a': case 'A': case 'a': case 'A':
if (have_a_format) if (have_a_format)
@ -1641,8 +1643,6 @@ Cell *printstat(Node **a, int n) /* print a[0] */
Cell *nullproc(Node **a, int n) Cell *nullproc(Node **a, int n)
{ {
n = n;
a = a;
return 0; return 0;
} }
@ -1756,7 +1756,6 @@ Cell *closefile(Node **a, int n)
Cell *x; Cell *x;
int i, stat; int i, stat;
n = n;
x = execute(a[0]); x = execute(a[0]);
getsval(x); getsval(x);
stat = -1; stat = -1;

6
tran.c
View File

@ -347,7 +347,7 @@ char *setsval(Cell *vp, const char *s) /* set string val of a Cell */
fldno = atoi(vp->nval); fldno = atoi(vp->nval);
if (fldno > *NF) if (fldno > *NF)
newfld(fldno); newfld(fldno);
dprintf( ("setting field %d to %s (%p)\n", fldno, s, s) ); dprintf( ("setting field %d to %s (%p)\n", fldno, s, (void *) s) );
} else if (isrec(vp)) { } else if (isrec(vp)) {
donefld = 0; /* mark $1... invalid */ donefld = 0; /* mark $1... invalid */
donerec = 1; donerec = 1;
@ -363,7 +363,7 @@ char *setsval(Cell *vp, const char *s) /* set string val of a Cell */
vp->fmt = NULL; vp->fmt = NULL;
setfree(vp); setfree(vp);
dprintf( ("setsval %p: %s = \"%s (%p) \", t=%o r,f=%d,%d\n", dprintf( ("setsval %p: %s = \"%s (%p) \", t=%o r,f=%d,%d\n",
(void*)vp, NN(vp->nval), t,t, vp->tval, donerec, donefld) ); (void*)vp, NN(vp->nval), t, (void *) t, vp->tval, donerec, donefld) );
vp->sval = t; vp->sval = t;
if (&vp->fval == NF) { if (&vp->fval == NF) {
donerec = 0; /* mark $0 invalid */ donerec = 0; /* mark $0 invalid */
@ -477,7 +477,7 @@ static char *get_str_val(Cell *vp, char **fmt) /* get string val of a Cel
} }
done: done:
dprintf( ("getsval %p: %s = \"%s (%p)\", t=%o\n", dprintf( ("getsval %p: %s = \"%s (%p)\", t=%o\n",
(void*)vp, NN(vp->nval), vp->sval, vp->sval, vp->tval) ); (void*)vp, NN(vp->nval), vp->sval, (void *) vp->sval, vp->tval) );
return(vp->sval); return(vp->sval);
} }