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
#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
......
......@@ -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){
......
......@@ -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;
......
......@@ -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);
}
......@@ -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;
}
......
......@@ -78,7 +78,7 @@ typedef struct Strlit Strlit;
struct Strlit
{
int32 len;
char s[3]; // variable
char s[1]; // variable
};
enum
......
......@@ -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.
......
......@@ -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];
......
......@@ -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;
......
......@@ -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;
}
......
......@@ -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
......
......@@ -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;
}
......
......@@ -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:
......
......@@ -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);
......
......@@ -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);
......
......@@ -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;
......
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