blob: 050c3f8dc56a533aeed0d54e7ec4edc0881db86a [file] [log] [blame]
David Blaikie95187bd2012-03-15 04:50:32 +00001// RUN: %clang_cc1 -std=c++11 -Wno-conversion-null -analyze -analyzer-checker=core -analyzer-store region -verify %s
Ted Kremeneke970c602011-04-22 18:01:30 +00002
3// test to see if nullptr is detected as a null pointer
4void 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
10void 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
20void 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 Rosed27a3682012-10-01 19:07:15 +000026 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;
Ted Kremeneke970c602011-04-22 18:01:30 +000030}
31
32// nullptr is implemented as a zero integer value, so should be able to compare
33void 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 Rose8f084262011-07-15 20:29:02 +000042int pr10372(void *& x) {
43 // GNU null is a pointer-sized integer, not a pointer.
44 x = __null;
45 // This used to crash.
46 return __null;
47}
48
Erik Verbruggen4fafeb62012-02-29 08:42:57 +000049void zoo1() {
50 char **p = 0;
51 delete *(p + 0); // expected-warning{{Dereference of null pointer}}
52}
Erik Verbruggena81d3d42012-03-04 18:12:21 +000053
54void zoo2() {
55 int **a = 0;
56 int **b = 0;
57 asm ("nop"
Simon Atanasyand95e95e2012-05-22 11:03:10 +000058 :"=r"(*a)
Erik Verbruggena81d3d42012-03-04 18:12:21 +000059 :"0"(*b) // expected-warning{{Dereference of null pointer}}
60 );
61}
Erik Verbruggene711d7e2012-03-14 18:01:43 +000062
63int exprWithCleanups() {
64 struct S {
65 S(int a):a(a){}
66 ~S() {}
67
68 int a;
69 };
70
71 int *x = 0;
72 return S(*x).a; // expected-warning{{Dereference of null pointer}}
73}
74
75int materializeTempExpr() {
76 int *n = 0;
77 struct S {
78 int a;
79 S(int i): a(i) {}
80 };
81 const S &s = S(*n); // expected-warning{{Dereference of null pointer}}
82 return s.a;
83}