integer-overflow.c revision 0e2c34f92f00628d48968dfea096d36381f494cb
10e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// RUN: %clang_cc1 %s -verify -fsyntax-only
20e2c34f92f00628d48968dfea096d36381f494cbStephen Hinestypedef unsigned long long uint64_t;
30e2c34f92f00628d48968dfea096d36381f494cbStephen Hinestypedef unsigned long long uint32_t;
40e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
50e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesuint64_t f0(uint64_t);
60e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesuint64_t f1(uint64_t, uint32_t);
70e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesuint64_t f2(uint64_t, ...);
80e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
90e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesstatic const uint64_t overflow = 1 * 4608 * 1024 * 1024; // expected-warning {{overflow in expression; result is 536870912 with type 'int'}}
100e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
110e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesuint64_t check_integer_overflows(int i) {
120e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
130e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  uint64_t overflow = 4608 * 1024 * 1024,
140e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
150e2c34f92f00628d48968dfea096d36381f494cbStephen Hines           overflow2 = (uint64_t)(4608 * 1024 * 1024),
160e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
170e2c34f92f00628d48968dfea096d36381f494cbStephen Hines           overflow3 = (uint64_t)(4608 * 1024 * 1024 * i),
180e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
190e2c34f92f00628d48968dfea096d36381f494cbStephen Hines           overflow4 =  (1ULL * ((4608) * ((1024) * (1024))) + 2ULL),
200e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
210e2c34f92f00628d48968dfea096d36381f494cbStephen Hines           multi_overflow = (uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024));
220e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
230e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
240e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  overflow += overflow2 = overflow3 = (uint64_t)(4608 * 1024 * 1024);
250e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
260e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  overflow += overflow2 = overflow3 = 4608 * 1024 * 1024;
270e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
280e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  uint64_t not_overflow = 4608 * 1024 * 1024ULL;
290e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  uint64_t not_overflow2 = (1ULL * ((uint64_t)(4608) * (1024 * 1024)) + 2ULL);
300e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
310e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
320e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  overflow = 4608 * 1024 * 1024 ?  4608 * 1024 * 1024 : 0;
330e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
340e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
350e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  overflow =  0 ? 0 : 4608 * 1024 * 1024;
360e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
370e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
380e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if (4608 * 1024 * 1024)
390e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return 0;
400e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
410e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
420e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if ((uint64_t)(4608 * 1024 * 1024))
430e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return 1;
440e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
450e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
460e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if ((uint64_t)(4608 * 1024 * 1024))
470e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return 2;
480e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
490e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
500e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if ((uint64_t)(4608 * 1024 * 1024 * i))
510e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return 3;
520e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
530e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
540e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL))
550e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return 4;
560e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
570e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
580e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  if ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)))
590e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return 5;
600e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
610e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  switch (i) {
620e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
630e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case 4608 * 1024 * 1024:
640e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return 6;
650e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}}
660e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case (uint64_t)(4609 * 1024 * 1024):
670e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return 7;
680e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-error@+1 {{expression is not an integer constant expression}}
690e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case ((uint64_t)(4608 * 1024 * 1024 * i)):
700e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return 8;
710e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
720e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)):
730e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return 9;
740e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+2 2{{overflow in expression; result is 536870912 with type 'int'}}
750e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow converting case value to switch condition type (288230376151711744 to 0)}}
760e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  case ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))):
770e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return 10;
780e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
790e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
800e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
810e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  while (4608 * 1024 * 1024);
820e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
830e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
840e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  while ((uint64_t)(4608 * 1024 * 1024));
850e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
860e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
870e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  while ((uint64_t)(4608 * 1024 * 1024));
880e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
890e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
900e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  while ((uint64_t)(4608 * 1024 * 1024 * i));
910e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
920e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
930e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
940e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
950e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
960e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
970e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
980e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
990e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  do { } while (4608 * 1024 * 1024);
1000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1010e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
1020e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  do { } while ((uint64_t)(4608 * 1024 * 1024));
1030e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1040e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
1050e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  do { } while ((uint64_t)(4608 * 1024 * 1024));
1060e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1070e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
1080e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  do { } while ((uint64_t)(4608 * 1024 * 1024 * i));
1090e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1100e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
1110e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  do { } while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
1120e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1130e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
1140e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  do { } while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
1150e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1160e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
1170e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
1180e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
1190e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  for (uint64_t i = 4608 * 1024 * 1024;
1200e2c34f92f00628d48968dfea096d36381f494cbStephen Hines       (uint64_t)(4608 * 1024 * 1024);
1210e2c34f92f00628d48968dfea096d36381f494cbStephen Hines       i += (uint64_t)(4608 * 1024 * 1024 * i));
1220e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1230e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
1240e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
1250e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
1260e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  for (uint64_t i = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL);
1270e2c34f92f00628d48968dfea096d36381f494cbStephen Hines       ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
1280e2c34f92f00628d48968dfea096d36381f494cbStephen Hines       i = ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))));
1290e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1300e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
1310e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  _Complex long long x = 4608 * 1024 * 1024;
1320e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1330e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
1340e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  (__real__ x) = 4608 * 1024 * 1024;
1350e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1360e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
1370e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  (__imag__ x) = 4608 * 1024 * 1024;
1380e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1390e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+4 {{overflow in expression; result is 536870912 with type 'int'}}
1400e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+3 {{array index 536870912 is past the end of the array (which contains 10 elements)}}
1410e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-note@+1 {{array 'a' declared here}}
1420e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  uint64_t a[10];
1430e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  a[4608 * 1024 * 1024] = 1i;
1440e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
1450e2c34f92f00628d48968dfea096d36381f494cbStephen Hines// expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
1460e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)));
1470e2c34f92f00628d48968dfea096d36381f494cbStephen Hines}
148