From: Russ Cox Date: Sun, 15 Nov 2009 07:25:55 +0000 (-0800) Subject: cc: eliminate two fixed-size buffers X-Git-Tag: weekly.2009-11-17~57 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=139a053733f2b7ec00c1a515524e308d57a2492b;p=gostls13.git cc: eliminate two fixed-size buffers Fixes bug 168. Alternative to https://golang.org/cl/152143. R=ken2 https://golang.org/cl/155042 --- diff --git a/src/cmd/cc/dpchk.c b/src/cmd/cc/dpchk.c index 59702f0633..5dcc82f2b7 100644 --- a/src/cmd/cc/dpchk.c +++ b/src/cmd/cc/dpchk.c @@ -61,7 +61,7 @@ struct Tname static Type* indchar; static uchar flagbits[512]; -static char fmtbuf[100]; +static char* lastfmt; static int lastadj; static int lastverb; static int nstar; @@ -97,17 +97,17 @@ getflag(char *s) { Bits flag; int f; - char *fmt; + Fmt fmt; Rune c; - fmt = fmtbuf; flag = zbits; nstar = 0; + fmtstrinit(&fmt); for(;;) { s += chartorune(&c, s); if(c == 0 || c >= nelem(flagbits)) break; - fmt += runetochar(fmt, &c); + fmtrune(&fmt, c); f = flagbits[c]; switch(f) { case Fnone: @@ -126,7 +126,8 @@ getflag(char *s) if(f >= Fverb) break; } - *fmt = 0; + free(lastfmt); + lastfmt = fmtstrflush(&fmt); return flag; } @@ -204,23 +205,26 @@ static char* getquoted(void) { int c; - char *t; Rune r; + Fmt fmt; c = getnsc(); if(c != '"') return nil; - t = fmtbuf; + fmtstrinit(&fmt); for(;;) { r = getr(); - if(r == ' ' || r == '\n') + if(r == ' ' || r == '\n') { + free(fmtstrflush(&fmt)); return nil; + } if(r == '"') break; - t += runetochar(t, &r); + fmtrune(&fmt, r); } - *t = 0; - return strdup(fmtbuf); + free(lastfmt); + lastfmt = fmtstrflush(&fmt); + return strdup(lastfmt); } void @@ -336,7 +340,7 @@ checkargs(Node *nn, char *s, int pos) nstar--; if(a == Z) { warn(nn, "more format than arguments %s", - fmtbuf); + lastfmt); return; } if(a->type == T) @@ -344,7 +348,7 @@ checkargs(Node *nn, char *s, int pos) if(!sametype(types[TINT], a->type) && !sametype(types[TUINT], a->type)) warn(nn, "format mismatch '*' in %s %T, arg %d", - fmtbuf, a->type, pos); + lastfmt, a->type, pos); } for(l=tprot; l; l=l->link) if(sametype(types[TVOID], l->type)) { @@ -358,7 +362,7 @@ checkargs(Node *nn, char *s, int pos) pos++; if(a == Z) { warn(nn, "more format than arguments %s", - fmtbuf); + lastfmt); return; } if(a->type == 0) @@ -369,7 +373,7 @@ checkargs(Node *nn, char *s, int pos) if(beq(flag, l->flag)) goto loop; } - warn(nn, "format mismatch %s %T, arg %d", fmtbuf, a->type, pos); + warn(nn, "format mismatch %s %T, arg %d", lastfmt, a->type, pos); loop:; } } diff --git a/src/cmd/cc/lex.c b/src/cmd/cc/lex.c index 9fbf3a3acd..1635e8b9de 100644 --- a/src/cmd/cc/lex.c +++ b/src/cmd/cc/lex.c @@ -188,12 +188,12 @@ main(int argc, char *argv[]) int compile(char *file, char **defs, int ndef) { - char ofile[400], incfile[20]; + char *ofile, incfile[20]; char *p, *av[100], opt[256]; int i, c, fd[2]; static int first = 1; - strcpy(ofile, file); + ofile = strdup(file); p = utfrrune(ofile, pathchar()); if(p) { *p++ = 0; @@ -288,14 +288,10 @@ compile(char *file, char **defs, int ndef) sprint(opt, "-+"); av[i++] = strdup(opt); } - for(c = 0; c < ndef; c++) { - sprint(opt, "-D%s", defs[c]); - av[i++] = strdup(opt); - } - for(c = 0; c < ninclude; c++) { - sprint(opt, "-I%s", include[c]); - av[i++] = strdup(opt); - } + for(c = 0; c < ndef; c++) + av[i++] = smprint("-D%s", defs[c]); + for(c = 0; c < ninclude; c++) + av[i++] = smprint("-I%s", include[c]); if(strcmp(file, "stdin") != 0) av[i++] = file; av[i] = 0;