Skip to content

Commit acba82a

Browse files
visitorckwjserv
authored andcommitted
Fix undefined behavior caused by left-shifting signed 1LL by 63
According to the C11 standard (ISO/IEC 9899:2011, 6.5.7), left-shifting a signed value into the sign bit results in undefined behavior if the result is not representable in the type. Specifically, the expression 1LL << 63 invokes undefined behavior because 1LL is a signed long long, and 2^63 exceeds its positive range. Replace 1LL << 63 with 1ULL << 63 to ensure the shift is performed on an unsigned value, which is well-defined.
1 parent 551e096 commit acba82a

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

app/test64.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void task(void)
4747
}
4848

4949
printf("test signed divide...\n");
50-
c = stuff.num | 1LL << 63;
50+
c = stuff.num | 1ULL << 63;
5151
for (i = 0; i < 20; i++) {
5252
stuff.unum = c / d;
5353
printf("%x%x / %x%x = %x%x\n", (uint32_t) (c >> 32),
@@ -64,15 +64,15 @@ void task(void)
6464
stuff.unum <<= 1;
6565
}
6666

67-
stuff.unum = 1LL << 63;
67+
stuff.unum = 1ULL << 63;
6868

6969
printf("testing right (logical) shifts..\n");
7070
for (i = 0; i < 64; i++) {
7171
printf("%x %x\n", stuff.l.high, stuff.l.low);
7272
stuff.unum >>= 1;
7373
}
7474

75-
stuff.unum = 1LL << 63;
75+
stuff.unum = 1ULL << 63;
7676

7777
printf("testing right (arithmetic) shifts..\n");
7878
for (i = 0; i < 64; i++) {

0 commit comments

Comments
 (0)