Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 1 | // RUN: %clang_analyze_cc1 -std=c++11 -Wno-conversion-null -analyzer-checker=core,debug.ExprInspection -analyzer-store region -analyzer-output=text -verify %s |
Gabor Horvath | e86cb2e | 2015-12-04 15:02:30 +0000 | [diff] [blame] | 2 | |
| 3 | void clang_analyzer_eval(int); |
Ted Kremenek | 11e5c8b | 2011-04-22 18:01:30 +0000 | [diff] [blame] | 4 | |
| 5 | // test to see if nullptr is detected as a null pointer |
| 6 | void foo1(void) { |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 7 | char *np = nullptr; // expected-note{{'np' initialized to a null pointer value}} |
Ted Kremenek | 11e5c8b | 2011-04-22 18:01:30 +0000 | [diff] [blame] | 8 | *np = 0; // expected-warning{{Dereference of null pointer}} |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 9 | // expected-note@-1{{Dereference of null pointer}} |
Ted Kremenek | 11e5c8b | 2011-04-22 18:01:30 +0000 | [diff] [blame] | 10 | } |
| 11 | |
| 12 | // check if comparing nullptr to nullptr is detected properly |
| 13 | void foo2(void) { |
| 14 | char *np1 = nullptr; |
| 15 | char *np2 = np1; |
| 16 | char c; |
| 17 | if (np1 == np2) |
| 18 | np1 = &c; |
| 19 | *np1 = 0; // no-warning |
| 20 | } |
| 21 | |
| 22 | // invoving a nullptr in a more complex operation should be cause a warning |
| 23 | void foo3(void) { |
| 24 | struct foo { |
| 25 | int a, f; |
| 26 | }; |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 27 | char *np = nullptr; // expected-note{{'np' initialized to a null pointer value}} |
Ted Kremenek | 11e5c8b | 2011-04-22 18:01:30 +0000 | [diff] [blame] | 28 | // casting a nullptr to anything should be caught eventually |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 29 | int *ip = &(((struct foo *)np)->f); // expected-note{{'ip' initialized to a null pointer value}} |
Jordan Rose | 12024f8 | 2012-10-01 19:07:15 +0000 | [diff] [blame] | 30 | *ip = 0; // expected-warning{{Dereference of null pointer}} |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 31 | // expected-note@-1{{Dereference of null pointer}} |
Jordan Rose | 12024f8 | 2012-10-01 19:07:15 +0000 | [diff] [blame] | 32 | // should be error here too, but analysis gets stopped |
| 33 | // *np = 0; |
Ted Kremenek | 11e5c8b | 2011-04-22 18:01:30 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // nullptr is implemented as a zero integer value, so should be able to compare |
| 37 | void foo4(void) { |
| 38 | char *np = nullptr; |
| 39 | if (np != 0) |
| 40 | *np = 0; // no-warning |
| 41 | char *cp = 0; |
| 42 | if (np != cp) |
| 43 | *np = 0; // no-warning |
| 44 | } |
| 45 | |
Jordy Rose | b72bd53 | 2011-07-15 20:29:02 +0000 | [diff] [blame] | 46 | int pr10372(void *& x) { |
| 47 | // GNU null is a pointer-sized integer, not a pointer. |
| 48 | x = __null; |
| 49 | // This used to crash. |
| 50 | return __null; |
| 51 | } |
| 52 | |
Erik Verbruggen | 64aea65 | 2012-02-29 08:42:57 +0000 | [diff] [blame] | 53 | void zoo1() { |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 54 | char **p = 0; // expected-note{{'p' initialized to a null pointer value}} |
Erik Verbruggen | 64aea65 | 2012-02-29 08:42:57 +0000 | [diff] [blame] | 55 | delete *(p + 0); // expected-warning{{Dereference of null pointer}} |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 56 | // expected-note@-1{{Dereference of null pointer}} |
| 57 | } |
| 58 | |
| 59 | void zoo1backwards() { |
| 60 | char **p = 0; // expected-note{{'p' initialized to a null pointer value}} |
| 61 | delete *(0 + p); // expected-warning{{Dereference of null pointer}} |
| 62 | // expected-note@-1{{Dereference of null pointer}} |
| 63 | } |
| 64 | |
| 65 | typedef __INTPTR_TYPE__ intptr_t; |
| 66 | void zoo1multiply() { |
| 67 | char **p = 0; // FIXME-should-be-note:{{'p' initialized to a null pointer value}} |
| 68 | delete *((char **)((intptr_t)p * 2)); // expected-warning{{Dereference of null pointer}} |
| 69 | // expected-note@-1{{Dereference of null pointer}} |
Erik Verbruggen | 64aea65 | 2012-02-29 08:42:57 +0000 | [diff] [blame] | 70 | } |
Erik Verbruggen | 8c738bc | 2012-03-04 18:12:21 +0000 | [diff] [blame] | 71 | |
| 72 | void zoo2() { |
| 73 | int **a = 0; |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 74 | int **b = 0; // expected-note{{'b' initialized to a null pointer value}} |
Erik Verbruggen | 8c738bc | 2012-03-04 18:12:21 +0000 | [diff] [blame] | 75 | asm ("nop" |
Simon Atanasyan | d45982c | 2012-05-22 11:03:10 +0000 | [diff] [blame] | 76 | :"=r"(*a) |
Erik Verbruggen | 8c738bc | 2012-03-04 18:12:21 +0000 | [diff] [blame] | 77 | :"0"(*b) // expected-warning{{Dereference of null pointer}} |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 78 | // expected-note@-1{{Dereference of null pointer}} |
Erik Verbruggen | 8c738bc | 2012-03-04 18:12:21 +0000 | [diff] [blame] | 79 | ); |
| 80 | } |
Erik Verbruggen | 5923cbd | 2012-03-14 18:01:43 +0000 | [diff] [blame] | 81 | |
| 82 | int exprWithCleanups() { |
| 83 | struct S { |
| 84 | S(int a):a(a){} |
| 85 | ~S() {} |
| 86 | |
| 87 | int a; |
| 88 | }; |
| 89 | |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 90 | int *x = 0; // expected-note{{'x' initialized to a null pointer value}} |
Erik Verbruggen | 5923cbd | 2012-03-14 18:01:43 +0000 | [diff] [blame] | 91 | return S(*x).a; // expected-warning{{Dereference of null pointer}} |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 92 | // expected-note@-1{{Dereference of null pointer}} |
Erik Verbruggen | 5923cbd | 2012-03-14 18:01:43 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | int materializeTempExpr() { |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 96 | int *n = 0; // expected-note{{'n' initialized to a null pointer value}} |
Erik Verbruggen | 5923cbd | 2012-03-14 18:01:43 +0000 | [diff] [blame] | 97 | struct S { |
| 98 | int a; |
| 99 | S(int i): a(i) {} |
| 100 | }; |
| 101 | const S &s = S(*n); // expected-warning{{Dereference of null pointer}} |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 102 | // expected-note@-1{{Dereference of null pointer}} |
Erik Verbruggen | 5923cbd | 2012-03-14 18:01:43 +0000 | [diff] [blame] | 103 | return s.a; |
| 104 | } |
Anna Zaks | c30e0d7 | 2013-07-12 17:58:33 +0000 | [diff] [blame] | 105 | |
| 106 | typedef decltype(nullptr) nullptr_t; |
| 107 | void testMaterializeTemporaryExprWithNullPtr() { |
| 108 | // Create MaterializeTemporaryExpr with a nullptr inside. |
| 109 | const nullptr_t &r = nullptr; |
| 110 | } |
Gabor Horvath | e86cb2e | 2015-12-04 15:02:30 +0000 | [diff] [blame] | 111 | |
| 112 | int getSymbol(); |
| 113 | |
| 114 | struct X { |
| 115 | virtual void f() {} |
| 116 | }; |
| 117 | |
| 118 | void invokeF(X* x) { |
| 119 | x->f(); // expected-warning{{Called C++ object pointer is null}} |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 120 | // expected-note@-1{{Called C++ object pointer is null}} |
Gabor Horvath | e86cb2e | 2015-12-04 15:02:30 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | struct Type { |
| 124 | decltype(nullptr) x; |
| 125 | }; |
| 126 | |
| 127 | void shouldNotCrash() { |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 128 | decltype(nullptr) p; // expected-note{{'p' declared without an initial value}} |
| 129 | if (getSymbol()) // expected-note {{Assuming the condition is false}} |
| 130 | // expected-note@-1{{Taking false branch}} |
| 131 | // expected-note@-2{{Assuming the condition is false}} |
| 132 | // expected-note@-3{{Taking false branch}} |
| 133 | // expected-note@-4{{Assuming the condition is true}} |
| 134 | // expected-note@-5{{Taking true branch}} |
| 135 | invokeF(p); // expected-warning{{1st function call argument is an uninitialized value}} |
| 136 | // expected-note@-1{{1st function call argument is an uninitialized value}} |
| 137 | if (getSymbol()) // expected-note {{Assuming the condition is false}} |
| 138 | // expected-note@-1{{Taking false branch}} |
| 139 | // expected-note@-2{{Assuming the condition is true}} |
| 140 | // expected-note@-3{{Taking true branch}} |
| 141 | invokeF(nullptr); // expected-note {{Calling 'invokeF'}} |
| 142 | // expected-note@-1{{Passing null pointer value via 1st parameter 'x'}} |
| 143 | if (getSymbol()) { // expected-note {{Assuming the condition is true}} |
| 144 | // expected-note@-1{{Taking true branch}} |
Artem Dergachev | fcad574 | 2017-12-20 00:47:17 +0000 | [diff] [blame] | 145 | X *xx = Type().x; // expected-note {{Null pointer value stored to field 'x'}} |
| 146 | // expected-note@-1{{'xx' initialized to a null pointer value}} |
| 147 | xx->f(); // expected-warning{{Called C++ object pointer is null}} |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 148 | // expected-note@-1{{Called C++ object pointer is null}} |
Gabor Horvath | e86cb2e | 2015-12-04 15:02:30 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | void f(decltype(nullptr) p) { |
| 153 | int *q = nullptr; |
| 154 | clang_analyzer_eval(p == 0); // expected-warning{{TRUE}} |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 155 | // expected-note@-1{{TRUE}} |
Gabor Horvath | e86cb2e | 2015-12-04 15:02:30 +0000 | [diff] [blame] | 156 | clang_analyzer_eval(q == 0); // expected-warning{{TRUE}} |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 157 | // expected-note@-1{{TRUE}} |
Gabor Horvath | e86cb2e | 2015-12-04 15:02:30 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | decltype(nullptr) returnsNullPtrType(); |
| 161 | void fromReturnType() { |
| 162 | ((X *)returnsNullPtrType())->f(); // expected-warning{{Called C++ object pointer is null}} |
Artem Dergachev | 8c80061 | 2017-09-27 09:50:45 +0000 | [diff] [blame] | 163 | // expected-note@-1{{Called C++ object pointer is null}} |
Gabor Horvath | e86cb2e | 2015-12-04 15:02:30 +0000 | [diff] [blame] | 164 | } |
Anna Zaks | c9f16fe4 | 2016-01-06 00:32:49 +0000 | [diff] [blame] | 165 | |
| 166 | #define AS_ATTRIBUTE __attribute__((address_space(256))) |
| 167 | class AS1 { |
| 168 | public: |
| 169 | int x; |
| 170 | ~AS1() { |
| 171 | int AS_ATTRIBUTE *x = 0; |
| 172 | *x = 3; // no-warning |
| 173 | } |
| 174 | }; |
| 175 | void test_address_space_field_access() { |
| 176 | AS1 AS_ATTRIBUTE *pa = 0; |
| 177 | pa->x = 0; // no-warning |
| 178 | } |
| 179 | void test_address_space_bind() { |
| 180 | AS1 AS_ATTRIBUTE *pa = 0; |
| 181 | AS1 AS_ATTRIBUTE &r = *pa; |
| 182 | r.x = 0; // no-warning |
| 183 | } |