]> Cypherpunks repositories - gostls13.git/commitdiff
cc: eliminate two fixed-size buffers
authorRuss Cox <rsc@golang.org>
Sun, 15 Nov 2009 07:25:55 +0000 (23:25 -0800)
committerRuss Cox <rsc@golang.org>
Sun, 15 Nov 2009 07:25:55 +0000 (23:25 -0800)
Fixes bug 168.
Alternative to https://golang.org/cl/152143.

R=ken2
https://golang.org/cl/155042

src/cmd/cc/dpchk.c
src/cmd/cc/lex.c

index 59702f06334e4e1ec4ae689d424f907ee05aa814..5dcc82f2b7a294402f419e987f05800094d66d41 100644 (file)
@@ -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:;
        }
 }
index 9fbf3a3acdd0a09fa7be9ae869b00cde25f01a56..1635e8b9de88a8cde8c753d9ae9fc210b9fca827 100644 (file)
@@ -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;