Merge from trunk
[official-gcc.git] / gcc / testsuite / c-c++-common / ubsan / overflow-add-1.c
blob436082d21d95b7799fbeac5194d8c6f2d0160671
1 /* { dg-do run } */
2 /* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */
3 /* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */
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)
10 void __attribute__((noinline,noclone))
11 check (int i, int j)
13 if (i != j)
14 __builtin_abort ();
17 int
18 main (void)
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);
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
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
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
60 return 0;