0f2ea59df491ac4692dbe7c5c4d9c41135d50922
[gcc.git] / gcc / testsuite / c-c++-common / ubsan / overflow-mul-1.c
1 /* { dg-do run } */
2 /* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */
3 /* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */
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 /* Test integer promotion. */
21 #if __SCHAR_MAX__ == 127
22 volatile signed char a = -2;
23 volatile signed char b = SCHAR_MAX;
24 volatile signed char c = a * b;
25 check (c, 2);
26 #endif
27
28 #if __SHRT_MAX__ == 32767
29 volatile short d = SHRT_MAX;
30 volatile short e = 2;
31 volatile short f = d * e;
32 check (f, -2);
33 #endif
34
35 #if __INT_MAX__ == 2147483647
36 volatile int m = INT_MAX;
37 volatile int n = 1;
38 volatile int o = m * n;
39 check (o, INT_MAX);
40
41 m = INT_MIN;
42 o = m * n;
43 check (o, INT_MIN);
44 #endif
45
46 return 0;
47 }