Richard Smith | 762bb9d | 2011-10-13 22:29:44 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++11 -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 |
| 26 | int *ip = &(((struct foo *)np)->f); |
| 27 | *ip = 0; // expected-warning{{Dereference of null pointer}} |
| 28 | // should be error here too, but analysis gets stopped |
| 29 | // *np = 0; |
| 30 | } |
| 31 | |
| 32 | // nullptr is implemented as a zero integer value, so should be able to compare |
| 33 | void foo4(void) { |
| 34 | char *np = nullptr; |
| 35 | if (np != 0) |
| 36 | *np = 0; // no-warning |
| 37 | char *cp = 0; |
| 38 | if (np != cp) |
| 39 | *np = 0; // no-warning |
| 40 | } |
| 41 | |
Jordy Rose | 8f08426 | 2011-07-15 20:29:02 +0000 | [diff] [blame] | 42 | |
| 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 | |