]> Cypherpunks repositories - gostls13.git/commitdiff
build: remove various uses of C undefined behavior
authorRuss Cox <rsc@golang.org>
Mon, 9 Sep 2013 19:07:23 +0000 (15:07 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 9 Sep 2013 19:07:23 +0000 (15:07 -0400)
If you thought gcc -ansi -pedantic was pedantic, just wait
until you meet clang -fsanitize=undefined.

I think this addresses all the reported "errors", but we'll
need another run to be sure.

all.bash still passes.

Update #5764

Dave, can you please try again?

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/13334049

16 files changed:
include/bio.h
src/cmd/cc/com.c
src/cmd/cc/lex.c
src/cmd/cc/lexbody
src/cmd/gc/bv.c
src/cmd/gc/go.h
src/cmd/gc/md5.c
src/cmd/gc/mparith2.c
src/cmd/gc/subr.c
src/cmd/ld/go.c
src/cmd/ld/lib.c
src/cmd/pack/ar.c
src/libmach/5obj.c
src/libmach/6obj.c
src/libmach/8obj.c
src/libmach/obj.c

index be4d8d80eec8048f870a838c5db525a2631d642e..5506c7c3284f3ca6b72806662fa0755717a174e8 100644 (file)
@@ -79,7 +79,7 @@ struct        Biobuf
 #define        BGETLE2(bp)\
        ((bp)->icount<=-2?((bp)->icount+=2,((bp)->ebuf[(bp)->icount-2])|((bp)->ebuf[(bp)->icount-1]<<8)):Bgetle2((bp)))
 #define        BGETLE4(bp)\
-       ((bp)->icount<=-4?((bp)->icount+=4,((bp)->ebuf[(bp)->icount-4])|((bp)->ebuf[(bp)->icount-3]<<8)|((bp)->ebuf[(bp)->icount-2]<<16)|((bp)->ebuf[(bp)->icount-1]<<24)):Bgetle4((bp)))
+       (int)((bp)->icount<=-4?((bp)->icount+=4,((bp)->ebuf[(bp)->icount-4])|((bp)->ebuf[(bp)->icount-3]<<8)|((bp)->ebuf[(bp)->icount-2]<<16)|((uint32)(bp)->ebuf[(bp)->icount-1]<<24)):Bgetle4((bp)))
 
 /*
  * These macros put 1-, 2-, and 4-byte integer values by writing the
index c7ca91d1e7fca60c5d89caab01845199a80fa3ac..4886b73eb36d308ed43d043751c754dd644ba7c4 100644 (file)
@@ -1325,10 +1325,10 @@ compar(Node *n, int reverse)
                if(lt->width == 8)
                        hi = big(0, ~0ULL);
                else
-                       hi = big(0, (1LL<<(l->type->width*8))-1);
+                       hi = big(0, (1ULL<<(l->type->width*8))-1);
        }else{
-               lo = big(~0ULL, -(1LL<<(l->type->width*8-1)));
-               hi = big(0, (1LL<<(l->type->width*8-1))-1);
+               lo = big(~0ULL, -(1ULL<<(l->type->width*8-1)));
+               hi = big(0, (1ULL<<(l->type->width*8-1))-1);
        }
 
        switch(op){
index d1aa2e483b4615cd8314edcdf397d373a3d1f9f0..049dc5196e28577e8ae038496bbbde9bfc9e82e8 100644 (file)
@@ -1019,7 +1019,7 @@ hex:
                        c += 10-'A';
                else
                        goto bad;
-               nn = n*16 + c;
+               nn = (uvlong)n*16 + c;
                if(n < 0 && nn >= 0)
                        goto bad;
                n = nn;
index ccc0c405d0e39b4d6b42225161ae56d4940286a5..9d293b0898f1213796f04962bd822bd5f71a8ea8 100644 (file)
@@ -224,7 +224,7 @@ Sym*
 lookup(void)
 {
        Sym *s;
-       int32 h;
+       uint32 h;
        char *p;
        int c, l;
        char *r, *w;
@@ -400,7 +400,7 @@ l1:
                        if(c >= '0' && c <= '9') {
                                if(c > '7' && c1 == 3)
                                        break;
-                               yylval.lval <<= c1;
+                               yylval.lval = (uvlong)yylval.lval << c1;
                                yylval.lval += c - '0';
                                c = GETC();
                                continue;
@@ -410,7 +410,7 @@ l1:
                        if(c >= 'A' && c <= 'F')
                                c += 'a' - 'A';
                        if(c >= 'a' && c <= 'f') {
-                               yylval.lval <<= c1;
+                               yylval.lval = (uvlong)yylval.lval << c1;
                                yylval.lval += c - 'a' + 10;
                                c = GETC();
                                continue;
@@ -770,6 +770,6 @@ ieeedtod(Ieee *ieee, double native)
        f = 65536L;
        fr = modf(fr*f, &ho);
        ieee->l = ho;
-       ieee->l <<= 16;
+       ieee->l = (uint32)ieee->l << 16;
        ieee->l |= (int32)(fr*f);
 }
index e3edd720af42b1fc81268743dae719942fec4560..92834a97b90a148ec6aa96ba93e81dff1fc9e3e8 100644 (file)
@@ -41,7 +41,7 @@ bvset(Bvec *bv, int32 i)
 
        if(i < 0 || i >= bv->n)
                fatal("bvset: index %d is out of bounds with length %d\n", i, bv->n);
-       mask = 1 << (i % WORDBITS);
+       mask = 1U << (i % WORDBITS);
        bv->b[i / WORDBITS] |= mask;
 }
 
index 51f8fe67f8629d609a2907cf20c00e91da5db5a3..d7d626be46de54c4339bea71bb2f2fbea400d500 100644 (file)
@@ -78,7 +78,7 @@ typedef       struct  Strlit  Strlit;
 struct Strlit
 {
        int32   len;
-       char    s[3];   // variable
+       char    s[1]; // variable
 };
 
 enum
index 5856aab511c1ef426bc2cab04c0d2c273b0f5ea1..bbd4e298f05ea7de28fae7367c610cd2e4dfdcfc 100644 (file)
@@ -196,7 +196,7 @@ md5block(MD5 *dig, uchar *p, int nn)
 
                for(i=0; i<16; i++) {
                        j = i*4;
-                       X[i] = p[j] | (p[j+1]<<8) | (p[j+2]<<16) | (p[j+3]<<24);
+                       X[i] = p[j] | (p[j+1]<<8) | (p[j+2]<<16) | ((uint32)p[j+3]<<24);
                }
 
                // Round 1.
index 8e52ff21625101fc37a1171bfb3ae2b2121f6939..9b2f664f7b7a9be487710cebc41442493b699947 100644 (file)
@@ -565,11 +565,11 @@ mpgetfix(Mpint *a)
                return 0;
        }
 
-       v = (vlong)a->a[0];
-       v |= (vlong)a->a[1] << Mpscale;
-       v |= (vlong)a->a[2] << (Mpscale+Mpscale);
+       v = (uvlong)a->a[0];
+       v |= (uvlong)a->a[1] << Mpscale;
+       v |= (uvlong)a->a[2] << (Mpscale+Mpscale);
        if(a->neg)
-               v = -v;
+               v = -(uvlong)v;
        return v;
 }
 
@@ -586,7 +586,7 @@ mpmovecfix(Mpint *a, vlong c)
        x = c;
        if(x < 0) {
                a->neg = 1;
-               x = -x;
+               x = -(uvlong)x;
        }
 
        a1 = &a->a[0];
index 079ca305d5865e014ff4d9fb4a51ea28f003d26b..54fddbb9098563c975b4205d68876d422c771c9e 100644 (file)
@@ -322,7 +322,7 @@ setlineno(Node *n)
 uint32
 stringhash(char *p)
 {
-       int32 h;
+       uint32 h;
        int c;
 
        h = 0;
@@ -333,9 +333,9 @@ stringhash(char *p)
                h = h*PRIME1 + c;
        }
 
-       if(h < 0) {
+       if((int32)h < 0) {
                h = -h;
-               if(h < 0)
+               if((int32)h < 0)
                        h = 0;
        }
        return h;
index 85f9d48b391d288a8e57047ab14ed610b9dd3cea..39ffa3d87343cff58b561357a4775dd3a3773bc2 100644 (file)
@@ -37,13 +37,12 @@ static void imported(char *pkg, char *import);
 static int
 hashstr(char *name)
 {
-       int h;
+       uint32 h;
        char *cp;
 
        h = 0;
        for(cp = name; *cp; h += *cp++)
                h *= 1119;
-       // not if(h < 0) h = ~h, because gcc 4.3 -O2 miscompiles it.
        h &= 0xffffff;
        return h;
 }
index 0d67ae999b2165dffaafb82a7a5f14801d6f81b7..b3c3713e2da20a562982587f0798e0dadd148458 100644 (file)
@@ -951,7 +951,7 @@ _lookup(char *symb, int v, int creat)
 {
        Sym *s;
        char *p;
-       int32 h;
+       uint32 h;
        int c;
 
        h = v;
@@ -1613,7 +1613,7 @@ le16(uchar *b)
 uint32
 le32(uchar *b)
 {
-       return b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24;
+       return b[0] | b[1]<<8 | b[2]<<16 | (uint32)b[3]<<24;
 }
 
 uint64
@@ -1631,7 +1631,7 @@ be16(uchar *b)
 uint32
 be32(uchar *b)
 {
-       return b[0]<<24 | b[1]<<16 | b[2]<<8 | b[3];
+       return (uint32)b[0]<<24 | b[1]<<16 | b[2]<<8 | b[3];
 }
 
 uint64
index aff5f37eb97360724825c3aa30e1b219b9b1fa2c..5b300dbb966060185cc09c7add0e0ec662d64df5 100644 (file)
@@ -937,21 +937,12 @@ objsym(Sym *s, void *p)
 int
 hashstr(char *name)
 {
-       int h;
+       uint32 h;
        char *cp;
 
        h = 0;
        for(cp = name; *cp; h += *cp++)
                h *= 1119;
-       
-       // the code used to say
-       //      if(h < 0)
-       //              h = ~h;
-       // but on gcc 4.3 with -O2 on some systems,
-       // the if(h < 0) gets compiled away as not possible.
-       // use a mask instead, leaving plenty of bits but
-       // definitely not the sign bit.
-
        return h & 0xfffffff;
 }
 
index c2a7931e1ac37f5aa8cbc935f0091d6af8bce7e8..48fc49fd349bbcf0f7cae354f2727d1d739eda35 100644 (file)
@@ -130,7 +130,7 @@ addr(Biobuf *bp)
                BGETC(bp);
                break;
        case D_CONST2:
-               BGETLE4(bp);    // fall through
+               Bgetle4(bp); // fall through
        case D_OREG:
        case D_CONST:
        case D_BRANCH:
index 9971ccfcad419c3801f4c8810716c610a84f3894..9a7c9ac21d5c1d52a8803ee5f2500c3288cef264 100644 (file)
@@ -134,7 +134,7 @@ addr(Biobuf *bp)
                        off = ((vlong)l << 32) | (off & 0xFFFFFFFF);
                }
                if(off < 0)
-                       off = -off;
+                       off = -(uvlong)off;
        }
        if(a.flags & T_SYM)
                a.sym = BGETC(bp);
index efa61252e134c34f422ac14a0eba966ba4d63c36..e11a7dfd14e6dee1838b2b6b4a4eb9cd5ec07e92 100644 (file)
@@ -131,7 +131,7 @@ addr(Biobuf *bp)
                        off = -off;
        }
        if(a.flags & T_OFFSET2){
-               BGETLE4(bp);
+               Bgetle4(bp);
        }
        if(a.flags & T_SYM)
                a.sym = BGETC(bp);
index 0e1421d859dd562a7cbc0538f172dd9bfae9d702..729a3eab8093bdc5dbe57b22278f75d3dabd7b68 100644 (file)
@@ -244,7 +244,7 @@ processprog(Prog *p, int doautos)
 static void
 objlookup(int id, char *name, int type, uint sig)
 {
-       int32 h;
+       uint32 h;
        char *cp;
        Sym *s;
        Symtab *sp;