xref: /llvm-project/compiler-rt/test/ubsan/TestCases/Pointer/index-overflow.cpp (revision b076e515a0f2ddbaf43901bb36d47421ef5f5ed3)
141dfc4f1SVedant Kumar // RUN: %clangxx -fsanitize=pointer-overflow %s -o %t
2*b076e515SRoy Sundahl // RUN: %run %t  2 2>&1 | FileCheck %s --implicit-check-not="runtime error:" --check-prefix=ERR2
3*b076e515SRoy Sundahl // RUN: %run %t  1 2>&1 | FileCheck %s --implicit-check-not="runtime error:" --check-prefix=ERR1
4*b076e515SRoy Sundahl // RUN: %run %t  0 2>&1 | FileCheck %s --implicit-check-not="runtime error:" --check-prefix=SAFE
5*b076e515SRoy Sundahl // RUN: %run %t -1 2>&1 | FileCheck %s --implicit-check-not="runtime error:" --check-prefix=SAFE
6*b076e515SRoy Sundahl // RUN: %run %t -2 2>&1 | FileCheck %s --implicit-check-not="runtime error:" --check-prefix=SAFE
741dfc4f1SVedant Kumar 
841dfc4f1SVedant Kumar #include <stdio.h>
941dfc4f1SVedant Kumar #include <stdint.h>
1041dfc4f1SVedant Kumar #include <stdlib.h>
1141dfc4f1SVedant Kumar 
main(int argc,char * argv[])1241dfc4f1SVedant Kumar int main(int argc, char *argv[]) {
1341dfc4f1SVedant Kumar   // SAFE-NOT: runtime error
14536b0ee4SRoman Lebedev   // ERR2: runtime error: pointer index expression with base {{.*}} overflowed to
15536b0ee4SRoman Lebedev   // ERR2: runtime error: pointer index expression with base {{.*}} overflowed to
16536b0ee4SRoman Lebedev   // ERR1: runtime error: applying non-zero offset to non-null pointer 0x{{.*}} produced null pointer
17536b0ee4SRoman Lebedev   // ERR1: runtime error: applying non-zero offset to non-null pointer 0x{{.*}} produced null pointer
1841dfc4f1SVedant Kumar 
1941dfc4f1SVedant Kumar   char *p = (char *)(UINTPTR_MAX);
2041dfc4f1SVedant Kumar 
2141dfc4f1SVedant Kumar   printf("%p\n", p + atoi(argv[1]));
2241dfc4f1SVedant Kumar 
23536b0ee4SRoman Lebedev   char *q = (char *)(UINTPTR_MAX);
24536b0ee4SRoman Lebedev 
25536b0ee4SRoman Lebedev   printf("%p\n", p - (-atoi(argv[1])));
26536b0ee4SRoman Lebedev 
2741dfc4f1SVedant Kumar   return 0;
2841dfc4f1SVedant Kumar }
29