xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/null-deref-ps.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,deadcode,alpha.core -std=gnu99 -analyzer-store=region -analyzer-constraints=range -analyzer-purge=none -verify %s -Wno-error=return-type
2*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,deadcode,alpha.core -std=gnu99 -analyzer-store=region -analyzer-constraints=range -verify %s -Wno-error=return-type
3f4a2713aSLionel Sambuc 
4f4a2713aSLionel Sambuc typedef unsigned uintptr_t;
5f4a2713aSLionel Sambuc 
6f4a2713aSLionel Sambuc extern void __assert_fail (__const char *__assertion, __const char *__file,
7f4a2713aSLionel Sambuc     unsigned int __line, __const char *__function)
8f4a2713aSLionel Sambuc      __attribute__ ((__noreturn__));
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #define assert(expr) \
11f4a2713aSLionel Sambuc   ((expr)  ? (void)(0)  : __assert_fail (#expr, __FILE__, __LINE__, __func__))
12f4a2713aSLionel Sambuc 
f1(int * p)13f4a2713aSLionel Sambuc void f1(int *p) {
14f4a2713aSLionel Sambuc   if (p) *p = 1;
15f4a2713aSLionel Sambuc   else *p = 0; // expected-warning{{ereference}}
16f4a2713aSLionel Sambuc }
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc struct foo_struct {
19f4a2713aSLionel Sambuc   int x;
20f4a2713aSLionel Sambuc };
21f4a2713aSLionel Sambuc 
f2(struct foo_struct * p)22f4a2713aSLionel Sambuc int f2(struct foo_struct* p) {
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc   if (p)
25f4a2713aSLionel Sambuc     p->x = 1;
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc   return p->x++; // expected-warning{{Access to field 'x' results in a dereference of a null pointer (loaded from variable 'p')}}
28f4a2713aSLionel Sambuc }
29f4a2713aSLionel Sambuc 
f3(char * x)30f4a2713aSLionel Sambuc int f3(char* x) {
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc   int i = 2;
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc   if (x)
35f4a2713aSLionel Sambuc     return x[i - 1];
36f4a2713aSLionel Sambuc 
37f4a2713aSLionel Sambuc   return x[i+1]; // expected-warning{{Array access (from variable 'x') results in a null pointer dereference}}
38f4a2713aSLionel Sambuc }
39f4a2713aSLionel Sambuc 
f3_b(char * x)40f4a2713aSLionel Sambuc int f3_b(char* x) {
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc   int i = 2;
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc   if (x)
45f4a2713aSLionel Sambuc     return x[i - 1];
46f4a2713aSLionel Sambuc 
47f4a2713aSLionel Sambuc   return x[i+1]++; // expected-warning{{Array access (from variable 'x') results in a null pointer dereference}}
48f4a2713aSLionel Sambuc }
49f4a2713aSLionel Sambuc 
f4(int * p)50f4a2713aSLionel Sambuc int f4(int *p) {
51f4a2713aSLionel Sambuc 
52f4a2713aSLionel Sambuc   uintptr_t x = (uintptr_t) p;
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc   if (x)
55f4a2713aSLionel Sambuc     return 1;
56f4a2713aSLionel Sambuc 
57f4a2713aSLionel Sambuc   int *q = (int*) x;
58f4a2713aSLionel Sambuc   return *q; // expected-warning{{Dereference of null pointer (loaded from variable 'q')}}
59f4a2713aSLionel Sambuc }
60f4a2713aSLionel Sambuc 
f4_b()61f4a2713aSLionel Sambuc int f4_b() {
62f4a2713aSLionel Sambuc   short array[2];
63f4a2713aSLionel Sambuc   uintptr_t x = array; // expected-warning{{incompatible pointer to integer conversion}}
64f4a2713aSLionel Sambuc   short *p = x; // expected-warning{{incompatible integer to pointer conversion}}
65f4a2713aSLionel Sambuc 
66f4a2713aSLionel Sambuc   // The following branch should be infeasible.
67*0a6a1f1dSLionel Sambuc   if (!(p == &array[0])) {
68f4a2713aSLionel Sambuc     p = 0;
69f4a2713aSLionel Sambuc     *p = 1; // no-warning
70f4a2713aSLionel Sambuc   }
71f4a2713aSLionel Sambuc 
72f4a2713aSLionel Sambuc   if (p) {
73f4a2713aSLionel Sambuc     *p = 5; // no-warning
74f4a2713aSLionel Sambuc     p = 0;
75f4a2713aSLionel Sambuc   }
76f4a2713aSLionel Sambuc   else return; // expected-warning {{non-void function 'f4_b' should return a value}}
77f4a2713aSLionel Sambuc 
78f4a2713aSLionel Sambuc   *p += 10; // expected-warning{{Dereference of null pointer}}
79f4a2713aSLionel Sambuc   return 0;
80f4a2713aSLionel Sambuc }
81f4a2713aSLionel Sambuc 
f5()82f4a2713aSLionel Sambuc int f5() {
83f4a2713aSLionel Sambuc 
84f4a2713aSLionel Sambuc   char *s = "hello world";
85f4a2713aSLionel Sambuc   return s[0]; // no-warning
86f4a2713aSLionel Sambuc }
87f4a2713aSLionel Sambuc 
88f4a2713aSLionel Sambuc int bar(int* p, int q) __attribute__((nonnull));
89f4a2713aSLionel Sambuc 
f6(int * p)90f4a2713aSLionel Sambuc int f6(int *p) {
91f4a2713aSLionel Sambuc   return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
92f4a2713aSLionel Sambuc          : bar(p, 0);   // no-warning
93f4a2713aSLionel Sambuc }
94f4a2713aSLionel Sambuc 
95f4a2713aSLionel Sambuc int bar2(int* p, int q) __attribute__((nonnull(1)));
96f4a2713aSLionel Sambuc 
f6b(int * p)97f4a2713aSLionel Sambuc int f6b(int *p) {
98f4a2713aSLionel Sambuc   return !p ? bar2(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
99f4a2713aSLionel Sambuc          : bar2(p, 0);   // no-warning
100f4a2713aSLionel Sambuc }
101f4a2713aSLionel Sambuc 
102f4a2713aSLionel Sambuc int bar3(int*p, int q, int *r) __attribute__((nonnull(1,3)));
103f4a2713aSLionel Sambuc 
f6c(int * p,int * q)104f4a2713aSLionel Sambuc int f6c(int *p, int *q) {
105f4a2713aSLionel Sambuc    return !p ? bar3(q, 2, p) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
106f4a2713aSLionel Sambuc              : bar3(p, 2, q); // no-warning
107f4a2713aSLionel Sambuc }
108f4a2713aSLionel Sambuc 
f6d(int * p)109f4a2713aSLionel Sambuc void f6d(int *p) {
110f4a2713aSLionel Sambuc   bar(p, 0);
111f4a2713aSLionel Sambuc   // At this point, 'p' cannot be null.
112f4a2713aSLionel Sambuc   if (!p) {
113f4a2713aSLionel Sambuc     int *q = 0;
114f4a2713aSLionel Sambuc     *q = 0xDEADBEEF; // no-warning
115f4a2713aSLionel Sambuc   }
116f4a2713aSLionel Sambuc }
117f4a2713aSLionel Sambuc 
f6e(int * p,int offset)118f4a2713aSLionel Sambuc void f6e(int *p, int offset) {
119f4a2713aSLionel Sambuc   // PR7406 - crash from treating an UnknownVal as defined, to see if it's 0.
120f4a2713aSLionel Sambuc   bar((p+offset)+1, 0); // not crash
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc 
123f4a2713aSLionel Sambuc int* qux();
124f4a2713aSLionel Sambuc 
f7(int x)125f4a2713aSLionel Sambuc int f7(int x) {
126f4a2713aSLionel Sambuc 
127f4a2713aSLionel Sambuc   int* p = 0;
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc   if (0 == x)
130f4a2713aSLionel Sambuc     p = qux();
131f4a2713aSLionel Sambuc 
132f4a2713aSLionel Sambuc   if (0 == x)
133f4a2713aSLionel Sambuc     *p = 1; // no-warning
134f4a2713aSLionel Sambuc 
135f4a2713aSLionel Sambuc   return x;
136f4a2713aSLionel Sambuc }
137f4a2713aSLionel Sambuc 
f7b(int * x)138f4a2713aSLionel Sambuc int* f7b(int *x) {
139f4a2713aSLionel Sambuc 
140f4a2713aSLionel Sambuc   int* p = 0;
141f4a2713aSLionel Sambuc 
142f4a2713aSLionel Sambuc   if (((void*)0) == x)
143f4a2713aSLionel Sambuc     p = qux();
144f4a2713aSLionel Sambuc 
145f4a2713aSLionel Sambuc   if (((void*)0) == x)
146f4a2713aSLionel Sambuc     *p = 1; // no-warning
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc   return x;
149f4a2713aSLionel Sambuc }
150f4a2713aSLionel Sambuc 
f7c(int * x)151f4a2713aSLionel Sambuc int* f7c(int *x) {
152f4a2713aSLionel Sambuc 
153f4a2713aSLionel Sambuc   int* p = 0;
154f4a2713aSLionel Sambuc 
155f4a2713aSLionel Sambuc   if (((void*)0) == x)
156f4a2713aSLionel Sambuc     p = qux();
157f4a2713aSLionel Sambuc 
158f4a2713aSLionel Sambuc   if (((void*)0) != x)
159f4a2713aSLionel Sambuc     return x;
160f4a2713aSLionel Sambuc 
161f4a2713aSLionel Sambuc   // If we reach here then 'p' is not null.
162f4a2713aSLionel Sambuc   *p = 1; // no-warning
163f4a2713aSLionel Sambuc   return x;
164f4a2713aSLionel Sambuc }
165f4a2713aSLionel Sambuc 
f7c2(int * x)166f4a2713aSLionel Sambuc int* f7c2(int *x) {
167f4a2713aSLionel Sambuc 
168f4a2713aSLionel Sambuc   int* p = 0;
169f4a2713aSLionel Sambuc 
170f4a2713aSLionel Sambuc   if (((void*)0) == x)
171f4a2713aSLionel Sambuc     p = qux();
172f4a2713aSLionel Sambuc 
173f4a2713aSLionel Sambuc   if (((void*)0) == x)
174f4a2713aSLionel Sambuc     return x;
175f4a2713aSLionel Sambuc 
176f4a2713aSLionel Sambuc   *p = 1; // expected-warning{{null}}
177f4a2713aSLionel Sambuc   return x;
178f4a2713aSLionel Sambuc }
179f4a2713aSLionel Sambuc 
180f4a2713aSLionel Sambuc 
f8(int * p,int * q)181f4a2713aSLionel Sambuc void f8(int *p, int *q) {
182f4a2713aSLionel Sambuc   if (!p)
183f4a2713aSLionel Sambuc     if (p)
184f4a2713aSLionel Sambuc       *p = 1; // no-warning
185f4a2713aSLionel Sambuc 
186f4a2713aSLionel Sambuc   if (q)
187f4a2713aSLionel Sambuc     if (!q)
188f4a2713aSLionel Sambuc       *q = 1; // no-warning
189f4a2713aSLionel Sambuc }
190f4a2713aSLionel Sambuc 
191f4a2713aSLionel Sambuc int* qux();
192f4a2713aSLionel Sambuc 
f9(unsigned len)193f4a2713aSLionel Sambuc int f9(unsigned len) {
194f4a2713aSLionel Sambuc   assert (len != 0);
195f4a2713aSLionel Sambuc   int *p = 0;
196f4a2713aSLionel Sambuc   unsigned i;
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc   for (i = 0; i < len; ++i)
199f4a2713aSLionel Sambuc    p = qux(i);
200f4a2713aSLionel Sambuc 
201f4a2713aSLionel Sambuc   return *p++; // no-warning
202f4a2713aSLionel Sambuc }
203f4a2713aSLionel Sambuc 
f9b(unsigned len)204f4a2713aSLionel Sambuc int f9b(unsigned len) {
205f4a2713aSLionel Sambuc   assert (len > 0);  // note use of '>'
206f4a2713aSLionel Sambuc   int *p = 0;
207f4a2713aSLionel Sambuc   unsigned i;
208f4a2713aSLionel Sambuc 
209f4a2713aSLionel Sambuc   for (i = 0; i < len; ++i)
210f4a2713aSLionel Sambuc    p = qux(i);
211f4a2713aSLionel Sambuc 
212f4a2713aSLionel Sambuc   return *p++; // no-warning
213f4a2713aSLionel Sambuc }
214f4a2713aSLionel Sambuc 
f10(int * p,signed char x,int y)215f4a2713aSLionel Sambuc int* f10(int* p, signed char x, int y) {
216f4a2713aSLionel Sambuc   // This line tests symbolication with compound assignments where the
217f4a2713aSLionel Sambuc   // LHS and RHS have different bitwidths.  The new symbolic value
218f4a2713aSLionel Sambuc   // for 'x' should have a bitwidth of 8.
219f4a2713aSLionel Sambuc   x &= y;
220f4a2713aSLionel Sambuc 
221f4a2713aSLionel Sambuc   // This tests that our symbolication worked, and that we correctly test
222f4a2713aSLionel Sambuc   // x against 0 (with the same bitwidth).
223f4a2713aSLionel Sambuc   if (!x) {
224f4a2713aSLionel Sambuc     if (!p) return 0;
225f4a2713aSLionel Sambuc     *p = 10;
226f4a2713aSLionel Sambuc   }
227f4a2713aSLionel Sambuc   else p = 0;
228f4a2713aSLionel Sambuc 
229f4a2713aSLionel Sambuc   if (!x)
230f4a2713aSLionel Sambuc     *p = 5; // no-warning
231f4a2713aSLionel Sambuc 
232f4a2713aSLionel Sambuc   return p;
233f4a2713aSLionel Sambuc }
234f4a2713aSLionel Sambuc 
235f4a2713aSLionel Sambuc // Test case from <rdar://problem/6407949>
f11(unsigned i)236f4a2713aSLionel Sambuc void f11(unsigned i) {
237f4a2713aSLionel Sambuc   int *x = 0;
238f4a2713aSLionel Sambuc   if (i >= 0) { // expected-warning{{always true}}
239f4a2713aSLionel Sambuc     // always true
240f4a2713aSLionel Sambuc   } else {
241f4a2713aSLionel Sambuc     *x = 42; // no-warning
242f4a2713aSLionel Sambuc   }
243f4a2713aSLionel Sambuc }
244f4a2713aSLionel Sambuc 
f11b(unsigned i)245f4a2713aSLionel Sambuc void f11b(unsigned i) {
246f4a2713aSLionel Sambuc   int *x = 0;
247f4a2713aSLionel Sambuc   if (i <= ~(unsigned)0) {
248f4a2713aSLionel Sambuc     // always true
249f4a2713aSLionel Sambuc   } else {
250f4a2713aSLionel Sambuc     *x = 42; // no-warning
251f4a2713aSLionel Sambuc   }
252f4a2713aSLionel Sambuc }
253f4a2713aSLionel Sambuc 
254f4a2713aSLionel Sambuc // Test case for switch statements with weird case arms.
255f4a2713aSLionel Sambuc typedef int     BOOL, *PBOOL, *LPBOOL;
256f4a2713aSLionel Sambuc typedef long    LONG_PTR, *PLONG_PTR;
257f4a2713aSLionel Sambuc typedef unsigned long ULONG_PTR, *PULONG_PTR;
258f4a2713aSLionel Sambuc typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR;
259f4a2713aSLionel Sambuc typedef LONG_PTR LRESULT;
260f4a2713aSLionel Sambuc typedef struct _F12ITEM *HF12ITEM;
261f4a2713aSLionel Sambuc 
f12(HF12ITEM i,char * q)262f4a2713aSLionel Sambuc void f12(HF12ITEM i, char *q) {
263f4a2713aSLionel Sambuc   char *p = 0;
264f4a2713aSLionel Sambuc   switch ((DWORD_PTR) i) {
265f4a2713aSLionel Sambuc   case 0 ... 10:
266f4a2713aSLionel Sambuc     p = q;
267f4a2713aSLionel Sambuc     break;
268f4a2713aSLionel Sambuc   case (DWORD_PTR) ((HF12ITEM) - 65535):
269f4a2713aSLionel Sambuc     return;
270f4a2713aSLionel Sambuc   default:
271f4a2713aSLionel Sambuc     return;
272f4a2713aSLionel Sambuc   }
273f4a2713aSLionel Sambuc 
274f4a2713aSLionel Sambuc   *p = 1; // no-warning
275f4a2713aSLionel Sambuc }
276f4a2713aSLionel Sambuc 
277f4a2713aSLionel Sambuc // Test handling of translating between integer "pointers" and back.
f13()278f4a2713aSLionel Sambuc void f13() {
279f4a2713aSLionel Sambuc   int *x = 0;
280f4a2713aSLionel Sambuc   if (((((int) x) << 2) + 1) >> 1) *x = 1;
281f4a2713aSLionel Sambuc }
282f4a2713aSLionel Sambuc 
283f4a2713aSLionel Sambuc // PR 4759 - Attribute non-null checking by the analyzer was not correctly
284f4a2713aSLionel Sambuc // handling pointer values that were undefined.
285f4a2713aSLionel Sambuc void pr4759_aux(int *p) __attribute__((nonnull));
286f4a2713aSLionel Sambuc 
pr4759()287f4a2713aSLionel Sambuc void pr4759() {
288f4a2713aSLionel Sambuc   int *p;
289f4a2713aSLionel Sambuc   pr4759_aux(p); // expected-warning{{Function call argument is an uninitialized value}}
290f4a2713aSLionel Sambuc }
291f4a2713aSLionel Sambuc 
292f4a2713aSLionel Sambuc // Relax function call arguments invalidation to be aware of const
293f4a2713aSLionel Sambuc // arguments. Test with function pointers. radar://10595327
294f4a2713aSLionel Sambuc void ttt(const int *nptr);
295f4a2713aSLionel Sambuc void ttt2(const int *nptr);
296f4a2713aSLionel Sambuc typedef void (*NoConstType)(int*);
foo10595327(int b)297f4a2713aSLionel Sambuc int foo10595327(int b) {
298f4a2713aSLionel Sambuc   void (*fp)(int *);
299f4a2713aSLionel Sambuc   // We use path sensitivity to get the function declaration. Even when the
300*0a6a1f1dSLionel Sambuc   // function pointer is cast to non-pointer-to-const parameter type, we can
301f4a2713aSLionel Sambuc   // find the right function declaration.
302f4a2713aSLionel Sambuc   if (b > 5)
303f4a2713aSLionel Sambuc     fp = (NoConstType)ttt2;
304f4a2713aSLionel Sambuc   else
305f4a2713aSLionel Sambuc     fp = (NoConstType)ttt;
306f4a2713aSLionel Sambuc   int x = 3;
307f4a2713aSLionel Sambuc   int y = x + 1;
308f4a2713aSLionel Sambuc   int *p = 0;
309f4a2713aSLionel Sambuc   fp(&y);
310f4a2713aSLionel Sambuc   if (x == y)
311f4a2713aSLionel Sambuc       return *p; // no-warning
312f4a2713aSLionel Sambuc   return 0;
313f4a2713aSLionel Sambuc }
314