Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | // RUN: %clangxx -fsanitize=null %s -O3 -o %t |
| 2 | // RUN: %run %t l 2>&1 | FileCheck %s --check-prefix=CHECK-LOAD |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 3 | // RUN: %expect_crash %run %t s 2>&1 | FileCheck %s --check-prefix=CHECK-STORE |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 4 | // 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 | |
| 8 | struct S { |
| 9 | int f() { return 0; } |
| 10 | int k; |
| 11 | }; |
| 12 | |
| 13 | int 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 Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 21 | // CHECK-LOAD: null.cpp:[[@LINE+1]]:12: runtime error: load of null pointer of type 'int' |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 22 | return *p; |
| 23 | case 's': |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 24 | // CHECK-STORE: null.cpp:[[@LINE+1]]:5: runtime error: store to null pointer of type 'int' |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 25 | *p = 1; |
| 26 | break; |
| 27 | case 'r': |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 28 | // CHECK-REFERENCE: null.cpp:[[@LINE+1]]:15: runtime error: reference binding to null pointer of type 'int' |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 29 | {int &r = *p;} |
| 30 | break; |
| 31 | case 'm': |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 32 | // CHECK-MEMBER: null.cpp:[[@LINE+1]]:15: runtime error: member access within null pointer of type 'S' |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 33 | return s->k; |
| 34 | case 'f': |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 35 | // CHECK-MEMFUN: null.cpp:[[@LINE+1]]:12: runtime error: member call on null pointer of type 'S' |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 36 | return s->f(); |
| 37 | } |
| 38 | } |