Chad Rosier | 7a0a31c | 2012-02-03 01:49:51 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,deadcode,experimental.deadcode.IdempotentOperations,experimental.core -std=gnu99 -analyzer-store=region -analyzer-constraints=range -analyzer-purge=none -verify %s -Wno-error=return-type |
| 2 | // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,deadcode,experimental.deadcode.IdempotentOperations,experimental.core -std=gnu99 -analyzer-store=region -analyzer-constraints=range -verify %s -Wno-error=return-type |
Ted Kremenek | 2f54af4 | 2008-04-02 16:54:39 +0000 | [diff] [blame] | 3 | |
Ted Kremenek | f8add9b | 2009-09-28 23:54:40 +0000 | [diff] [blame] | 4 | typedef unsigned uintptr_t; |
| 5 | |
| 6 | extern void __assert_fail (__const char *__assertion, __const char *__file, |
| 7 | unsigned int __line, __const char *__function) |
| 8 | __attribute__ ((__noreturn__)); |
| 9 | |
| 10 | #define assert(expr) \ |
| 11 | ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__)) |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 12 | |
Ted Kremenek | 2f54af4 | 2008-04-02 16:54:39 +0000 | [diff] [blame] | 13 | void f1(int *p) { |
| 14 | if (p) *p = 1; |
| 15 | else *p = 0; // expected-warning{{ereference}} |
| 16 | } |
Ted Kremenek | b9ab690 | 2008-04-21 23:44:17 +0000 | [diff] [blame] | 17 | |
| 18 | struct foo_struct { |
| 19 | int x; |
| 20 | }; |
| 21 | |
| 22 | int f2(struct foo_struct* p) { |
| 23 | |
| 24 | if (p) |
| 25 | p->x = 1; |
| 26 | |
Ted Kremenek | 646c3c3 | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 27 | 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] | 28 | } |
Ted Kremenek | 9704eac | 2008-04-22 04:56:55 +0000 | [diff] [blame] | 29 | |
| 30 | int f3(char* x) { |
| 31 | |
| 32 | int i = 2; |
| 33 | |
| 34 | if (x) |
| 35 | return x[i - 1]; |
| 36 | |
Ted Kremenek | 646c3c3 | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 37 | 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] | 38 | } |
| 39 | |
Ted Kremenek | e2013f5 | 2008-04-29 23:25:09 +0000 | [diff] [blame] | 40 | int f3_b(char* x) { |
| 41 | |
| 42 | int i = 2; |
| 43 | |
| 44 | if (x) |
| 45 | return x[i - 1]; |
| 46 | |
Ted Kremenek | 646c3c3 | 2010-10-26 00:06:13 +0000 | [diff] [blame] | 47 | 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] | 48 | } |
| 49 | |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 50 | int f4(int *p) { |
| 51 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 52 | uintptr_t x = (uintptr_t) p; |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 53 | |
| 54 | if (x) |
| 55 | return 1; |
| 56 | |
| 57 | int *q = (int*) x; |
Ted Kremenek | 452b84d | 2010-03-23 01:11:38 +0000 | [diff] [blame] | 58 | return *q; // expected-warning{{Dereference of null pointer (loaded from variable 'q')}} |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Tom Care | 6d0e6ce | 2010-08-27 22:46:32 +0000 | [diff] [blame] | 61 | int f4_b() { |
| 62 | short array[2]; |
| 63 | uintptr_t x = array; // expected-warning{{incompatible pointer to integer conversion}} |
| 64 | short *p = x; // expected-warning{{incompatible integer to pointer conversion}} |
| 65 | |
| 66 | // The following branch should be infeasible. |
Tom Care | 9edd4d0 | 2010-08-27 22:50:47 +0000 | [diff] [blame] | 67 | 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] | 68 | p = 0; |
| 69 | *p = 1; // no-warning |
| 70 | } |
| 71 | |
| 72 | if (p) { |
| 73 | *p = 5; // no-warning |
| 74 | p = 0; |
| 75 | } |
| 76 | else return; // expected-warning {{non-void function 'f4_b' should return a value}} |
| 77 | |
| 78 | *p += 10; // expected-warning{{Dereference of null pointer}} |
| 79 | return 0; |
| 80 | } |
Ted Kremenek | e1c2a67 | 2009-01-13 01:04:21 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 82 | int f5() { |
| 83 | |
| 84 | char *s = "hello world"; |
| 85 | return s[0]; // no-warning |
| 86 | } |
| 87 | |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 88 | int bar(int* p, int q) __attribute__((nonnull)); |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 89 | |
| 90 | int f6(int *p) { |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 91 | return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 92 | : bar(p, 0); // no-warning |
| 93 | } |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 95 | int bar2(int* p, int q) __attribute__((nonnull(1))); |
| 96 | |
| 97 | int f6b(int *p) { |
| 98 | return !p ? bar2(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 99 | : bar2(p, 0); // no-warning |
| 100 | } |
| 101 | |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 102 | int bar3(int*p, int q, int *r) __attribute__((nonnull(1,3))); |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 103 | |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 104 | int f6c(int *p, int *q) { |
Ted Kremenek | a317e90 | 2008-12-04 19:44:23 +0000 | [diff] [blame] | 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 |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 107 | } |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 108 | |
Mike Stump | f0549e2 | 2009-07-22 22:55:09 +0000 | [diff] [blame] | 109 | void f6d(int *p) { |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 110 | bar(p, 0); |
| 111 | // At this point, 'p' cannot be null. |
| 112 | if (!p) { |
| 113 | int *q = 0; |
| 114 | *q = 0xDEADBEEF; // no-warning |
| 115 | } |
| 116 | } |
| 117 | |
Jordy Rose | 9a12685 | 2010-06-21 20:08:28 +0000 | [diff] [blame] | 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 |
| 121 | } |
| 122 | |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 123 | int* qux(); |
| 124 | |
| 125 | int f7(int x) { |
| 126 | |
| 127 | int* p = 0; |
| 128 | |
| 129 | if (0 == x) |
| 130 | p = qux(); |
| 131 | |
| 132 | if (0 == x) |
| 133 | *p = 1; // no-warning |
| 134 | |
| 135 | return x; |
| 136 | } |
| 137 | |
Ted Kremenek | 935022a | 2009-05-02 00:41:02 +0000 | [diff] [blame] | 138 | int* f7b(int *x) { |
| 139 | |
| 140 | int* p = 0; |
| 141 | |
| 142 | if (((void*)0) == x) |
| 143 | p = qux(); |
| 144 | |
| 145 | if (((void*)0) == x) |
| 146 | *p = 1; // no-warning |
| 147 | |
| 148 | return x; |
| 149 | } |
| 150 | |
Ted Kremenek | 1308f57 | 2009-05-04 17:27:32 +0000 | [diff] [blame] | 151 | int* f7c(int *x) { |
| 152 | |
| 153 | int* p = 0; |
| 154 | |
| 155 | if (((void*)0) == x) |
| 156 | p = qux(); |
| 157 | |
| 158 | if (((void*)0) != x) |
| 159 | return x; |
Ted Kremenek | 65d80fd | 2009-05-04 17:53:11 +0000 | [diff] [blame] | 160 | |
| 161 | // If we reach here then 'p' is not null. |
| 162 | *p = 1; // no-warning |
Ted Kremenek | 1308f57 | 2009-05-04 17:27:32 +0000 | [diff] [blame] | 163 | return x; |
| 164 | } |
| 165 | |
| 166 | int* f7c2(int *x) { |
| 167 | |
| 168 | int* p = 0; |
| 169 | |
| 170 | if (((void*)0) == x) |
| 171 | p = qux(); |
| 172 | |
| 173 | if (((void*)0) == x) |
| 174 | return x; |
| 175 | |
| 176 | *p = 1; // expected-warning{{null}} |
| 177 | return x; |
| 178 | } |
| 179 | |
Ted Kremenek | 935022a | 2009-05-02 00:41:02 +0000 | [diff] [blame] | 180 | |
Mike Stump | 339d52a | 2009-07-21 18:51:31 +0000 | [diff] [blame] | 181 | void f8(int *p, int *q) { |
Ted Kremenek | dd463b8 | 2008-08-16 00:45:40 +0000 | [diff] [blame] | 182 | if (!p) |
| 183 | if (p) |
| 184 | *p = 1; // no-warning |
| 185 | |
| 186 | if (q) |
| 187 | if (!q) |
| 188 | *q = 1; // no-warning |
| 189 | } |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 190 | |
| 191 | int* qux(); |
| 192 | |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 193 | int f9(unsigned len) { |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 194 | assert (len != 0); |
| 195 | int *p = 0; |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 196 | unsigned i; |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 197 | |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 198 | for (i = 0; i < len; ++i) |
Ted Kremenek | e2b0083 | 2008-09-16 23:25:28 +0000 | [diff] [blame] | 199 | p = qux(i); |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 200 | |
| 201 | return *p++; // no-warning |
| 202 | } |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 203 | |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 204 | int f9b(unsigned len) { |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 205 | assert (len > 0); // note use of '>' |
| 206 | int *p = 0; |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 207 | unsigned i; |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 209 | for (i = 0; i < len; ++i) |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 210 | p = qux(i); |
| 211 | |
| 212 | return *p++; // no-warning |
| 213 | } |
| 214 | |
Ted Kremenek | 973e72a | 2008-11-15 04:44:13 +0000 | [diff] [blame] | 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. |
| 219 | x &= y; |
| 220 | |
| 221 | // This tests that our symbolication worked, and that we correctly test |
| 222 | // x against 0 (with the same bitwidth). |
| 223 | if (!x) { |
Anna Zaks | 01f2a1e | 2011-11-18 00:45:20 +0000 | [diff] [blame] | 224 | if (!p) return 0; |
Ted Kremenek | 973e72a | 2008-11-15 04:44:13 +0000 | [diff] [blame] | 225 | *p = 10; |
| 226 | } |
| 227 | else p = 0; |
| 228 | |
| 229 | if (!x) |
| 230 | *p = 5; // no-warning |
| 231 | |
| 232 | return p; |
| 233 | } |
| 234 | |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 235 | // Test case from <rdar://problem/6407949> |
| 236 | void f11(unsigned i) { |
| 237 | int *x = 0; |
John McCall | 8205c1a | 2010-09-08 02:01:27 +0000 | [diff] [blame] | 238 | if (i >= 0) { // expected-warning{{always true}} |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 239 | // always true |
| 240 | } else { |
| 241 | *x = 42; // no-warning |
| 242 | } |
| 243 | } |
| 244 | |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 245 | void f11b(unsigned i) { |
| 246 | int *x = 0; |
| 247 | if (i <= ~(unsigned)0) { |
| 248 | // always true |
| 249 | } else { |
| 250 | *x = 42; // no-warning |
| 251 | } |
| 252 | } |
| 253 | |
Ted Kremenek | 72afb37 | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 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; |
| 261 | |
| 262 | void f12(HF12ITEM i, char *q) { |
| 263 | char *p = 0; |
| 264 | switch ((DWORD_PTR) i) { |
| 265 | case 0 ... 10: |
| 266 | p = q; |
| 267 | break; |
| 268 | case (DWORD_PTR) ((HF12ITEM) - 65535): |
| 269 | return; |
| 270 | default: |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | *p = 1; // no-warning |
| 275 | } |
| 276 | |
Ted Kremenek | efcfcc0 | 2009-03-05 02:42:32 +0000 | [diff] [blame] | 277 | // Test handling of translating between integer "pointers" and back. |
| 278 | void f13() { |
| 279 | int *x = 0; |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 280 | if (((((int) x) << 2) + 1) >> 1) *x = 1; |
Ted Kremenek | efcfcc0 | 2009-03-05 02:42:32 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Ted Kremenek | ac50213 | 2009-08-24 22:56:32 +0000 | [diff] [blame] | 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)); |
| 286 | |
| 287 | void pr4759() { |
| 288 | int *p; |
Ted Kremenek | 818b433 | 2010-09-09 22:51:55 +0000 | [diff] [blame] | 289 | pr4759_aux(p); // expected-warning{{Function call argument is an uninitialized value}} |
Ted Kremenek | ac50213 | 2009-08-24 22:56:32 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Anna Zaks | 2cbe791 | 2011-12-20 22:35:30 +0000 | [diff] [blame] | 292 | // Relax function call arguments invalidation to be aware of const |
| 293 | // arguments. Test with function pointers. radar://10595327 |
| 294 | void ttt(const int *nptr); |
| 295 | void ttt2(const int *nptr); |
| 296 | typedef void (*NoConstType)(int*); |
| 297 | int foo10595327(int b) { |
| 298 | void (*fp)(int *); |
| 299 | // We use path sensitivity to get the function declaration. Even when the |
| 300 | // function pointer is cast to non pointer-to-const parameter type, we can |
| 301 | // find the right function declaration. |
| 302 | if (b > 5) |
| 303 | fp = (NoConstType)ttt2; |
| 304 | else |
| 305 | fp = (NoConstType)ttt; |
| 306 | int x = 3; |
| 307 | int y = x + 1; |
| 308 | int *p = 0; |
| 309 | fp(&y); |
| 310 | if (x == y) |
| 311 | return *p; // no-warning |
| 312 | return 0; |
| 313 | } |