Argyrios Kyrtzidis | bd90076 | 2011-02-28 01:28:01 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,core.experimental -std=gnu99 -analyzer-check-objc-mem -verify %s -analyzer-constraints=basic -analyzer-store=basic -Wreturn-type |
| 2 | // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,core.experimental -std=gnu99 -analyzer-check-objc-mem -verify %s -analyzer-constraints=range -analyzer-store=basic -Wreturn-type |
| 3 | // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,core.experimental -std=gnu99 -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -analyzer-no-purge-dead -verify %s -Wreturn-type |
| 4 | // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,core.experimental -std=gnu99 -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -verify %s -Wreturn-type |
Ted Kremenek | 2f54af4 | 2008-04-02 16:54:39 +0000 | [diff] [blame] | 5 | |
Ted Kremenek | f8add9b | 2009-09-28 23:54:40 +0000 | [diff] [blame] | 6 | typedef unsigned uintptr_t; |
| 7 | |
| 8 | extern void __assert_fail (__const char *__assertion, __const char *__file, |
| 9 | unsigned int __line, __const char *__function) |
| 10 | __attribute__ ((__noreturn__)); |
| 11 | |
| 12 | #define assert(expr) \ |
| 13 | ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__)) |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 14 | |
Ted Kremenek | 2f54af4 | 2008-04-02 16:54:39 +0000 | [diff] [blame] | 15 | void f1(int *p) { |
| 16 | if (p) *p = 1; |
| 17 | else *p = 0; // expected-warning{{ereference}} |
| 18 | } |
Ted Kremenek | b9ab690 | 2008-04-21 23:44:17 +0000 | [diff] [blame] | 19 | |
| 20 | struct foo_struct { |
| 21 | int x; |
| 22 | }; |
| 23 | |
| 24 | int f2(struct foo_struct* p) { |
| 25 | |
| 26 | if (p) |
| 27 | p->x = 1; |
| 28 | |
Ted Kremenek | 646c3c3 | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 29 | return p->x++; // expected-warning{{Access to field 'x' results in a dereference of a null pointer (loaded from variable 'p')}} |
Ted Kremenek | b9ab690 | 2008-04-21 23:44:17 +0000 | [diff] [blame] | 30 | } |
Ted Kremenek | 9704eac | 2008-04-22 04:56:55 +0000 | [diff] [blame] | 31 | |
| 32 | int f3(char* x) { |
| 33 | |
| 34 | int i = 2; |
| 35 | |
| 36 | if (x) |
| 37 | return x[i - 1]; |
| 38 | |
Ted Kremenek | 646c3c3 | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 39 | return x[i+1]; // expected-warning{{Array access (from variable 'x') results in a null pointer dereference}} |
Ted Kremenek | 9704eac | 2008-04-22 04:56:55 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Ted Kremenek | e2013f5 | 2008-04-29 23:25:09 +0000 | [diff] [blame] | 42 | int f3_b(char* x) { |
| 43 | |
| 44 | int i = 2; |
| 45 | |
| 46 | if (x) |
| 47 | return x[i - 1]; |
| 48 | |
Ted Kremenek | 646c3c3 | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 49 | return x[i+1]++; // expected-warning{{Array access (from variable 'x') results in a null pointer dereference}} |
Ted Kremenek | e2013f5 | 2008-04-29 23:25:09 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 52 | int f4(int *p) { |
| 53 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 54 | uintptr_t x = (uintptr_t) p; |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 55 | |
| 56 | if (x) |
| 57 | return 1; |
| 58 | |
| 59 | int *q = (int*) x; |
Ted Kremenek | 452b84d | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 60 | return *q; // expected-warning{{Dereference of null pointer (loaded from variable 'q')}} |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Tom Care | 6d0e6ce | 2010-08-27 22:46:32 +0000 | [diff] [blame] | 63 | int f4_b() { |
| 64 | short array[2]; |
| 65 | uintptr_t x = array; // expected-warning{{incompatible pointer to integer conversion}} |
| 66 | short *p = x; // expected-warning{{incompatible integer to pointer conversion}} |
| 67 | |
| 68 | // The following branch should be infeasible. |
Tom Care | 9edd4d0 | 2010-08-27 22:50:47 +0000 | [diff] [blame] | 69 | if (!(p == &array[0])) { // expected-warning{{Both operands to '==' always have the same value}} |
Tom Care | 6d0e6ce | 2010-08-27 22:46:32 +0000 | [diff] [blame] | 70 | p = 0; |
| 71 | *p = 1; // no-warning |
| 72 | } |
| 73 | |
| 74 | if (p) { |
| 75 | *p = 5; // no-warning |
| 76 | p = 0; |
| 77 | } |
| 78 | else return; // expected-warning {{non-void function 'f4_b' should return a value}} |
| 79 | |
| 80 | *p += 10; // expected-warning{{Dereference of null pointer}} |
| 81 | return 0; |
| 82 | } |
Ted Kremenek | e1c2a67 | 2009-01-13 01:04:21 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 84 | int f5() { |
| 85 | |
| 86 | char *s = "hello world"; |
| 87 | return s[0]; // no-warning |
| 88 | } |
| 89 | |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 90 | int bar(int* p, int q) __attribute__((nonnull)); |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 91 | |
| 92 | int f6(int *p) { |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 93 | return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 94 | : bar(p, 0); // no-warning |
| 95 | } |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 97 | int bar2(int* p, int q) __attribute__((nonnull(1))); |
| 98 | |
| 99 | int f6b(int *p) { |
| 100 | return !p ? bar2(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 101 | : bar2(p, 0); // no-warning |
| 102 | } |
| 103 | |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 104 | int bar3(int*p, int q, int *r) __attribute__((nonnull(1,3))); |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 105 | |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 106 | int f6c(int *p, int *q) { |
Ted Kremenek | a317e90 | 2008-12-04 19:44:23 +0000 | [diff] [blame] | 107 | return !p ? bar3(q, 2, p) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 108 | : bar3(p, 2, q); // no-warning |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 109 | } |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 110 | |
Mike Stump | f0549e2 | 2009-07-22 22:55:09 +0000 | [diff] [blame] | 111 | void f6d(int *p) { |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 112 | bar(p, 0); |
| 113 | // At this point, 'p' cannot be null. |
| 114 | if (!p) { |
| 115 | int *q = 0; |
| 116 | *q = 0xDEADBEEF; // no-warning |
| 117 | } |
| 118 | } |
| 119 | |
Jordy Rose | 9a12685 | 2010-06-21 20:08:28 +0000 | [diff] [blame] | 120 | void f6e(int *p, int offset) { |
| 121 | // PR7406 - crash from treating an UnknownVal as defined, to see if it's 0. |
| 122 | bar((p+offset)+1, 0); // not crash |
| 123 | } |
| 124 | |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 125 | int* qux(); |
| 126 | |
| 127 | int f7(int x) { |
| 128 | |
| 129 | int* p = 0; |
| 130 | |
| 131 | if (0 == x) |
| 132 | p = qux(); |
| 133 | |
| 134 | if (0 == x) |
| 135 | *p = 1; // no-warning |
| 136 | |
| 137 | return x; |
| 138 | } |
| 139 | |
Ted Kremenek | 935022a | 2009-05-02 00:41:02 +0000 | [diff] [blame] | 140 | int* f7b(int *x) { |
| 141 | |
| 142 | int* p = 0; |
| 143 | |
| 144 | if (((void*)0) == x) |
| 145 | p = qux(); |
| 146 | |
| 147 | if (((void*)0) == x) |
| 148 | *p = 1; // no-warning |
| 149 | |
| 150 | return x; |
| 151 | } |
| 152 | |
Ted Kremenek | 1308f57 | 2009-05-04 17:27:32 +0000 | [diff] [blame] | 153 | int* f7c(int *x) { |
| 154 | |
| 155 | int* p = 0; |
| 156 | |
| 157 | if (((void*)0) == x) |
| 158 | p = qux(); |
| 159 | |
| 160 | if (((void*)0) != x) |
| 161 | return x; |
Ted Kremenek | 65d80fd | 2009-05-04 17:53:11 +0000 | [diff] [blame] | 162 | |
| 163 | // If we reach here then 'p' is not null. |
| 164 | *p = 1; // no-warning |
Ted Kremenek | 1308f57 | 2009-05-04 17:27:32 +0000 | [diff] [blame] | 165 | return x; |
| 166 | } |
| 167 | |
| 168 | int* f7c2(int *x) { |
| 169 | |
| 170 | int* p = 0; |
| 171 | |
| 172 | if (((void*)0) == x) |
| 173 | p = qux(); |
| 174 | |
| 175 | if (((void*)0) == x) |
| 176 | return x; |
| 177 | |
| 178 | *p = 1; // expected-warning{{null}} |
| 179 | return x; |
| 180 | } |
| 181 | |
Ted Kremenek | 935022a | 2009-05-02 00:41:02 +0000 | [diff] [blame] | 182 | |
Mike Stump | 339d52a | 2009-07-21 18:51:31 +0000 | [diff] [blame] | 183 | void f8(int *p, int *q) { |
Ted Kremenek | dd463b8 | 2008-08-16 00:45:40 +0000 | [diff] [blame] | 184 | if (!p) |
| 185 | if (p) |
| 186 | *p = 1; // no-warning |
| 187 | |
| 188 | if (q) |
| 189 | if (!q) |
| 190 | *q = 1; // no-warning |
| 191 | } |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 192 | |
| 193 | int* qux(); |
| 194 | |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 195 | int f9(unsigned len) { |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 196 | assert (len != 0); |
| 197 | int *p = 0; |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 198 | unsigned i; |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 199 | |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 200 | for (i = 0; i < len; ++i) |
Ted Kremenek | e2b0083 | 2008-09-16 23:25:28 +0000 | [diff] [blame] | 201 | p = qux(i); |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 202 | |
| 203 | return *p++; // no-warning |
| 204 | } |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 205 | |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 206 | int f9b(unsigned len) { |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 207 | assert (len > 0); // note use of '>' |
| 208 | int *p = 0; |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 209 | unsigned i; |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 210 | |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 211 | for (i = 0; i < len; ++i) |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 212 | p = qux(i); |
| 213 | |
| 214 | return *p++; // no-warning |
| 215 | } |
| 216 | |
Ted Kremenek | 973e72a | 2008-11-15 04:44:13 +0000 | [diff] [blame] | 217 | int* f10(int* p, signed char x, int y) { |
| 218 | // This line tests symbolication with compound assignments where the |
| 219 | // LHS and RHS have different bitwidths. The new symbolic value |
| 220 | // for 'x' should have a bitwidth of 8. |
| 221 | x &= y; |
| 222 | |
| 223 | // This tests that our symbolication worked, and that we correctly test |
| 224 | // x against 0 (with the same bitwidth). |
| 225 | if (!x) { |
Steve Naroff | 2c0ccd0 | 2009-04-30 16:01:26 +0000 | [diff] [blame] | 226 | if (!p) return; // expected-warning {{non-void function 'f10' should return a value}} |
Ted Kremenek | 973e72a | 2008-11-15 04:44:13 +0000 | [diff] [blame] | 227 | *p = 10; |
| 228 | } |
| 229 | else p = 0; |
| 230 | |
| 231 | if (!x) |
| 232 | *p = 5; // no-warning |
| 233 | |
| 234 | return p; |
| 235 | } |
| 236 | |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 237 | // Test case from <rdar://problem/6407949> |
| 238 | void f11(unsigned i) { |
| 239 | int *x = 0; |
John McCall | 8205c1a | 2010-09-08 02:01:27 +0000 | [diff] [blame] | 240 | if (i >= 0) { // expected-warning{{always true}} |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 241 | // always true |
| 242 | } else { |
| 243 | *x = 42; // no-warning |
| 244 | } |
| 245 | } |
| 246 | |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 247 | void f11b(unsigned i) { |
| 248 | int *x = 0; |
| 249 | if (i <= ~(unsigned)0) { |
| 250 | // always true |
| 251 | } else { |
| 252 | *x = 42; // no-warning |
| 253 | } |
| 254 | } |
| 255 | |
Ted Kremenek | 72afb37 | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 256 | // Test case for switch statements with weird case arms. |
| 257 | typedef int BOOL, *PBOOL, *LPBOOL; |
| 258 | typedef long LONG_PTR, *PLONG_PTR; |
| 259 | typedef unsigned long ULONG_PTR, *PULONG_PTR; |
| 260 | typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR; |
| 261 | typedef LONG_PTR LRESULT; |
| 262 | typedef struct _F12ITEM *HF12ITEM; |
| 263 | |
| 264 | void f12(HF12ITEM i, char *q) { |
| 265 | char *p = 0; |
| 266 | switch ((DWORD_PTR) i) { |
| 267 | case 0 ... 10: |
| 268 | p = q; |
| 269 | break; |
| 270 | case (DWORD_PTR) ((HF12ITEM) - 65535): |
| 271 | return; |
| 272 | default: |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | *p = 1; // no-warning |
| 277 | } |
| 278 | |
Ted Kremenek | efcfcc0 | 2009-03-05 02:42:32 +0000 | [diff] [blame] | 279 | // Test handling of translating between integer "pointers" and back. |
| 280 | void f13() { |
| 281 | int *x = 0; |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 282 | if (((((int) x) << 2) + 1) >> 1) *x = 1; |
Ted Kremenek | efcfcc0 | 2009-03-05 02:42:32 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Ted Kremenek | ac50213 | 2009-08-24 22:56:32 +0000 | [diff] [blame] | 285 | // PR 4759 - Attribute non-null checking by the analyzer was not correctly |
| 286 | // handling pointer values that were undefined. |
| 287 | void pr4759_aux(int *p) __attribute__((nonnull)); |
| 288 | |
| 289 | void pr4759() { |
| 290 | int *p; |
Ted Kremenek | 818b433 | 2010-09-09 22:51:55 +0000 | [diff] [blame] | 291 | pr4759_aux(p); // expected-warning{{Function call argument is an uninitialized value}} |
Ted Kremenek | ac50213 | 2009-08-24 22:56:32 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Ted Kremenek | efcfcc0 | 2009-03-05 02:42:32 +0000 | [diff] [blame] | 294 | |