blob: eeeebdb98c74123cec76752a3f1a1f56a12aca58 [file] [log] [blame]
Dominic Chen184c6242017-03-03 18:02:02 +00001// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-output=text -analyzer-config suppress-null-return-paths=false -verify %s
George Karpenkov39165092018-06-12 19:07:41 +00002// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-output=plist-multi-file -analyzer-config suppress-null-return-paths=false %s -o %t.plist
Mikhail Maltsevc704f4d2018-09-17 10:19:46 +00003// RUN: cat %t.plist | %diff_plist %S/Inputs/expected-plists/path-notes.c.plist
Jordan Rose3eb3cd42012-08-03 23:08:54 +00004
5void zero(int **p) {
6 *p = 0;
7 // expected-note@-1 {{Null pointer value stored to 'a'}}
8}
9
10void testZero(int *a) {
11 zero(&a);
12 // expected-note@-1 {{Calling 'zero'}}
13 // expected-note@-2 {{Returning from 'zero'}}
14 *a = 1; // expected-warning{{Dereference of null pointer}}
15 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'a')}}
Jordan Rosecfb4eb22012-08-03 23:09:01 +000016}
17
Jordan Rosecfb4eb22012-08-03 23:09:01 +000018void testCheck(int *a) {
Anna Zaks8d7c8a42013-03-02 03:20:52 +000019 if (a) {
20 // expected-note@-1 + {{Assuming 'a' is null}}
21 // expected-note@-2 + {{Taking false branch}}
22 ;
23 }
Jordan Rosecfb4eb22012-08-03 23:09:01 +000024 *a = 1; // expected-warning{{Dereference of null pointer}}
25 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'a')}}
26}
27
28
29int *getPointer();
30
31void testInitCheck() {
32 int *a = getPointer();
Ted Kremenek37c777e2013-02-26 19:44:38 +000033 // expected-note@-1 {{'a' initialized here}}
Anna Zaks8d7c8a42013-03-02 03:20:52 +000034 if (a) {
35 // expected-note@-1 + {{Assuming 'a' is null}}
36 // expected-note@-2 + {{Taking false branch}}
37 ;
38 }
Jordan Rosecfb4eb22012-08-03 23:09:01 +000039 *a = 1; // expected-warning{{Dereference of null pointer}}
40 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'a')}}
41}
42
43void testStoreCheck(int *a) {
44 a = getPointer();
45 // expected-note@-1 {{Value assigned to 'a'}}
Anna Zaks8d7c8a42013-03-02 03:20:52 +000046 if (a) {
47 // expected-note@-1 + {{Assuming 'a' is null}}
48 // expected-note@-2 + {{Taking false branch}}
49 ;
50 }
Jordan Rosecfb4eb22012-08-03 23:09:01 +000051 *a = 1; // expected-warning{{Dereference of null pointer}}
52 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'a')}}
53}
Jordan Rosea3e33432012-08-06 21:28:14 +000054
55
Jordan Rose51c27162012-08-24 16:34:31 +000056int *getZero() {
57 int *p = 0;
Ted Kremenek37c777e2013-02-26 19:44:38 +000058 // expected-note@-1 + {{'p' initialized to a null pointer value}}
Jordan Rose51c27162012-08-24 16:34:31 +000059 // ^ This note checks that we add a second visitor for the return value.
60 return p;
Jordan Rose1a616742012-08-27 20:18:30 +000061 // expected-note@-1 + {{Returning null pointer (loaded from 'p')}}
Jordan Rose51c27162012-08-24 16:34:31 +000062}
63
64void testReturnZero() {
65 *getZero() = 1; // expected-warning{{Dereference of null pointer}}
66 // expected-note@-1 {{Calling 'getZero'}}
67 // expected-note@-2 {{Returning from 'getZero'}}
68 // expected-note@-3 {{Dereference of null pointer}}
69}
70
Jordan Rose1a616742012-08-27 20:18:30 +000071int testReturnZero2() {
72 return *getZero(); // expected-warning{{Dereference of null pointer}}
73 // expected-note@-1 {{Calling 'getZero'}}
74 // expected-note@-2 {{Returning from 'getZero'}}
75 // expected-note@-3 {{Dereference of null pointer}}
76}
77
Jordan Rose51c27162012-08-24 16:34:31 +000078void testInitZero() {
Jordan Rose51c27162012-08-24 16:34:31 +000079 int *a = getZero();
Jordan Rose50909042012-08-28 00:50:42 +000080 // expected-note@-1 {{Calling 'getZero'}}
81 // expected-note@-2 {{Returning from 'getZero'}}
Ted Kremenek37c777e2013-02-26 19:44:38 +000082 // expected-note@-3 {{'a' initialized to a null pointer value}}
Jordan Rose51c27162012-08-24 16:34:31 +000083 *a = 1; // expected-warning{{Dereference of null pointer}}
84 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'a')}}
85}
86
87void testStoreZero(int *a) {
Jordan Rose51c27162012-08-24 16:34:31 +000088 a = getZero();
Jordan Rose50909042012-08-28 00:50:42 +000089 // expected-note@-1 {{Calling 'getZero'}}
90 // expected-note@-2 {{Returning from 'getZero'}}
91 // expected-note@-3 {{Null pointer value stored to 'a'}}
Jordan Rose51c27162012-08-24 16:34:31 +000092 *a = 1; // expected-warning{{Dereference of null pointer}}
93 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'a')}}
94}
95
Jordan Rose4ac7cba2012-09-22 01:25:00 +000096void usePointer(int *p) {
97 *p = 1; // expected-warning{{Dereference of null pointer}}
98 // expected-note@-1 {{Dereference of null pointer}}
99}
100
101void testUseOfNullPointer() {
102 // Test the case where an argument expression is itself a call.
103 usePointer(getZero());
104 // expected-note@-1 {{Calling 'getZero'}}
105 // expected-note@-2 {{Returning from 'getZero'}}
106 // expected-note@-3 {{Passing null pointer value via 1st parameter 'p'}}
107 // expected-note@-4 {{Calling 'usePointer'}}
108}
109
Anna Zaks6cea7d92013-04-12 18:40:21 +0000110struct X { char *p; };
111
112void setFieldToNull(struct X *x) {
113 x->p = 0; // expected-note {{Null pointer value stored to field 'p'}}
114}
115
116int testSetFieldToNull(struct X *x) {
117 setFieldToNull(x); // expected-note {{Calling 'setFieldToNull'}}
118 // expected-note@-1{{Returning from 'setFieldToNull'}}
119 return *x->p;
120 // expected-warning@-1 {{Dereference of null pointer (loaded from field 'p')}}
121 // expected-note@-2 {{Dereference of null pointer (loaded from field 'p')}}
122}
123
Anna Zakse2e8ea62013-04-15 22:37:59 +0000124struct Outer {
125 struct Inner {
126 int *p;
127 } inner;
128};
129
130void test(struct Outer *wrapperPtr) {
131 wrapperPtr->inner.p = 0; // expected-note {{Null pointer value stored to field 'p'}}
132 *wrapperPtr->inner.p = 1; //expected-warning {{Dereference of null pointer (loaded from field 'p')}}
133 // expected-note@-1 {{Dereference of null pointer (loaded from field 'p')}}
134}
135
Anna Zaks4f598352013-04-17 22:29:51 +0000136void test4(int **p) {
137 if (*p) return; // expected-note {{Taking false branch}}
138 // expected-note@-1 {{Assuming pointer value is null}}
139 **p = 1; // expected-warning {{Dereference of null pointer}}
140 // expected-note@-1 {{Dereference of null pointer}}
141}
142
Artem Dergachevc8b1d5f2018-04-03 18:52:30 +0000143void boringCallee() {
144}
145
146void interestingCallee(int *x) {
147 *x = 0; // expected-note{{The value 0 is assigned to 'x'}}
148 boringCallee(); // no-note
149}
150
151int testBoringCalleeOfInterestingCallee() {
152 int x;
153 interestingCallee(&x); // expected-note{{Calling 'interestingCallee'}}
154 // expected-note@-1{{Returning from 'interestingCallee'}}
155 return 1 / x; // expected-warning{{Division by zero}}
156 // expected-note@-1{{Division by zero}}
157}
158