Commit 139a0537 authored by Russ Cox's avatar Russ Cox

cc: eliminate two fixed-size buffers

Fixes bug 168.
Alternative to https://golang.org/cl/152143.

R=ken2
https://golang.org/cl/155042
parent 529216fa
...@@ -61,7 +61,7 @@ struct Tname ...@@ -61,7 +61,7 @@ struct Tname
static Type* indchar; static Type* indchar;
static uchar flagbits[512]; static uchar flagbits[512];
static char fmtbuf[100]; static char* lastfmt;
static int lastadj; static int lastadj;
static int lastverb; static int lastverb;
static int nstar; static int nstar;
...@@ -97,17 +97,17 @@ getflag(char *s) ...@@ -97,17 +97,17 @@ getflag(char *s)
{ {
Bits flag; Bits flag;
int f; int f;
char *fmt; Fmt fmt;
Rune c; Rune c;
fmt = fmtbuf;
flag = zbits; flag = zbits;
nstar = 0; nstar = 0;
fmtstrinit(&fmt);
for(;;) { for(;;) {
s += chartorune(&c, s); s += chartorune(&c, s);
if(c == 0 || c >= nelem(flagbits)) if(c == 0 || c >= nelem(flagbits))
break; break;
fmt += runetochar(fmt, &c); fmtrune(&fmt, c);
f = flagbits[c]; f = flagbits[c];
switch(f) { switch(f) {
case Fnone: case Fnone:
...@@ -126,7 +126,8 @@ getflag(char *s) ...@@ -126,7 +126,8 @@ getflag(char *s)
if(f >= Fverb) if(f >= Fverb)
break; break;
} }
*fmt = 0; free(lastfmt);
lastfmt = fmtstrflush(&fmt);
return flag; return flag;
} }
...@@ -204,23 +205,26 @@ static char* ...@@ -204,23 +205,26 @@ static char*
getquoted(void) getquoted(void)
{ {
int c; int c;
char *t;
Rune r; Rune r;
Fmt fmt;
c = getnsc(); c = getnsc();
if(c != '"') if(c != '"')
return nil; return nil;
t = fmtbuf; fmtstrinit(&fmt);
for(;;) { for(;;) {
r = getr(); r = getr();
if(r == ' ' || r == '\n') if(r == ' ' || r == '\n') {
free(fmtstrflush(&fmt));
return nil; return nil;
}
if(r == '"') if(r == '"')
break; break;
t += runetochar(t, &r); fmtrune(&fmt, r);
} }
*t = 0; free(lastfmt);
return strdup(fmtbuf); lastfmt = fmtstrflush(&fmt);
return strdup(lastfmt);
} }
void void
...@@ -336,7 +340,7 @@ checkargs(Node *nn, char *s, int pos) ...@@ -336,7 +340,7 @@ checkargs(Node *nn, char *s, int pos)
nstar--; nstar--;
if(a == Z) { if(a == Z) {
warn(nn, "more format than arguments %s", warn(nn, "more format than arguments %s",
fmtbuf); lastfmt);
return; return;
} }
if(a->type == T) if(a->type == T)
...@@ -344,7 +348,7 @@ checkargs(Node *nn, char *s, int pos) ...@@ -344,7 +348,7 @@ checkargs(Node *nn, char *s, int pos)
if(!sametype(types[TINT], a->type) && if(!sametype(types[TINT], a->type) &&
!sametype(types[TUINT], a->type)) !sametype(types[TUINT], a->type))
warn(nn, "format mismatch '*' in %s %T, arg %d", warn(nn, "format mismatch '*' in %s %T, arg %d",
fmtbuf, a->type, pos); lastfmt, a->type, pos);
} }
for(l=tprot; l; l=l->link) for(l=tprot; l; l=l->link)
if(sametype(types[TVOID], l->type)) { if(sametype(types[TVOID], l->type)) {
...@@ -358,7 +362,7 @@ checkargs(Node *nn, char *s, int pos) ...@@ -358,7 +362,7 @@ checkargs(Node *nn, char *s, int pos)
pos++; pos++;
if(a == Z) { if(a == Z) {
warn(nn, "more format than arguments %s", warn(nn, "more format than arguments %s",
fmtbuf); lastfmt);
return; return;
} }
if(a->type == 0) if(a->type == 0)
...@@ -369,7 +373,7 @@ checkargs(Node *nn, char *s, int pos) ...@@ -369,7 +373,7 @@ checkargs(Node *nn, char *s, int pos)
if(beq(flag, l->flag)) if(beq(flag, l->flag))
goto loop; 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:; loop:;
} }
} }
......
...@@ -188,12 +188,12 @@ main(int argc, char *argv[]) ...@@ -188,12 +188,12 @@ main(int argc, char *argv[])
int int
compile(char *file, char **defs, int ndef) compile(char *file, char **defs, int ndef)
{ {
char ofile[400], incfile[20]; char *ofile, incfile[20];
char *p, *av[100], opt[256]; char *p, *av[100], opt[256];
int i, c, fd[2]; int i, c, fd[2];
static int first = 1; static int first = 1;
strcpy(ofile, file); ofile = strdup(file);
p = utfrrune(ofile, pathchar()); p = utfrrune(ofile, pathchar());
if(p) { if(p) {
*p++ = 0; *p++ = 0;
...@@ -288,14 +288,10 @@ compile(char *file, char **defs, int ndef) ...@@ -288,14 +288,10 @@ compile(char *file, char **defs, int ndef)
sprint(opt, "-+"); sprint(opt, "-+");
av[i++] = strdup(opt); av[i++] = strdup(opt);
} }
for(c = 0; c < ndef; c++) { for(c = 0; c < ndef; c++)
sprint(opt, "-D%s", defs[c]); av[i++] = smprint("-D%s", defs[c]);
av[i++] = strdup(opt); for(c = 0; c < ninclude; c++)
} av[i++] = smprint("-I%s", include[c]);
for(c = 0; c < ninclude; c++) {
sprint(opt, "-I%s", include[c]);
av[i++] = strdup(opt);
}
if(strcmp(file, "stdin") != 0) if(strcmp(file, "stdin") != 0)
av[i++] = file; av[i++] = file;
av[i] = 0; av[i] = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment