David Blaikie | 95187bd | 2012-03-15 04:50:32 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++11 -Wno-conversion-null -analyze -analyzer-checker=core -analyzer-store region -verify %s |
Ted Kremenek | e970c60 | 2011-04-22 18:01:30 +0000 | [diff] [blame] | 2 | |
| 3 | // test to see if nullptr is detected as a null pointer |
| 4 | void foo1(void) { |
| 5 | char *np = nullptr; |
| 6 | *np = 0; // expected-warning{{Dereference of null pointer}} |
| 7 | } |
| 8 | |
| 9 | // check if comparing nullptr to nullptr is detected properly |
| 10 | void foo2(void) { |
| 11 | char *np1 = nullptr; |
| 12 | char *np2 = np1; |
| 13 | char c; |
| 14 | if (np1 == np2) |
| 15 | np1 = &c; |
| 16 | *np1 = 0; // no-warning |
| 17 | } |
| 18 | |
| 19 | // invoving a nullptr in a more complex operation should be cause a warning |
| 20 | void foo3(void) { |
| 21 | struct foo { |
| 22 | int a, f; |
| 23 | }; |
| 24 | char *np = nullptr; |
| 25 | // casting a nullptr to anything should be caught eventually |
Jordan Rose | dd1d7d8 | 2012-09-22 01:24:33 +0000 | [diff] [blame] | 26 | int *ip = &(((struct foo *)np)->f); // expected-warning{{Access to field 'f' results in a dereference of a null pointer (loaded from variable 'np')}} |
| 27 | |
| 28 | // Analysis stops at the first problem case, so we won't actually warn here. |
| 29 | *ip = 0; |
| 30 | *np = 0; |
Ted Kremenek | e970c60 | 2011-04-22 18:01:30 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // nullptr is implemented as a zero integer value, so should be able to compare |
| 34 | void foo4(void) { |
| 35 | char *np = nullptr; |
| 36 | if (np != 0) |
| 37 | *np = 0; // no-warning |
| 38 | char *cp = 0; |
| 39 | if (np != cp) |
| 40 | *np = 0; // no-warning |
| 41 | } |
| 42 | |
Jordy Rose | 8f08426 | 2011-07-15 20:29:02 +0000 | [diff] [blame] | 43 | int pr10372(void *& x) { |
| 44 | // GNU null is a pointer-sized integer, not a pointer. |
| 45 | x = __null; |
| 46 | // This used to crash. |
| 47 | return __null; |
| 48 | } |
| 49 | |
Erik Verbruggen | 4fafeb6 | 2012-02-29 08:42:57 +0000 | [diff] [blame] | 50 | void zoo1() { |
| 51 | char **p = 0; |
| 52 | delete *(p + 0); // expected-warning{{Dereference of null pointer}} |
| 53 | } |
Erik Verbruggen | a81d3d4 | 2012-03-04 18:12:21 +0000 | [diff] [blame] | 54 | |
| 55 | void zoo2() { |
| 56 | int **a = 0; |
| 57 | int **b = 0; |
| 58 | asm ("nop" |
Simon Atanasyan | d95e95e | 2012-05-22 11:03:10 +0000 | [diff] [blame] | 59 | :"=r"(*a) |
Erik Verbruggen | a81d3d4 | 2012-03-04 18:12:21 +0000 | [diff] [blame] | 60 | :"0"(*b) // expected-warning{{Dereference of null pointer}} |
| 61 | ); |
| 62 | } |
Erik Verbruggen | e711d7e | 2012-03-14 18:01:43 +0000 | [diff] [blame] | 63 | |
| 64 | int exprWithCleanups() { |
| 65 | struct S { |
| 66 | S(int a):a(a){} |
| 67 | ~S() {} |
| 68 | |
| 69 | int a; |
| 70 | }; |
| 71 | |
| 72 | int *x = 0; |
| 73 | return S(*x).a; // expected-warning{{Dereference of null pointer}} |
| 74 | } |
| 75 | |
| 76 | int materializeTempExpr() { |
| 77 | int *n = 0; |
| 78 | struct S { |
| 79 | int a; |
| 80 | S(int i): a(i) {} |
| 81 | }; |
| 82 | const S &s = S(*n); // expected-warning{{Dereference of null pointer}} |
| 83 | return s.a; |
| 84 | } |