xref: /aosp_15_r20/external/clang/test/Sema/integer-overflow.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 %s -verify -fsyntax-only -triple x86_64-pc-linux-gnu
2*67e74705SXin Li typedef unsigned long long uint64_t;
3*67e74705SXin Li typedef unsigned int uint32_t;
4*67e74705SXin Li 
5*67e74705SXin Li // Check integer sizes.
6*67e74705SXin Li int array64[sizeof(uint64_t) == 8 ? 1 : -1];
7*67e74705SXin Li int array32[sizeof(uint32_t) == 4 ? 1 : -1];
8*67e74705SXin Li int arrayint[sizeof(int) < sizeof(uint64_t) ? 1 : -1];
9*67e74705SXin Li 
10*67e74705SXin Li uint64_t f0(uint64_t);
11*67e74705SXin Li uint64_t f1(uint64_t, uint32_t);
12*67e74705SXin Li uint64_t f2(uint64_t, ...);
13*67e74705SXin Li 
14*67e74705SXin Li static const uint64_t overflow = 1 * 4608 * 1024 * 1024; // expected-warning {{overflow in expression; result is 536870912 with type 'int'}}
15*67e74705SXin Li 
check_integer_overflows(int i)16*67e74705SXin Li uint64_t check_integer_overflows(int i) {
17*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
18*67e74705SXin Li   uint64_t overflow = 4608 * 1024 * 1024,
19*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
20*67e74705SXin Li            overflow2 = (uint64_t)(4608 * 1024 * 1024),
21*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
22*67e74705SXin Li            overflow3 = (uint64_t)(4608 * 1024 * 1024 * i),
23*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
24*67e74705SXin Li            overflow4 =  (1ULL * ((4608) * ((1024) * (1024))) + 2ULL),
25*67e74705SXin Li // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
26*67e74705SXin Li            multi_overflow = (uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024));
27*67e74705SXin Li 
28*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
29*67e74705SXin Li   overflow += overflow2 = overflow3 = (uint64_t)(4608 * 1024 * 1024);
30*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
31*67e74705SXin Li   overflow += overflow2 = overflow3 = 4608 * 1024 * 1024;
32*67e74705SXin Li 
33*67e74705SXin Li   uint64_t not_overflow = 4608 * 1024 * 1024ULL;
34*67e74705SXin Li   uint64_t not_overflow2 = (1ULL * ((uint64_t)(4608) * (1024 * 1024)) + 2ULL);
35*67e74705SXin Li 
36*67e74705SXin Li // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
37*67e74705SXin Li   overflow = 4608 * 1024 * 1024 ?  4608 * 1024 * 1024 : 0;
38*67e74705SXin Li 
39*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
40*67e74705SXin Li   overflow =  0 ? 0 : 4608 * 1024 * 1024;
41*67e74705SXin Li 
42*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
43*67e74705SXin Li   if (4608 * 1024 * 1024)
44*67e74705SXin Li     return 0;
45*67e74705SXin Li 
46*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
47*67e74705SXin Li   if ((uint64_t)(4608 * 1024 * 1024))
48*67e74705SXin Li     return 1;
49*67e74705SXin Li 
50*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
51*67e74705SXin Li   if ((uint64_t)(4608 * 1024 * 1024))
52*67e74705SXin Li     return 2;
53*67e74705SXin Li 
54*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
55*67e74705SXin Li   if ((uint64_t)(4608 * 1024 * 1024 * i))
56*67e74705SXin Li     return 3;
57*67e74705SXin Li 
58*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
59*67e74705SXin Li   if ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL))
60*67e74705SXin Li     return 4;
61*67e74705SXin Li 
62*67e74705SXin Li // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
63*67e74705SXin Li   if ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)))
64*67e74705SXin Li     return 5;
65*67e74705SXin Li 
66*67e74705SXin Li   switch (i) {
67*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
68*67e74705SXin Li   case 4608 * 1024 * 1024:
69*67e74705SXin Li     return 6;
70*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}}
71*67e74705SXin Li   case (uint64_t)(4609 * 1024 * 1024):
72*67e74705SXin Li     return 7;
73*67e74705SXin Li // expected-error@+1 {{expression is not an integer constant expression}}
74*67e74705SXin Li   case ((uint64_t)(4608 * 1024 * 1024 * i)):
75*67e74705SXin Li     return 8;
76*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
77*67e74705SXin Li   case ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)):
78*67e74705SXin Li     return 9;
79*67e74705SXin Li // expected-warning@+2 2{{overflow in expression; result is 536870912 with type 'int'}}
80*67e74705SXin Li // expected-warning@+1 {{overflow converting case value to switch condition type (288230376151711744 to 0)}}
81*67e74705SXin Li   case ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))):
82*67e74705SXin Li     return 10;
83*67e74705SXin Li   }
84*67e74705SXin Li 
85*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
86*67e74705SXin Li   while (4608 * 1024 * 1024);
87*67e74705SXin Li 
88*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
89*67e74705SXin Li   while ((uint64_t)(4608 * 1024 * 1024));
90*67e74705SXin Li 
91*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
92*67e74705SXin Li   while ((uint64_t)(4608 * 1024 * 1024));
93*67e74705SXin Li 
94*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
95*67e74705SXin Li   while ((uint64_t)(4608 * 1024 * 1024 * i));
96*67e74705SXin Li 
97*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
98*67e74705SXin Li   while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
99*67e74705SXin Li 
100*67e74705SXin Li // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
101*67e74705SXin Li   while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
102*67e74705SXin Li 
103*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
104*67e74705SXin Li   do { } while (4608 * 1024 * 1024);
105*67e74705SXin Li 
106*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
107*67e74705SXin Li   do { } while ((uint64_t)(4608 * 1024 * 1024));
108*67e74705SXin Li 
109*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
110*67e74705SXin Li   do { } while ((uint64_t)(4608 * 1024 * 1024));
111*67e74705SXin Li 
112*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
113*67e74705SXin Li   do { } while ((uint64_t)(4608 * 1024 * 1024 * i));
114*67e74705SXin Li 
115*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
116*67e74705SXin Li   do { } while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
117*67e74705SXin Li 
118*67e74705SXin Li // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
119*67e74705SXin Li   do { } while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
120*67e74705SXin Li 
121*67e74705SXin Li // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
122*67e74705SXin Li // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
123*67e74705SXin Li // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
124*67e74705SXin Li   for (uint64_t i = 4608 * 1024 * 1024;
125*67e74705SXin Li        (uint64_t)(4608 * 1024 * 1024);
126*67e74705SXin Li        i += (uint64_t)(4608 * 1024 * 1024 * i));
127*67e74705SXin Li 
128*67e74705SXin Li // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
129*67e74705SXin Li // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
130*67e74705SXin Li // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
131*67e74705SXin Li   for (uint64_t i = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL);
132*67e74705SXin Li        ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
133*67e74705SXin Li        i = ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))));
134*67e74705SXin Li 
135*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
136*67e74705SXin Li   _Complex long long x = 4608 * 1024 * 1024;
137*67e74705SXin Li 
138*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
139*67e74705SXin Li   (__real__ x) = 4608 * 1024 * 1024;
140*67e74705SXin Li 
141*67e74705SXin Li // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
142*67e74705SXin Li   (__imag__ x) = 4608 * 1024 * 1024;
143*67e74705SXin Li 
144*67e74705SXin Li // expected-warning@+4 {{overflow in expression; result is 536870912 with type 'int'}}
145*67e74705SXin Li // expected-warning@+3 {{array index 536870912 is past the end of the array (which contains 10 elements)}}
146*67e74705SXin Li // expected-note@+1 {{array 'a' declared here}}
147*67e74705SXin Li   uint64_t a[10];
148*67e74705SXin Li   a[4608 * 1024 * 1024] = 1i;
149*67e74705SXin Li 
150*67e74705SXin Li // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
151*67e74705SXin Li   return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)));
152*67e74705SXin Li }
153*67e74705SXin Li 
154*67e74705SXin Li struct s {
155*67e74705SXin Li   unsigned x;
156*67e74705SXin Li   unsigned y;
157*67e74705SXin Li } s = {
158*67e74705SXin Li   .y = 5,
159*67e74705SXin Li   .x = 4 * 1024 * 1024 * 1024  // expected-warning {{overflow in expression; result is 0 with type 'int'}}
160*67e74705SXin Li };
161*67e74705SXin Li 
162*67e74705SXin Li struct s2 {
163*67e74705SXin Li   unsigned a0;
164*67e74705SXin Li 
165*67e74705SXin Li   struct s3 {
166*67e74705SXin Li     unsigned a2;
167*67e74705SXin Li 
168*67e74705SXin Li     struct s4 {
169*67e74705SXin Li       unsigned a4;
170*67e74705SXin Li     } a3;
171*67e74705SXin Li   } a1;
172*67e74705SXin Li } s2 = {
173*67e74705SXin Li   .a0 = 4 * 1024 * 1024 * 1024, // expected-warning {{overflow in expression; result is 0 with type 'int'}}
174*67e74705SXin Li   {
175*67e74705SXin Li     .a2 = 4 * 1024 * 1024 * 1024, // expected-warning {{overflow in expression; result is 0 with type 'int'}}
176*67e74705SXin Li     {
177*67e74705SXin Li       .a4 = 4 * 1024 * 1024 * 1024 // expected-warning {{overflow in expression; result is 0 with type 'int'}}
178*67e74705SXin Li     }
179*67e74705SXin Li   }
180*67e74705SXin Li };
181