Debian Codegraph @

gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c

raw
1 /* { dg-do run } */
2 /* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable -fno-sanitize-recover=signed-integer-overflow" } */
3 /* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable -fno-sanitize-recover=signed-integer-overflow -Wno-volatile" { target c++ } } */
4
5 #define SCHAR_MAX __SCHAR_MAX__
6 #define SHRT_MAX __SHRT_MAX__
7 #define INT_MAX __INT_MAX__
8 #define INT_MIN (-__INT_MAX__ - 1)
9
10 void __attribute__((noinline,noclone))
11 check (int i, int j)
12 {
13 if (i != j)
14 __builtin_abort ();
15 }
16
17 int
18 main (void)
19 {
20 #if __INT_MAX__ == 2147483647
21 /* Here, nothing should fail. */
22 volatile int j = INT_MAX;
23 volatile int i = -1;
24 volatile int k = j + i;
25 check (k, 2147483646);
26 k = i + j;
27 check (k, 2147483646);
28 j--;
29 check (j, 2147483646);
30
31 i = 1;
32 j = INT_MIN;
33 k = i + j;
34 check (k, -2147483647);
35 k = j + i;
36 check (k, -2147483647);
37 j++;
38 check (j, -2147483647);
39 #endif
40
41 /* Test integer promotion. */
42 #if __SCHAR_MAX__ == 127
43 volatile signed char a = SCHAR_MAX;
44 volatile signed char b = 1;
45 volatile signed char c = a + b;
46 check (c, -128);
47 a++;
48 check (a, -128);
49 #endif
50
51 #if __SHRT_MAX__ == 32767
52 volatile short d = SHRT_MAX;
53 volatile short e = 1;
54 volatile short f = d + e;
55 check (f, -32768);
56 d++;
57 check (d, -32768);
58 #endif
59 return 0;
60 }