Commit 7d734d92 authored by Russ Cox's avatar Russ Cox

build: remove various uses of C undefined behavior

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
parent 6252b419
...@@ -79,7 +79,7 @@ struct Biobuf ...@@ -79,7 +79,7 @@ struct Biobuf
#define BGETLE2(bp)\ #define BGETLE2(bp)\
((bp)->icount<=-2?((bp)->icount+=2,((bp)->ebuf[(bp)->icount-2])|((bp)->ebuf[(bp)->icount-1]<<8)):Bgetle2((bp))) ((bp)->icount<=-2?((bp)->icount+=2,((bp)->ebuf[(bp)->icount-2])|((bp)->ebuf[(bp)->icount-1]<<8)):Bgetle2((bp)))
#define BGETLE4(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 * These macros put 1-, 2-, and 4-byte integer values by writing the
......
...@@ -1325,10 +1325,10 @@ compar(Node *n, int reverse) ...@@ -1325,10 +1325,10 @@ compar(Node *n, int reverse)
if(lt->width == 8) if(lt->width == 8)
hi = big(0, ~0ULL); hi = big(0, ~0ULL);
else else
hi = big(0, (1LL<<(l->type->width*8))-1); hi = big(0, (1ULL<<(l->type->width*8))-1);
}else{ }else{
lo = big(~0ULL, -(1LL<<(l->type->width*8-1))); lo = big(~0ULL, -(1ULL<<(l->type->width*8-1)));
hi = big(0, (1LL<<(l->type->width*8-1))-1); hi = big(0, (1ULL<<(l->type->width*8-1))-1);
} }
switch(op){ switch(op){
......
...@@ -1019,7 +1019,7 @@ hex: ...@@ -1019,7 +1019,7 @@ hex:
c += 10-'A'; c += 10-'A';
else else
goto bad; goto bad;
nn = n*16 + c; nn = (uvlong)n*16 + c;
if(n < 0 && nn >= 0) if(n < 0 && nn >= 0)
goto bad; goto bad;
n = nn; n = nn;
......
...@@ -224,7 +224,7 @@ Sym* ...@@ -224,7 +224,7 @@ Sym*
lookup(void) lookup(void)
{ {
Sym *s; Sym *s;
int32 h; uint32 h;
char *p; char *p;
int c, l; int c, l;
char *r, *w; char *r, *w;
...@@ -400,7 +400,7 @@ l1: ...@@ -400,7 +400,7 @@ l1:
if(c >= '0' && c <= '9') { if(c >= '0' && c <= '9') {
if(c > '7' && c1 == 3) if(c > '7' && c1 == 3)
break; break;
yylval.lval <<= c1; yylval.lval = (uvlong)yylval.lval << c1;
yylval.lval += c - '0'; yylval.lval += c - '0';
c = GETC(); c = GETC();
continue; continue;
...@@ -410,7 +410,7 @@ l1: ...@@ -410,7 +410,7 @@ l1:
if(c >= 'A' && c <= 'F') if(c >= 'A' && c <= 'F')
c += 'a' - 'A'; c += 'a' - 'A';
if(c >= 'a' && c <= 'f') { if(c >= 'a' && c <= 'f') {
yylval.lval <<= c1; yylval.lval = (uvlong)yylval.lval << c1;
yylval.lval += c - 'a' + 10; yylval.lval += c - 'a' + 10;
c = GETC(); c = GETC();
continue; continue;
...@@ -770,6 +770,6 @@ ieeedtod(Ieee *ieee, double native) ...@@ -770,6 +770,6 @@ ieeedtod(Ieee *ieee, double native)
f = 65536L; f = 65536L;
fr = modf(fr*f, &ho); fr = modf(fr*f, &ho);
ieee->l = ho; ieee->l = ho;
ieee->l <<= 16; ieee->l = (uint32)ieee->l << 16;
ieee->l |= (int32)(fr*f); ieee->l |= (int32)(fr*f);
} }
...@@ -41,7 +41,7 @@ bvset(Bvec *bv, int32 i) ...@@ -41,7 +41,7 @@ bvset(Bvec *bv, int32 i)
if(i < 0 || i >= bv->n) if(i < 0 || i >= bv->n)
fatal("bvset: index %d is out of bounds with length %d\n", 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; bv->b[i / WORDBITS] |= mask;
} }
......
...@@ -78,7 +78,7 @@ typedef struct Strlit Strlit; ...@@ -78,7 +78,7 @@ typedef struct Strlit Strlit;
struct Strlit struct Strlit
{ {
int32 len; int32 len;
char s[3]; // variable char s[1]; // variable
}; };
enum enum
......
...@@ -196,7 +196,7 @@ md5block(MD5 *dig, uchar *p, int nn) ...@@ -196,7 +196,7 @@ md5block(MD5 *dig, uchar *p, int nn)
for(i=0; i<16; i++) { for(i=0; i<16; i++) {
j = i*4; 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. // Round 1.
......
...@@ -565,11 +565,11 @@ mpgetfix(Mpint *a) ...@@ -565,11 +565,11 @@ mpgetfix(Mpint *a)
return 0; return 0;
} }
v = (vlong)a->a[0]; v = (uvlong)a->a[0];
v |= (vlong)a->a[1] << Mpscale; v |= (uvlong)a->a[1] << Mpscale;
v |= (vlong)a->a[2] << (Mpscale+Mpscale); v |= (uvlong)a->a[2] << (Mpscale+Mpscale);
if(a->neg) if(a->neg)
v = -v; v = -(uvlong)v;
return v; return v;
} }
...@@ -586,7 +586,7 @@ mpmovecfix(Mpint *a, vlong c) ...@@ -586,7 +586,7 @@ mpmovecfix(Mpint *a, vlong c)
x = c; x = c;
if(x < 0) { if(x < 0) {
a->neg = 1; a->neg = 1;
x = -x; x = -(uvlong)x;
} }
a1 = &a->a[0]; a1 = &a->a[0];
......
...@@ -322,7 +322,7 @@ setlineno(Node *n) ...@@ -322,7 +322,7 @@ setlineno(Node *n)
uint32 uint32
stringhash(char *p) stringhash(char *p)
{ {
int32 h; uint32 h;
int c; int c;
h = 0; h = 0;
...@@ -333,9 +333,9 @@ stringhash(char *p) ...@@ -333,9 +333,9 @@ stringhash(char *p)
h = h*PRIME1 + c; h = h*PRIME1 + c;
} }
if(h < 0) { if((int32)h < 0) {
h = -h; h = -h;
if(h < 0) if((int32)h < 0)
h = 0; h = 0;
} }
return h; return h;
......
...@@ -37,13 +37,12 @@ static void imported(char *pkg, char *import); ...@@ -37,13 +37,12 @@ static void imported(char *pkg, char *import);
static int static int
hashstr(char *name) hashstr(char *name)
{ {
int h; uint32 h;
char *cp; char *cp;
h = 0; h = 0;
for(cp = name; *cp; h += *cp++) for(cp = name; *cp; h += *cp++)
h *= 1119; h *= 1119;
// not if(h < 0) h = ~h, because gcc 4.3 -O2 miscompiles it.
h &= 0xffffff; h &= 0xffffff;
return h; return h;
} }
......
...@@ -951,7 +951,7 @@ _lookup(char *symb, int v, int creat) ...@@ -951,7 +951,7 @@ _lookup(char *symb, int v, int creat)
{ {
Sym *s; Sym *s;
char *p; char *p;
int32 h; uint32 h;
int c; int c;
h = v; h = v;
...@@ -1613,7 +1613,7 @@ le16(uchar *b) ...@@ -1613,7 +1613,7 @@ le16(uchar *b)
uint32 uint32
le32(uchar *b) 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 uint64
...@@ -1631,7 +1631,7 @@ be16(uchar *b) ...@@ -1631,7 +1631,7 @@ be16(uchar *b)
uint32 uint32
be32(uchar *b) 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 uint64
......
...@@ -937,21 +937,12 @@ objsym(Sym *s, void *p) ...@@ -937,21 +937,12 @@ objsym(Sym *s, void *p)
int int
hashstr(char *name) hashstr(char *name)
{ {
int h; uint32 h;
char *cp; char *cp;
h = 0; h = 0;
for(cp = name; *cp; h += *cp++) for(cp = name; *cp; h += *cp++)
h *= 1119; 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; return h & 0xfffffff;
} }
......
...@@ -130,7 +130,7 @@ addr(Biobuf *bp) ...@@ -130,7 +130,7 @@ addr(Biobuf *bp)
BGETC(bp); BGETC(bp);
break; break;
case D_CONST2: case D_CONST2:
BGETLE4(bp); // fall through Bgetle4(bp); // fall through
case D_OREG: case D_OREG:
case D_CONST: case D_CONST:
case D_BRANCH: case D_BRANCH:
......
...@@ -134,7 +134,7 @@ addr(Biobuf *bp) ...@@ -134,7 +134,7 @@ addr(Biobuf *bp)
off = ((vlong)l << 32) | (off & 0xFFFFFFFF); off = ((vlong)l << 32) | (off & 0xFFFFFFFF);
} }
if(off < 0) if(off < 0)
off = -off; off = -(uvlong)off;
} }
if(a.flags & T_SYM) if(a.flags & T_SYM)
a.sym = BGETC(bp); a.sym = BGETC(bp);
......
...@@ -131,7 +131,7 @@ addr(Biobuf *bp) ...@@ -131,7 +131,7 @@ addr(Biobuf *bp)
off = -off; off = -off;
} }
if(a.flags & T_OFFSET2){ if(a.flags & T_OFFSET2){
BGETLE4(bp); Bgetle4(bp);
} }
if(a.flags & T_SYM) if(a.flags & T_SYM)
a.sym = BGETC(bp); a.sym = BGETC(bp);
......
...@@ -244,7 +244,7 @@ processprog(Prog *p, int doautos) ...@@ -244,7 +244,7 @@ processprog(Prog *p, int doautos)
static void static void
objlookup(int id, char *name, int type, uint sig) objlookup(int id, char *name, int type, uint sig)
{ {
int32 h; uint32 h;
char *cp; char *cp;
Sym *s; Sym *s;
Symtab *sp; Symtab *sp;
......
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