|
1 | 1 | #include <inttypes.h> |
2 | 2 |
|
3 | | -int main() |
| 3 | +void leftshift_overflow1(unsigned char x) |
4 | 4 | { |
5 | | - unsigned char x; |
6 | | - |
7 | 5 | // signed, owing to promotion, and may overflow |
8 | 6 | unsigned r = x << ((sizeof(unsigned) - 1) * 8 + 1); |
| 7 | +} |
9 | 8 |
|
| 9 | +void leftshift_overflow2(unsigned char x) |
| 10 | +{ |
10 | 11 | // signed, owing to promotion, and cannot overflow |
11 | | - r = x << ((sizeof(unsigned) - 1) * 8 - 1); |
| 12 | + unsigned r = x << ((sizeof(unsigned) - 1) * 8 - 1); |
| 13 | +} |
12 | 14 |
|
| 15 | +void leftshift_overflow3(unsigned char x) |
| 16 | +{ |
13 | 17 | // unsigned |
14 | | - r = (unsigned)x << ((sizeof(unsigned) - 1) * 8); |
| 18 | + unsigned r = (unsigned)x << ((sizeof(unsigned) - 1) * 8); |
| 19 | +} |
15 | 20 |
|
| 21 | +void leftshift_overflow4(unsigned char x) |
| 22 | +{ |
16 | 23 | // negative value or zero, not an overflow |
17 | 24 | int s = -x << ((sizeof(int) - 1) * 8); |
| 25 | +} |
18 | 26 |
|
| 27 | +void leftshift_overflow5(unsigned char x) |
| 28 | +{ |
19 | 29 | // overflow |
20 | | - s = 1 << x; |
| 30 | + int s = 1 << x; |
| 31 | +} |
| 32 | + |
| 33 | +int nondet_int(); |
21 | 34 |
|
| 35 | +void leftshift_overflow6(unsigned char x) |
| 36 | +{ |
22 | 37 | // distance too far, not an overflow |
23 | | - s = s << 100; |
| 38 | + int s = nondet_int(); |
| 39 | + int t = s << 100; |
| 40 | +} |
24 | 41 |
|
| 42 | +void leftshift_overflow7(unsigned char x) |
| 43 | +{ |
25 | 44 | // not an overflow in ANSI-C, but undefined in C99 |
26 | | - s = 1 << (sizeof(int) * 8 - 1); |
| 45 | + int s = 1 << (sizeof(int) * 8 - 1); |
| 46 | +} |
27 | 47 |
|
| 48 | +void leftshift_overflow8(unsigned char x) |
| 49 | +{ |
28 | 50 | // overflow in an expression where operand and distance types are different |
29 | 51 | uint32_t u32; |
30 | 52 | int64_t i64 = ((int64_t)u32) << 32; |
| 53 | +} |
| 54 | + |
| 55 | +unsigned char nondet_uchar(); |
| 56 | + |
| 57 | +int main() |
| 58 | +{ |
| 59 | + unsigned char x = nondet_uchar(); |
| 60 | + |
| 61 | + switch(nondet_uchar()) |
| 62 | + { |
| 63 | + case 1: |
| 64 | + leftshift_overflow1(x); |
| 65 | + break; |
| 66 | + case 2: |
| 67 | + leftshift_overflow2(x); |
| 68 | + break; |
| 69 | + case 3: |
| 70 | + leftshift_overflow3(x); |
| 71 | + break; |
| 72 | + case 4: |
| 73 | + leftshift_overflow4(x); |
| 74 | + break; |
| 75 | + case 5: |
| 76 | + leftshift_overflow5(x); |
| 77 | + break; |
| 78 | + case 6: |
| 79 | + leftshift_overflow6(x); |
| 80 | + break; |
| 81 | + case 7: |
| 82 | + leftshift_overflow7(x); |
| 83 | + break; |
| 84 | + case 8: |
| 85 | + leftshift_overflow8(x); |
| 86 | + break; |
| 87 | + } |
31 | 88 |
|
32 | 89 | return 0; |
33 | 90 | } |
0 commit comments