xref: /aosp_15_r20/external/compiler-rt/test/ubsan/TestCases/Integer/sub-overflow.cpp (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx -DSUB_I32 -fsanitize=signed-integer-overflow %s -o %t1 && %run %t1 2>&1 | FileCheck %s --check-prefix=CHECK-SUB_I32
2*7c3d14c8STreehugger Robot // RUN: %clangxx -DSUB_I64 -fsanitize=signed-integer-overflow %s -o %t2 && %run %t2 2>&1 | FileCheck %s --check-prefix=CHECK-SUB_I64
3*7c3d14c8STreehugger Robot // RUN: %clangxx -DSUB_I128 -fsanitize=signed-integer-overflow %s -o %t3 && %run %t3 2>&1 | FileCheck %s --check-prefix=CHECK-SUB_I128
4*7c3d14c8STreehugger Robot 
5*7c3d14c8STreehugger Robot #include <stdint.h>
6*7c3d14c8STreehugger Robot #include <stdio.h>
7*7c3d14c8STreehugger Robot 
main()8*7c3d14c8STreehugger Robot int main() {
9*7c3d14c8STreehugger Robot   // These promote to 'int'.
10*7c3d14c8STreehugger Robot   (void)(int8_t(-2) - int8_t(0x7f));
11*7c3d14c8STreehugger Robot   (void)(int16_t(-2) - int16_t(0x7fff));
12*7c3d14c8STreehugger Robot 
13*7c3d14c8STreehugger Robot #ifdef SUB_I32
14*7c3d14c8STreehugger Robot   (void)(int32_t(-2) - int32_t(0x7fffffff));
15*7c3d14c8STreehugger Robot   // CHECK-SUB_I32: sub-overflow.cpp:[[@LINE-1]]:22: runtime error: signed integer overflow: -2 - 2147483647 cannot be represented in type 'int'
16*7c3d14c8STreehugger Robot #endif
17*7c3d14c8STreehugger Robot 
18*7c3d14c8STreehugger Robot #ifdef SUB_I64
19*7c3d14c8STreehugger Robot   (void)(int64_t(-8000000000000000000ll) - int64_t(2000000000000000000ll));
20*7c3d14c8STreehugger Robot   // CHECK-SUB_I64: -8000000000000000000 - 2000000000000000000 cannot be represented in type '{{long( long)?}}'
21*7c3d14c8STreehugger Robot #endif
22*7c3d14c8STreehugger Robot 
23*7c3d14c8STreehugger Robot #ifdef SUB_I128
24*7c3d14c8STreehugger Robot # if defined(__SIZEOF_INT128__) && !defined(_WIN32)
25*7c3d14c8STreehugger Robot   (void)(-(__int128_t(1) << 126) - (__int128_t(1) << 126) - 1);
26*7c3d14c8STreehugger Robot # else
27*7c3d14c8STreehugger Robot   puts("__int128 not supported");
28*7c3d14c8STreehugger Robot # endif
29*7c3d14c8STreehugger Robot   // CHECK-SUB_I128: {{0x80000000000000000000000000000000 - 1 cannot be represented in type '__int128'|__int128 not supported}}
30*7c3d14c8STreehugger Robot #endif
31*7c3d14c8STreehugger Robot }
32