1 // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,deadcode,experimental.core -std=gnu99 -analyzer-store=region -analyzer-constraints=range -analyzer-purge=none -verify %s -Wreturn-type
2 // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,deadcode,experimental.core -std=gnu99 -analyzer-store=region -analyzer-constraints=range -verify %s -Wreturn-type
4 typedef unsigned uintptr_t;
6 extern void __assert_fail (__const char *__assertion, __const char *__file,
7 unsigned int __line, __const char *__function)
8 __attribute__ ((__noreturn__));
10 #define assert(expr) \
11 ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
15 else *p = 0; // expected-warning{{ereference}}
22 int f2(struct foo_struct* p) {
27 return p->x++; // expected-warning{{Access to field 'x' results in a dereference of a null pointer (loaded from variable 'p')}}
37 return x[i+1]; // expected-warning{{Array access (from variable 'x') results in a null pointer dereference}}
47 return x[i+1]++; // expected-warning{{Array access (from variable 'x') results in a null pointer dereference}}
52 uintptr_t x = (uintptr_t) p;
58 return *q; // expected-warning{{Dereference of null pointer (loaded from variable 'q')}}
63 uintptr_t x = array; // expected-warning{{incompatible pointer to integer conversion}}
64 short *p = x; // expected-warning{{incompatible integer to pointer conversion}}
66 // The following branch should be infeasible.
67 if (!(p == &array[0])) { // expected-warning{{Both operands to '==' always have the same value}}
76 else return; // expected-warning {{non-void function 'f4_b' should return a value}}
78 *p += 10; // expected-warning{{Dereference of null pointer}}
84 char *s = "hello world";
85 return s[0]; // no-warning
88 int bar(int* p, int q) __attribute__((nonnull));
91 return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
92 : bar(p, 0); // no-warning
95 int bar2(int* p, int q) __attribute__((nonnull(1)));
98 return !p ? bar2(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
99 : bar2(p, 0); // no-warning
102 int bar3(int*p, int q, int *r) __attribute__((nonnull(1,3)));
104 int f6c(int *p, int *q) {
105 return !p ? bar3(q, 2, p) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
106 : bar3(p, 2, q); // no-warning
111 // At this point, 'p' cannot be null.
114 *q = 0xDEADBEEF; // no-warning
118 void f6e(int *p, int offset) {
119 // PR7406 - crash from treating an UnknownVal as defined, to see if it's 0.
120 bar((p+offset)+1, 0); // not crash
133 *p = 1; // no-warning
146 *p = 1; // no-warning
161 // If we reach here then 'p' is not null.
162 *p = 1; // no-warning
176 *p = 1; // expected-warning{{null}}
181 void f8(int *p, int *q) {
184 *p = 1; // no-warning
188 *q = 1; // no-warning
193 int f9(unsigned len) {
198 for (i = 0; i < len; ++i)
201 return *p++; // no-warning
204 int f9b(unsigned len) {
205 assert (len > 0); // note use of '>'
209 for (i = 0; i < len; ++i)
212 return *p++; // no-warning
215 int* f10(int* p, signed char x, int y) {
216 // This line tests symbolication with compound assignments where the
217 // LHS and RHS have different bitwidths. The new symbolic value
218 // for 'x' should have a bitwidth of 8.
221 // This tests that our symbolication worked, and that we correctly test
222 // x against 0 (with the same bitwidth).
224 if (!p) return; // expected-warning {{non-void function 'f10' should return a value}}
230 *p = 5; // no-warning
235 // Test case from <rdar://problem/6407949>
236 void f11(unsigned i) {
238 if (i >= 0) { // expected-warning{{always true}}
241 *x = 42; // no-warning
245 void f11b(unsigned i) {
247 if (i <= ~(unsigned)0) {
250 *x = 42; // no-warning
254 // Test case for switch statements with weird case arms.
255 typedef int BOOL, *PBOOL, *LPBOOL;
256 typedef long LONG_PTR, *PLONG_PTR;
257 typedef unsigned long ULONG_PTR, *PULONG_PTR;
258 typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR;
259 typedef LONG_PTR LRESULT;
260 typedef struct _F12ITEM *HF12ITEM;
262 void f12(HF12ITEM i, char *q) {
264 switch ((DWORD_PTR) i) {
268 case (DWORD_PTR) ((HF12ITEM) - 65535):
274 *p = 1; // no-warning
277 // Test handling of translating between integer "pointers" and back.
280 if (((((int) x) << 2) + 1) >> 1) *x = 1;
283 // PR 4759 - Attribute non-null checking by the analyzer was not correctly
284 // handling pointer values that were undefined.
285 void pr4759_aux(int *p) __attribute__((nonnull));
289 pr4759_aux(p); // expected-warning{{Function call argument is an uninitialized value}}