Skip to content

Commit 7d734d9

Browse files
committed
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
1 parent 6252b41 commit 7d734d9

File tree

16 files changed

+29
-39
lines changed

16 files changed

+29
-39
lines changed

include/bio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct Biobuf
7979
#define BGETLE2(bp)\
8080
((bp)->icount<=-2?((bp)->icount+=2,((bp)->ebuf[(bp)->icount-2])|((bp)->ebuf[(bp)->icount-1]<<8)):Bgetle2((bp)))
8181
#define BGETLE4(bp)\
82-
((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)))
82+
(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)))
8383

8484
/*
8585
* These macros put 1-, 2-, and 4-byte integer values by writing the

src/cmd/cc/com.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,10 +1325,10 @@ compar(Node *n, int reverse)
13251325
if(lt->width == 8)
13261326
hi = big(0, ~0ULL);
13271327
else
1328-
hi = big(0, (1LL<<(l->type->width*8))-1);
1328+
hi = big(0, (1ULL<<(l->type->width*8))-1);
13291329
}else{
1330-
lo = big(~0ULL, -(1LL<<(l->type->width*8-1)));
1331-
hi = big(0, (1LL<<(l->type->width*8-1))-1);
1330+
lo = big(~0ULL, -(1ULL<<(l->type->width*8-1)));
1331+
hi = big(0, (1ULL<<(l->type->width*8-1))-1);
13321332
}
13331333

13341334
switch(op){

src/cmd/cc/lex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ mpatov(char *s, vlong *v)
10191019
c += 10-'A';
10201020
else
10211021
goto bad;
1022-
nn = n*16 + c;
1022+
nn = (uvlong)n*16 + c;
10231023
if(n < 0 && nn >= 0)
10241024
goto bad;
10251025
n = nn;

src/cmd/cc/lexbody

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ Sym*
224224
lookup(void)
225225
{
226226
Sym *s;
227-
int32 h;
227+
uint32 h;
228228
char *p;
229229
int c, l;
230230
char *r, *w;
@@ -400,7 +400,7 @@ l1:
400400
if(c >= '0' && c <= '9') {
401401
if(c > '7' && c1 == 3)
402402
break;
403-
yylval.lval <<= c1;
403+
yylval.lval = (uvlong)yylval.lval << c1;
404404
yylval.lval += c - '0';
405405
c = GETC();
406406
continue;
@@ -410,7 +410,7 @@ l1:
410410
if(c >= 'A' && c <= 'F')
411411
c += 'a' - 'A';
412412
if(c >= 'a' && c <= 'f') {
413-
yylval.lval <<= c1;
413+
yylval.lval = (uvlong)yylval.lval << c1;
414414
yylval.lval += c - 'a' + 10;
415415
c = GETC();
416416
continue;
@@ -770,6 +770,6 @@ ieeedtod(Ieee *ieee, double native)
770770
f = 65536L;
771771
fr = modf(fr*f, &ho);
772772
ieee->l = ho;
773-
ieee->l <<= 16;
773+
ieee->l = (uint32)ieee->l << 16;
774774
ieee->l |= (int32)(fr*f);
775775
}

src/cmd/gc/bv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ bvset(Bvec *bv, int32 i)
4141

4242
if(i < 0 || i >= bv->n)
4343
fatal("bvset: index %d is out of bounds with length %d\n", i, bv->n);
44-
mask = 1 << (i % WORDBITS);
44+
mask = 1U << (i % WORDBITS);
4545
bv->b[i / WORDBITS] |= mask;
4646
}
4747

src/cmd/gc/go.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ typedef struct Strlit Strlit;
7878
struct Strlit
7979
{
8080
int32 len;
81-
char s[3]; // variable
81+
char s[1]; // variable
8282
};
8383

8484
enum

src/cmd/gc/md5.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ md5block(MD5 *dig, uchar *p, int nn)
196196

197197
for(i=0; i<16; i++) {
198198
j = i*4;
199-
X[i] = p[j] | (p[j+1]<<8) | (p[j+2]<<16) | (p[j+3]<<24);
199+
X[i] = p[j] | (p[j+1]<<8) | (p[j+2]<<16) | ((uint32)p[j+3]<<24);
200200
}
201201

202202
// Round 1.

src/cmd/gc/mparith2.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,11 @@ mpgetfix(Mpint *a)
565565
return 0;
566566
}
567567

568-
v = (vlong)a->a[0];
569-
v |= (vlong)a->a[1] << Mpscale;
570-
v |= (vlong)a->a[2] << (Mpscale+Mpscale);
568+
v = (uvlong)a->a[0];
569+
v |= (uvlong)a->a[1] << Mpscale;
570+
v |= (uvlong)a->a[2] << (Mpscale+Mpscale);
571571
if(a->neg)
572-
v = -v;
572+
v = -(uvlong)v;
573573
return v;
574574
}
575575

@@ -586,7 +586,7 @@ mpmovecfix(Mpint *a, vlong c)
586586
x = c;
587587
if(x < 0) {
588588
a->neg = 1;
589-
x = -x;
589+
x = -(uvlong)x;
590590
}
591591

592592
a1 = &a->a[0];

src/cmd/gc/subr.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ setlineno(Node *n)
322322
uint32
323323
stringhash(char *p)
324324
{
325-
int32 h;
325+
uint32 h;
326326
int c;
327327

328328
h = 0;
@@ -333,9 +333,9 @@ stringhash(char *p)
333333
h = h*PRIME1 + c;
334334
}
335335

336-
if(h < 0) {
336+
if((int32)h < 0) {
337337
h = -h;
338-
if(h < 0)
338+
if((int32)h < 0)
339339
h = 0;
340340
}
341341
return h;

src/cmd/ld/go.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ static void imported(char *pkg, char *import);
3737
static int
3838
hashstr(char *name)
3939
{
40-
int h;
40+
uint32 h;
4141
char *cp;
4242

4343
h = 0;
4444
for(cp = name; *cp; h += *cp++)
4545
h *= 1119;
46-
// not if(h < 0) h = ~h, because gcc 4.3 -O2 miscompiles it.
4746
h &= 0xffffff;
4847
return h;
4948
}

src/cmd/ld/lib.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ _lookup(char *symb, int v, int creat)
951951
{
952952
Sym *s;
953953
char *p;
954-
int32 h;
954+
uint32 h;
955955
int c;
956956

957957
h = v;
@@ -1613,7 +1613,7 @@ le16(uchar *b)
16131613
uint32
16141614
le32(uchar *b)
16151615
{
1616-
return b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24;
1616+
return b[0] | b[1]<<8 | b[2]<<16 | (uint32)b[3]<<24;
16171617
}
16181618

16191619
uint64
@@ -1631,7 +1631,7 @@ be16(uchar *b)
16311631
uint32
16321632
be32(uchar *b)
16331633
{
1634-
return b[0]<<24 | b[1]<<16 | b[2]<<8 | b[3];
1634+
return (uint32)b[0]<<24 | b[1]<<16 | b[2]<<8 | b[3];
16351635
}
16361636

16371637
uint64

src/cmd/pack/ar.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -937,21 +937,12 @@ objsym(Sym *s, void *p)
937937
int
938938
hashstr(char *name)
939939
{
940-
int h;
940+
uint32 h;
941941
char *cp;
942942

943943
h = 0;
944944
for(cp = name; *cp; h += *cp++)
945945
h *= 1119;
946-
947-
// the code used to say
948-
// if(h < 0)
949-
// h = ~h;
950-
// but on gcc 4.3 with -O2 on some systems,
951-
// the if(h < 0) gets compiled away as not possible.
952-
// use a mask instead, leaving plenty of bits but
953-
// definitely not the sign bit.
954-
955946
return h & 0xfffffff;
956947
}
957948

src/libmach/5obj.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ addr(Biobuf *bp)
130130
BGETC(bp);
131131
break;
132132
case D_CONST2:
133-
BGETLE4(bp); // fall through
133+
Bgetle4(bp); // fall through
134134
case D_OREG:
135135
case D_CONST:
136136
case D_BRANCH:

src/libmach/6obj.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ addr(Biobuf *bp)
134134
off = ((vlong)l << 32) | (off & 0xFFFFFFFF);
135135
}
136136
if(off < 0)
137-
off = -off;
137+
off = -(uvlong)off;
138138
}
139139
if(a.flags & T_SYM)
140140
a.sym = BGETC(bp);

src/libmach/8obj.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ addr(Biobuf *bp)
131131
off = -off;
132132
}
133133
if(a.flags & T_OFFSET2){
134-
BGETLE4(bp);
134+
Bgetle4(bp);
135135
}
136136
if(a.flags & T_SYM)
137137
a.sym = BGETC(bp);

src/libmach/obj.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ processprog(Prog *p, int doautos)
244244
static void
245245
objlookup(int id, char *name, int type, uint sig)
246246
{
247-
int32 h;
247+
uint32 h;
248248
char *cp;
249249
Sym *s;
250250
Symtab *sp;

0 commit comments

Comments
 (0)