xref: /aosp_15_r20/external/compiler-rt/test/ubsan/TestCases/Integer/usub-overflow.cpp (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx -DSUB_I32 -fsanitize=unsigned-integer-overflow %s -o %t1 && %run %t1 2>&1 | FileCheck %s --check-prefix=CHECK-SUB_I32
2*7c3d14c8STreehugger Robot // RUN: %clangxx -DSUB_I64 -fsanitize=unsigned-integer-overflow %s -o %t2 && %run %t2 2>&1 | FileCheck %s --check-prefix=CHECK-SUB_I64
3*7c3d14c8STreehugger Robot // RUN: %clangxx -DSUB_I128 -fsanitize=unsigned-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)(uint8_t(0) - uint8_t(0x7f));
11*7c3d14c8STreehugger Robot   (void)(uint16_t(0) - uint16_t(0x7fff));
12*7c3d14c8STreehugger Robot 
13*7c3d14c8STreehugger Robot #ifdef SUB_I32
14*7c3d14c8STreehugger Robot   (void)(uint32_t(1) - uint32_t(2));
15*7c3d14c8STreehugger Robot   // CHECK-SUB_I32: usub-overflow.cpp:[[@LINE-1]]:22: runtime error: unsigned integer overflow: 1 - 2 cannot be represented in type 'unsigned int'
16*7c3d14c8STreehugger Robot #endif
17*7c3d14c8STreehugger Robot 
18*7c3d14c8STreehugger Robot #ifdef SUB_I64
19*7c3d14c8STreehugger Robot   (void)(uint64_t(8000000000000000000ll) - uint64_t(9000000000000000000ll));
20*7c3d14c8STreehugger Robot   // CHECK-SUB_I64: 8000000000000000000 - 9000000000000000000 cannot be represented in type 'unsigned {{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)((__uint128_t(1) << 126) - (__uint128_t(1) << 127));
26*7c3d14c8STreehugger Robot # else
27*7c3d14c8STreehugger Robot   puts("__int128 not supported\n");
28*7c3d14c8STreehugger Robot # endif
29*7c3d14c8STreehugger Robot   // CHECK-SUB_I128: {{0x40000000000000000000000000000000 - 0x80000000000000000000000000000000 cannot be represented in type 'unsigned __int128'|__int128 not supported}}
30*7c3d14c8STreehugger Robot #endif
31*7c3d14c8STreehugger Robot }
32