]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/g++.dg/ubsan/pr63956.C
34b5dd71ec2d142393db869a485015283468bf8d
[thirdparty/gcc.git] / gcc / testsuite / g++.dg / ubsan / pr63956.C
1 // PR sanitizer/63956
2 // { dg-do compile }
3 // { dg-options "-std=c++14 -fsanitize=undefined,float-divide-by-zero,float-cast-overflow" }
4
5 #define SA(X) static_assert((X),#X)
6 #define INT_MIN (-__INT_MAX__ - 1)
7
8 constexpr int
9 fn1 (int a, int b)
10 {
11 if (b != 2)
12 a <<= b;
13 // { dg-error "5 << -2.. is negative" "" { target *-*-* } .-1 }
14 // { dg-error "is >= than the precision of the left operand" "" { target *-*-* } .-2 }
15 // { dg-error "-2 << 4.. is negative" "" { target *-*-* } .-3 }
16 return a;
17 }
18
19 constexpr int i1 = fn1 (5, 3);
20 constexpr int i2 = fn1 (5, -2); // { dg-message "in .constexpr. expansion" }
21 constexpr int i3 = fn1 (5, sizeof (int) * __CHAR_BIT__); // { dg-message "in .constexpr. expansion" }
22 constexpr int i4 = fn1 (5, 256); // { dg-message "in .constexpr. expansion" }
23 constexpr int i5 = fn1 (5, 2);
24 constexpr int i6 = fn1 (-2, 4); // { dg-message "in .constexpr. expansion" }
25 constexpr int i7 = fn1 (0, 2);
26
27 SA (i1 == 40);
28 SA (i5 == 5);
29 SA (i7 == 0);
30
31 constexpr int
32 fn2 (int a, int b)
33 {
34 if (b != 2)
35 a >>= b;
36 // { dg-error "4 >> -1.. is negative" "" { target *-*-* } .-1 }
37 // { dg-error "is >= than the precision of the left operand" "" { target *-*-* } .-2 }
38
39 return a;
40 }
41
42 constexpr int j1 = fn2 (4, 1);
43 constexpr int j2 = fn2 (4, -1); // { dg-message "in .constexpr. expansion" }
44 constexpr int j3 = fn2 (10, sizeof (int) * __CHAR_BIT__); // { dg-message "in .constexpr. expansion" }
45 constexpr int j4 = fn2 (1, 256); // { dg-message "in .constexpr. expansion" }
46 constexpr int j5 = fn2 (5, 2);
47 constexpr int j6 = fn2 (-2, 4);
48 constexpr int j7 = fn2 (0, 4);
49
50 SA (j1 == 2);
51 SA (j5 == 5);
52 SA (j7 == 0);
53
54 constexpr int
55 fn3 (int a, int b)
56 {
57 if (b != 2)
58 a = a / b; // { dg-error "..7 / 0.. is not a constant expression" }
59 return a;
60 }
61
62 constexpr int k1 = fn3 (8, 4);
63 constexpr int k2 = fn3 (7, 0); // { dg-message "in .constexpr. expansion" }
64 constexpr int k3 = fn3 (INT_MIN, -1); // { dg-error "overflow in constant expression|in .constexpr. expansion of " }
65
66 SA (k1 == 2);
67
68 constexpr float
69 fn4 (float a, float b)
70 {
71 if (b != 2.0)
72 a = a / b; // { dg-error "is not a constant expression" }
73 return a;
74 }
75
76 constexpr float l1 = fn4 (5.0, 3.0);
77 constexpr float l2 = fn4 (7.0, 0.0); // { dg-message "in .constexpr. expansion" }
78
79 constexpr int
80 fn5 (const int *a, int b)
81 {
82 if (b != 2)
83 b = a[b];
84 return b;
85 }
86
87 constexpr int m1[4] = { 1, 2, 3, 4 };
88 constexpr int m2 = fn5 (m1, 3);
89 constexpr int m3 = fn5 (m1, 4); // { dg-error "array subscript|in .constexpr. expansion of " }
90
91 constexpr int
92 fn6 (const int &a, int b)
93 {
94 if (b != 2)
95 b = a;
96 return b;
97 }
98
99 constexpr int
100 fn7 (const int *a, int b)
101 {
102 if (b != 3)
103 return fn6 (*a, b);
104 return 7;
105 }
106
107 constexpr int n1 = 7;
108 constexpr int n2 = fn7 (&n1, 5);
109 constexpr int n3 = fn7 ((const int *) 0, 8); // { dg-error "null pointer|in .constexpr. expansion of " }
110
111 constexpr int
112 fn8 (int i)
113 {
114 constexpr int g[10] = { };
115 return g[i];
116 }
117
118 constexpr int o1 = fn8 (9);
119 constexpr int o2 = fn8 (10); // { dg-error "array subscript|in .constexpr. expansion of " }
120
121 constexpr int
122 fn9 (int a, int b)
123 {
124 if (b != 0)
125 return a + b;
126 return a;
127 }
128
129 constexpr int p1 = fn9 (42, 7);
130 constexpr int p2 = fn9 (__INT_MAX__, 1); // { dg-error "overflow in constant expression|in .constexpr. expansion of " }
131 constexpr int p3 = fn9 (__INT_MAX__, -1);
132 constexpr int p4 = fn9 (INT_MIN, 1);
133 constexpr int p5 = fn9 (INT_MIN, -1); // { dg-error "overflow in constant expression|in .constexpr. expansion of " }
134
135 SA (p1 == 49);
136 SA (p3 == __INT_MAX__ - 1);
137 SA (p4 == INT_MIN + 1);
138
139 constexpr int
140 fn10 (int a, int b)
141 {
142 if (b != 0)
143 return a * b;
144 return a;
145 }
146
147 constexpr int q1 = fn10 (10, 10);
148 constexpr int q2 = fn10 (__INT_MAX__, 2); // { dg-error "overflow in constant expression|in .constexpr. expansion of " }
149 constexpr int q3 = fn10 (INT_MIN, 2); // { dg-error "overflow in constant expression|in .constexpr. expansion of " }
150 constexpr int q4 = fn10 (-1, -1);
151
152 SA (q1 == 100);
153 SA (q4 == 1);
154
155 constexpr int
156 fn11 (double d)
157 {
158 int i = d;
159 if (i != 0)
160 return i;
161 return i * 2;
162 }
163
164 constexpr int r1 = fn11 (3.4);
165 constexpr int r2 = fn11 (__builtin_inf ()); // { dg-error "overflow in constant expression|in .constexpr. expansion of " }
166
167 constexpr int
168 fn12 (int i)
169 {
170 if (i == 42)
171 __builtin_unreachable (); // { dg-error "is not a constant expression" }
172 return i + 10;
173 }
174
175 constexpr int s1 = fn12 (1);
176 constexpr int s2 = fn12 (42); // { dg-message "in .constexpr. expansion of " }
177
178 SA (s1 == 11);