Merged revisions 208012,208018-208019,208021,208023-208030,208033,208037,208040-20804...
[official-gcc.git] / main / gcc / testsuite / c-c++-common / ubsan / overflow-add-1.c
blobfd6c6d325e8137e09bceb289c3151acc84fbfeab
1 /* { dg-do run } */
2 /* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */
4 #include <stdio.h>
6 #define SCHAR_MAX __SCHAR_MAX__
7 #define SHRT_MAX __SHRT_MAX__
8 #define INT_MAX __INT_MAX__
9 #define INT_MIN (-__INT_MAX__ - 1)
11 void __attribute__((noinline,noclone))
12 check (int i, int j)
14 if (i != j)
15 __builtin_abort ();
18 int
19 main (void)
21 fputs ("UBSAN TEST START\n", stderr);
23 #if __INT_MAX__ == 2147483647
24 /* Here, nothing should fail. */
25 volatile int j = INT_MAX;
26 volatile int i = -1;
27 volatile int k = j + i;
28 check (k, 2147483646);
29 k = i + j;
30 check (k, 2147483646);
31 j--;
32 check (j, 2147483646);
34 i = 1;
35 j = INT_MIN;
36 k = i + j;
37 check (k, -2147483647);
38 k = j + i;
39 check (k, -2147483647);
40 j++;
41 check (j, -2147483647);
42 #endif
44 /* Test integer promotion. */
45 #if __SCHAR_MAX__ == 127
46 volatile signed char a = SCHAR_MAX;
47 volatile signed char b = 1;
48 volatile signed char c = a + b;
49 check (c, -128);
50 a++;
51 check (a, -128);
52 #endif
54 #if __SHRT_MAX__ == 32767
55 volatile short d = SHRT_MAX;
56 volatile short e = 1;
57 volatile short f = d + e;
58 check (f, -32768);
59 d++;
60 check (d, -32768);
61 #endif
63 fputs ("UBSAN TEST END\n", stderr);
64 return 0;
67 /* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */