blob: 1e179559d5df49be3c6ec4fee39c037b1a72b423 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001// RUN: %clangxx -fsanitize=null %s -O3 -o %t
2// RUN: %run %t l 2>&1 | FileCheck %s --check-prefix=CHECK-LOAD
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08003// RUN: %expect_crash %run %t s 2>&1 | FileCheck %s --check-prefix=CHECK-STORE
Stephen Hines2d1fdb22014-05-28 23:58:16 -07004// RUN: %run %t r 2>&1 | FileCheck %s --check-prefix=CHECK-REFERENCE
5// RUN: %run %t m 2>&1 | FileCheck %s --check-prefix=CHECK-MEMBER
6// RUN: %run %t f 2>&1 | FileCheck %s --check-prefix=CHECK-MEMFUN
7
8struct S {
9 int f() { return 0; }
10 int k;
11};
12
13int main(int, char **argv) {
14 int *p = 0;
15 S *s = 0;
16
17 (void)*p; // ok!
18
19 switch (argv[1][0]) {
20 case 'l':
Stephen Hines86277eb2015-03-23 12:06:32 -070021 // CHECK-LOAD: null.cpp:[[@LINE+1]]:12: runtime error: load of null pointer of type 'int'
Stephen Hines2d1fdb22014-05-28 23:58:16 -070022 return *p;
23 case 's':
Stephen Hines86277eb2015-03-23 12:06:32 -070024 // CHECK-STORE: null.cpp:[[@LINE+1]]:5: runtime error: store to null pointer of type 'int'
Stephen Hines2d1fdb22014-05-28 23:58:16 -070025 *p = 1;
26 break;
27 case 'r':
Stephen Hines86277eb2015-03-23 12:06:32 -070028 // CHECK-REFERENCE: null.cpp:[[@LINE+1]]:15: runtime error: reference binding to null pointer of type 'int'
Stephen Hines2d1fdb22014-05-28 23:58:16 -070029 {int &r = *p;}
30 break;
31 case 'm':
Stephen Hines86277eb2015-03-23 12:06:32 -070032 // CHECK-MEMBER: null.cpp:[[@LINE+1]]:15: runtime error: member access within null pointer of type 'S'
Stephen Hines2d1fdb22014-05-28 23:58:16 -070033 return s->k;
34 case 'f':
Stephen Hines86277eb2015-03-23 12:06:32 -070035 // CHECK-MEMFUN: null.cpp:[[@LINE+1]]:12: runtime error: member call on null pointer of type 'S'
Stephen Hines2d1fdb22014-05-28 23:58:16 -070036 return s->f();
37 }
38}