1 /* { dg-do run } */
2 /* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */
3 
4 #include <stdio.h>
5 
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)
10 
11 void __attribute__((noinline,noclone))
check(int i,int j)12 check (int i, int j)
13 {
14   if (i != j)
15     __builtin_abort ();
16 }
17 
18 int
main(void)19 main (void)
20 {
21   fputs ("UBSAN TEST START\n", stderr);
22 
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);
33 
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
43 
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
53 
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
62 
63   fputs ("UBSAN TEST END\n", stderr);
64   return 0;
65 }
66 
67 /* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */
68