Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=basic --verify -fblocks %s && |
Ted Kremenek | 921109a | 2009-07-01 23:19:52 +0000 | [diff] [blame] | 2 | // RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=range --verify -fblocks %s && |
| 3 | // RUN: clang-cc -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=basic --verify -fblocks %s && |
| 4 | // RUN: clang-cc -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=range --verify -fblocks %s |
Ted Kremenek | 2dabd42 | 2009-01-22 18:53:15 +0000 | [diff] [blame] | 5 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 6 | typedef struct objc_ivar *Ivar; |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 7 | typedef struct objc_selector *SEL; |
| 8 | typedef signed char BOOL; |
| 9 | typedef int NSInteger; |
| 10 | typedef unsigned int NSUInteger; |
| 11 | typedef struct _NSZone NSZone; |
Ted Kremenek | 5997888 | 2009-07-08 22:42:46 +0000 | [diff] [blame] | 12 | @class NSInvocation, NSArray, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 13 | @protocol NSObject |
| 14 | - (BOOL)isEqual:(id)object; |
| 15 | - (id)autorelease; |
| 16 | @end |
| 17 | @protocol NSCopying |
| 18 | - (id)copyWithZone:(NSZone *)zone; |
| 19 | @end |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 20 | @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 21 | @protocol NSCoding |
| 22 | - (void)encodeWithCoder:(NSCoder *)aCoder; |
| 23 | @end |
| 24 | @interface NSObject <NSObject> {} |
| 25 | - (id)init; |
| 26 | + (id)allocWithZone:(NSZone *)zone; |
| 27 | @end |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 28 | extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone); |
| 29 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> |
| 30 | - (NSUInteger)length; |
| 31 | + (id)stringWithUTF8String:(const char *)nullTerminatedCString; |
| 32 | @end extern NSString * const NSBundleDidLoadNotification; |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 33 | @interface NSValue : NSObject <NSCopying, NSCoding> |
| 34 | - (void)getValue:(void *)value; |
| 35 | @end |
| 36 | @interface NSNumber : NSValue |
| 37 | - (char)charValue; |
| 38 | - (id)initWithBool:(BOOL)value; |
| 39 | @end |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 40 | @interface NSAssertionHandler : NSObject {} |
| 41 | + (NSAssertionHandler *)currentHandler; |
| 42 | - (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...; |
| 43 | @end |
| 44 | extern NSString * const NSConnectionReplyMode; |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 45 | typedef float CGFloat; |
| 46 | typedef struct _NSPoint { |
| 47 | CGFloat x; |
| 48 | CGFloat y; |
| 49 | } NSPoint; |
| 50 | typedef struct _NSSize { |
| 51 | CGFloat width; |
| 52 | CGFloat height; |
| 53 | } NSSize; |
| 54 | typedef struct _NSRect { |
| 55 | NSPoint origin; |
| 56 | NSSize size; |
| 57 | } NSRect; |
Ted Kremenek | 9f67ede | 2008-10-01 05:05:46 +0000 | [diff] [blame] | 58 | |
| 59 | // Reduced test case from crash in <rdar://problem/6253157> |
Ted Kremenek | 9f67ede | 2008-10-01 05:05:46 +0000 | [diff] [blame] | 60 | @interface A @end |
| 61 | @implementation A |
| 62 | - (void)foo:(void (^)(NSObject *x))block { |
| 63 | if (!((block != ((void *)0)))) {} |
| 64 | } |
| 65 | @end |
| 66 | |
Ted Kremenek | 6dfe2f5 | 2008-10-18 22:20:20 +0000 | [diff] [blame] | 67 | // Reduced test case from crash in PR 2796; |
| 68 | // http://llvm.org/bugs/show_bug.cgi?id=2796 |
| 69 | |
| 70 | unsigned foo(unsigned x) { return __alignof__((x)) + sizeof(x); } |
Ted Kremenek | 9253b0f | 2008-10-20 23:14:31 +0000 | [diff] [blame] | 71 | |
| 72 | // Improvement to path-sensitivity involving compound assignments. |
| 73 | // Addresses false positive in <rdar://problem/6268365> |
| 74 | // |
| 75 | |
| 76 | unsigned r6268365Aux(); |
| 77 | |
| 78 | void r6268365() { |
| 79 | unsigned x = 0; |
| 80 | x &= r6268365Aux(); |
| 81 | unsigned j = 0; |
| 82 | |
| 83 | if (x == 0) ++j; |
| 84 | if (x == 0) x = x / j; // no-warning |
| 85 | } |
| 86 | |
Ted Kremenek | c13b6e2 | 2008-10-20 23:40:25 +0000 | [diff] [blame] | 87 | void divzeroassume(unsigned x, unsigned j) { |
| 88 | x /= j; |
| 89 | if (j == 0) x /= 0; // no-warning |
| 90 | if (j == 0) x /= j; // no-warning |
| 91 | if (j == 0) x = x / 0; // no-warning |
| 92 | } |
| 93 | |
| 94 | void divzeroassumeB(unsigned x, unsigned j) { |
| 95 | x = x / j; |
| 96 | if (j == 0) x /= 0; // no-warning |
| 97 | if (j == 0) x /= j; // no-warning |
| 98 | if (j == 0) x = x / 0; // no-warning |
| 99 | } |
| 100 | |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 101 | // InitListExpr processing |
| 102 | |
| 103 | typedef float __m128 __attribute__((__vector_size__(16), __may_alias__)); |
| 104 | __m128 return128() { |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 105 | // This compound literal has a Vector type. We currently just |
| 106 | // return UnknownVal. |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 107 | return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 108 | } |
| 109 | |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 110 | typedef long long __v2di __attribute__ ((__vector_size__ (16))); |
| 111 | typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__)); |
| 112 | __m128i vec128i(long long __q1, long long __q0) { |
| 113 | // This compound literal returns true for both isVectorType() and |
| 114 | // isIntegerType(). |
| 115 | return __extension__ (__m128i)(__v2di){ __q0, __q1 }; |
| 116 | } |
| 117 | |
Ted Kremenek | 8322d6a | 2008-12-09 00:14:48 +0000 | [diff] [blame] | 118 | // Zero-sized VLAs. |
| 119 | void check_zero_sized_VLA(int x) { |
| 120 | if (x) |
| 121 | return; |
| 122 | |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 123 | int vla[x]; // expected-warning{{Variable-length array 'vla' has zero elements (undefined behavior)}} |
Ted Kremenek | 159d248 | 2008-12-09 00:44:16 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void check_uninit_sized_VLA() { |
| 127 | int x; |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 128 | int vla[x]; // expected-warning{{Variable-length array 'vla' garbage value for array size}} |
Ted Kremenek | 8322d6a | 2008-12-09 00:14:48 +0000 | [diff] [blame] | 129 | } |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 130 | |
Ted Kremenek | 55f7bcb | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 131 | // sizeof(void) |
| 132 | // - Tests a regression reported in PR 3211: http://llvm.org/bugs/show_bug.cgi?id=3211 |
| 133 | void handle_sizeof_void(unsigned flag) { |
| 134 | int* p = 0; |
| 135 | |
| 136 | if (flag) { |
| 137 | if (sizeof(void) == 1) |
| 138 | return; |
| 139 | // Infeasible. |
| 140 | *p = 1; // no-warning |
| 141 | } |
| 142 | |
| 143 | void* q; |
| 144 | |
| 145 | if (!flag) { |
| 146 | if (sizeof(*q) == 1) |
| 147 | return; |
| 148 | // Infeasibe. |
| 149 | *p = 1; // no-warning |
| 150 | } |
| 151 | |
| 152 | // Infeasible. |
| 153 | *p = 1; // no-warning |
| 154 | } |
| 155 | |
Ted Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 156 | // PR 3422 |
| 157 | void pr3422_helper(char *p); |
| 158 | void pr3422() { |
| 159 | char buf[100]; |
| 160 | char *q = &buf[10]; |
| 161 | pr3422_helper(&q[1]); |
| 162 | } |
| 163 | |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 164 | // PR 3543 (handle empty statement expressions) |
Mike Stump | 006105d | 2009-07-21 18:59:16 +0000 | [diff] [blame] | 165 | void pr_3543(void) { |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 166 | ({}); |
| 167 | } |
| 168 | |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 169 | // <rdar://problem/6611677> |
| 170 | // This test case test the use of a vector type within an array subscript |
| 171 | // expression. |
| 172 | typedef long long __a64vector __attribute__((__vector_size__(8))); |
| 173 | typedef long long __a128vector __attribute__((__vector_size__(16))); |
| 174 | static inline __a64vector __attribute__((__always_inline__, __nodebug__)) |
| 175 | my_test_mm_movepi64_pi64(__a128vector a) { |
| 176 | return (__a64vector)a[0]; |
| 177 | } |
| 178 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 179 | // Test basic tracking of ivars associated with 'self'. |
| 180 | @interface SelfIvarTest : NSObject { |
| 181 | int flag; |
| 182 | } |
| 183 | - (void)test_self_tracking; |
| 184 | @end |
| 185 | |
| 186 | @implementation SelfIvarTest |
| 187 | - (void)test_self_tracking { |
| 188 | char *p = 0; |
| 189 | char c; |
| 190 | |
| 191 | if (flag) |
| 192 | p = "hello"; |
| 193 | |
| 194 | if (flag) |
| 195 | c = *p; // no-warning |
| 196 | } |
| 197 | @end |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 7de20fe | 2009-03-11 02:29:48 +0000 | [diff] [blame] | 199 | // PR 3770 |
| 200 | char pr3770(int x) { |
| 201 | int y = x & 0x2; |
| 202 | char *p = 0; |
| 203 | if (y == 1) |
| 204 | p = "hello"; |
| 205 | |
| 206 | if (y == 1) |
| 207 | return p[0]; // no-warning |
| 208 | |
| 209 | return 'a'; |
| 210 | } |
| 211 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 212 | // PR 3772 |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 213 | // - We just want to test that this doesn't crash the analyzer. |
| 214 | typedef struct st ST; |
| 215 | struct st { char *name; }; |
| 216 | extern ST *Cur_Pu; |
| 217 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 218 | void pr3772(void) |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 219 | { |
| 220 | static ST *last_Cur_Pu; |
| 221 | if (last_Cur_Pu == Cur_Pu) { |
| 222 | return; |
| 223 | } |
| 224 | } |
| 225 | |
Ted Kremenek | 344d4c8 | 2009-03-11 18:17:16 +0000 | [diff] [blame] | 226 | // PR 3780 - This tests that StmtIterator isn't broken for VLAs in DeclGroups. |
| 227 | void pr3780(int sz) { typedef double MAT[sz][sz]; } |
Ted Kremenek | fa6228d | 2009-03-11 02:52:39 +0000 | [diff] [blame] | 228 | |
Ted Kremenek | ec099f1 | 2009-03-18 22:10:22 +0000 | [diff] [blame] | 229 | // <rdar://problem/6695527> - Test that we don't symbolicate doubles before |
| 230 | // we are ready to do something with them. |
| 231 | int rdar6695527(double x) { |
| 232 | if (!x) { return 0; } |
| 233 | return 1; |
| 234 | } |
Ted Kremenek | 693de5d | 2009-03-23 15:42:58 +0000 | [diff] [blame] | 235 | |
| 236 | // <rdar://problem/6708148> - Test that we properly invalidate structs |
| 237 | // passed-by-reference to a function. |
| 238 | void pr6708148_invalidate(NSRect *x); |
| 239 | void pr6708148_use(NSRect x); |
| 240 | void pr6708148_test(void) { |
| 241 | NSRect x; |
| 242 | pr6708148_invalidate(&x); |
| 243 | pr6708148_use(x); // no-warning |
| 244 | } |
| 245 | |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 246 | // Handle both kinds of noreturn attributes for pruning paths. |
| 247 | void rdar_6777003_noret() __attribute__((noreturn)); |
| 248 | void rdar_6777003_analyzer_noret() __attribute__((analyzer_noreturn)); |
| 249 | |
| 250 | void rdar_6777003(int x) { |
| 251 | int *p = 0; |
| 252 | |
| 253 | if (x == 1) { |
| 254 | rdar_6777003_noret(); |
| 255 | *p = 1; // no-warning; |
| 256 | } |
| 257 | |
| 258 | if (x == 2) { |
| 259 | rdar_6777003_analyzer_noret(); |
| 260 | *p = 1; // no-warning; |
| 261 | } |
| 262 | |
| 263 | *p = 1; // expected-warning{{Dereference of null pointer}} |
| 264 | } |
| 265 | |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 266 | // For pointer arithmetic, --/++ should be treated as preserving non-nullness, |
| 267 | // regardless of how well the underlying StoreManager reasons about pointer |
| 268 | // arithmetic. |
| 269 | // <rdar://problem/6777209> |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 270 | void rdar_6777209(char *p) { |
| 271 | if (p == 0) |
| 272 | return; |
| 273 | |
| 274 | ++p; |
| 275 | |
| 276 | // This branch should always be infeasible. |
| 277 | if (p == 0) |
| 278 | *p = 'c'; // no-warning |
| 279 | } |
Ted Kremenek | b3cfd58 | 2009-04-23 17:49:43 +0000 | [diff] [blame] | 280 | |
| 281 | // PR 4033. A symbolic 'void *' pointer can be used as the address for a |
| 282 | // computed goto. |
| 283 | typedef void *Opcode; |
| 284 | Opcode pr_4033_getOpcode(); |
| 285 | void pr_4033(void) { |
| 286 | next_opcode: |
| 287 | { |
| 288 | Opcode op = pr_4033_getOpcode(); |
| 289 | if (op) goto *op; |
| 290 | } |
| 291 | } |
| 292 | |
Ted Kremenek | 956a37d | 2009-05-01 23:35:18 +0000 | [diff] [blame] | 293 | // Test invalidating pointers-to-pointers with slightly different types. This |
| 294 | // example came from a recent false positive due to a regression where the |
| 295 | // branch condition was falsely reported as being uninitialized. |
| 296 | void invalidate_by_ref(char **x); |
| 297 | int test_invalidate_by_ref() { |
| 298 | unsigned short y; |
| 299 | invalidate_by_ref((char**) &y); |
| 300 | if (y) // no-warning |
| 301 | return 1; |
| 302 | return 0; |
| 303 | } |
| 304 | |
Ted Kremenek | ed47fc6 | 2009-07-03 00:10:50 +0000 | [diff] [blame] | 305 | // Test for <rdar://problem/7027684>. This just tests that the CFG is |
| 306 | // constructed correctly. Previously, the successor block of the entrance |
| 307 | // was the block containing the merge for '?', which would trigger an |
| 308 | // assertion failure. |
| 309 | int rdar_7027684_aux(); |
| 310 | int rdar_7027684_aux_2() __attribute__((noreturn)); |
| 311 | void rdar_7027684(int x, int y) { |
| 312 | {}; // this empty compound statement is critical. |
| 313 | (rdar_7027684_aux() ? rdar_7027684_aux_2() : (void) 0); |
| 314 | } |
| 315 | |
Ted Kremenek | 411af40 | 2009-07-06 22:23:45 +0000 | [diff] [blame] | 316 | // Test that we handle casts of string literals to arbitrary types. |
| 317 | unsigned const char *string_literal_test1() { |
| 318 | return (const unsigned char*) "hello"; |
| 319 | } |
| 320 | |
| 321 | const float *string_literal_test2() { |
| 322 | return (const float*) "hello"; |
| 323 | } |
| 324 | |
Ted Kremenek | 169077d | 2009-07-06 23:47:19 +0000 | [diff] [blame] | 325 | // Test that we handle casts *from* incomplete struct types. |
| 326 | extern const struct _FooAssertStruct _cmd; |
| 327 | void test_cast_from_incomplete_struct_aux(volatile const void *x); |
| 328 | void test_cast_from_incomplete_struct() { |
| 329 | test_cast_from_incomplete_struct_aux(&_cmd); |
| 330 | } |
Ted Kremenek | ed47fc6 | 2009-07-03 00:10:50 +0000 | [diff] [blame] | 331 | |
Ted Kremenek | 5997888 | 2009-07-08 22:42:46 +0000 | [diff] [blame] | 332 | // Test for <rdar://problem/7034511> |
| 333 | // "ValueManager::makeIntVal(uint64_t X, QualType T) should return a 'Loc' |
| 334 | // when 'T' is a pointer" |
| 335 | // |
| 336 | // Previously this case would crash. |
| 337 | void test_rdar_7034511(NSArray *y) { |
| 338 | NSObject *x; |
| 339 | for (x in y) {} |
| 340 | if (x == ((void*) 0)) {} |
| 341 | } |
| 342 | |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 343 | // Handle casts of function pointers (CodeTextRegions) to arbitrary pointer |
| 344 | // types. This was previously causing a crash in CastRegion. |
| 345 | void handle_funcptr_voidptr_casts() { |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 346 | void **ptr; |
| 347 | typedef void *PVOID; |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 348 | typedef void *PCHAR; |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 349 | typedef long INT_PTR, *PINT_PTR; |
| 350 | typedef INT_PTR (*FARPROC)(); |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 351 | FARPROC handle_funcptr_voidptr_casts_aux(); |
| 352 | PVOID handle_funcptr_voidptr_casts_aux_2(PVOID volatile *x); |
| 353 | PVOID handle_funcptr_voidptr_casts_aux_3(PCHAR volatile *x); |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 354 | |
Ted Kremenek | 8d344ae | 2009-07-10 21:24:45 +0000 | [diff] [blame] | 355 | ptr = (void**) handle_funcptr_voidptr_casts_aux(); |
| 356 | handle_funcptr_voidptr_casts_aux_2(ptr); |
| 357 | handle_funcptr_voidptr_casts_aux_3(ptr); |
Ted Kremenek | 3f9811b | 2009-07-10 21:11:16 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Ted Kremenek | 31ef2b6 | 2009-07-10 21:43:30 +0000 | [diff] [blame] | 360 | // RegionStore::Retrieve previously crashed on this example. This example |
| 361 | // was previously in the test file 'xfail_regionstore_wine_crash.c'. |
| 362 | void testA() { |
| 363 | long x = 0; |
| 364 | char *y = (char *) &x; |
| 365 | if (!*y) |
| 366 | return; |
| 367 | } |
| 368 | |
Ted Kremenek | 43d74a5 | 2009-07-11 04:38:49 +0000 | [diff] [blame] | 369 | // RegionStoreManager previously crashed on this example. The problem is that |
| 370 | // the value bound to the field of b->grue after the call to testB_aux is |
| 371 | // a symbolic region. The second '*__gruep__' involves performing a load |
| 372 | // from a 'int*' that really is a 'void**'. The loaded location must be |
| 373 | // implicitly converted to an integer that wraps a location. Previosly we would |
| 374 | // get a crash here due to an assertion failure. |
| 375 | typedef struct _BStruct { void *grue; } BStruct; |
| 376 | void testB_aux(void *ptr); |
| 377 | void testB(BStruct *b) { |
| 378 | { |
| 379 | int *__gruep__ = ((int *)&((b)->grue)); |
| 380 | int __gruev__ = *__gruep__; |
| 381 | testB_aux(__gruep__); |
| 382 | } |
| 383 | { |
| 384 | int *__gruep__ = ((int *)&((b)->grue)); |
| 385 | int __gruev__ = *__gruep__; |
| 386 | if (~0 != __gruev__) {} |
| 387 | } |
| 388 | } |
| 389 | |
Ted Kremenek | 54ca9b1 | 2009-07-13 21:55:12 +0000 | [diff] [blame] | 390 | void test_trivial_symbolic_comparison(int *x) { |
| 391 | int test_trivial_symbolic_comparison_aux(); |
| 392 | int a = test_trivial_symbolic_comparison_aux(); |
| 393 | int b = a; |
| 394 | if (a != b) { |
| 395 | int *p = 0; |
| 396 | *p = 0xDEADBEEF; // no-warning |
| 397 | } |
| 398 | |
| 399 | a = a == 1; |
| 400 | b = b == 1; |
| 401 | if (a != b) { |
| 402 | int *p = 0; |
| 403 | *p = 0xDEADBEEF; // no-warning |
| 404 | } |
| 405 | } |
| 406 | |
Ted Kremenek | fde2efe | 2009-07-15 22:09:25 +0000 | [diff] [blame] | 407 | // Test for: |
| 408 | // <rdar://problem/7062158> false positive null dereference due to |
| 409 | // BasicStoreManager not tracking *static* globals |
| 410 | // |
| 411 | // This just tests the proper tracking of symbolic values for globals (both |
| 412 | // static and non-static). |
| 413 | // |
| 414 | static int* x_rdar_7062158; |
| 415 | void rdar_7062158() { |
| 416 | int *current = x_rdar_7062158; |
| 417 | if (current == x_rdar_7062158) |
| 418 | return; |
| 419 | |
| 420 | int *p = 0; |
| 421 | *p = 0xDEADBEEF; // no-warning |
| 422 | } |
| 423 | |
| 424 | int* x_rdar_7062158_2; |
| 425 | void rdar_7062158_2() { |
| 426 | int *current = x_rdar_7062158_2; |
| 427 | if (current == x_rdar_7062158_2) |
| 428 | return; |
| 429 | |
| 430 | int *p = 0; |
| 431 | *p = 0xDEADBEEF; // no-warning |
| 432 | } |
| 433 | |
Ted Kremenek | 4653739 | 2009-07-16 01:33:37 +0000 | [diff] [blame] | 434 | // This test reproduces a case for a crash when analyzing ClamAV using |
| 435 | // RegionStoreManager (the crash doesn't exhibit in BasicStoreManager because |
| 436 | // it isn't doing anything smart about arrays). The problem is that on the |
| 437 | // second line, 'p = &p[i]', p is assigned an ElementRegion whose index |
| 438 | // is a 16-bit integer. On the third line, a new ElementRegion is created |
| 439 | // based on the previous region, but there the region uses a 32-bit integer, |
| 440 | // resulting in a clash of values (an assertion failure at best). We resolve |
| 441 | // this problem by implicitly converting index values to 'int' when the |
| 442 | // ElementRegion is created. |
| 443 | unsigned char test_array_index_bitwidth(const unsigned char *p) { |
| 444 | unsigned short i = 0; |
| 445 | for (i = 0; i < 2; i++) p = &p[i]; |
| 446 | return p[i+1]; |
| 447 | } |
| 448 | |
Ted Kremenek | 63b9cfe | 2009-07-18 06:27:51 +0000 | [diff] [blame] | 449 | // This case tests that CastRegion handles casts involving BlockPointerTypes. |
| 450 | // It should not crash. |
| 451 | void test_block_cast() { |
| 452 | id test_block_cast_aux(); |
| 453 | (void (^)(void *))test_block_cast_aux(); // expected-warning{{expression result unused}} |
| 454 | } |
| 455 | |
Ted Kremenek | 293769a | 2009-07-20 21:00:55 +0000 | [diff] [blame] | 456 | // Test comparison of 'id' instance variable to a null void* constant after |
| 457 | // performing an OSAtomicCompareAndSwap32Barrier. |
| 458 | // This previously was a crash in RegionStoreManager. |
| 459 | @interface TestIdNull { |
| 460 | id x; |
| 461 | } |
| 462 | -(int)foo; |
| 463 | @end |
| 464 | @implementation TestIdNull |
| 465 | -(int)foo { |
| 466 | OSAtomicCompareAndSwap32Barrier(0, (signed)2, (signed*)&x); |
| 467 | if (x == (void*) 0) { return 0; } |
| 468 | return 1; |
| 469 | } |
| 470 | @end |
| 471 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 472 | // PR 4594 - This was a crash when handling casts in SimpleSValuator. |
| 473 | void PR4594() { |
| 474 | char *buf[1]; |
| 475 | char **foo = buf; |
| 476 | *foo = "test"; |
| 477 | } |
Ted Kremenek | 0b331e3 | 2009-07-22 04:23:20 +0000 | [diff] [blame] | 478 | |
| 479 | // Test invalidation logic where an integer is casted to an array with a |
| 480 | // different sign and then invalidated. |
| 481 | void test_invalidate_cast_int() { |
| 482 | void test_invalidate_cast_int_aux(unsigned *i); |
| 483 | signed i; |
| 484 | test_invalidate_cast_int_aux((unsigned*) &i); |
| 485 | if (i < 0) |
| 486 | return; |
| 487 | } |
| 488 | |
Ted Kremenek | 19e1f0b | 2009-08-01 06:17:29 +0000 | [diff] [blame] | 489 | // Reduced from a crash involving the cast of an Objective-C symbolic region to |
| 490 | // 'char *' |
| 491 | static NSNumber *test_ivar_offset(id self, SEL _cmd, Ivar inIvar) { |
| 492 | return [[[NSNumber allocWithZone:((void*)0)] initWithBool:*(_Bool *)((char *)self + ivar_getOffset(inIvar))] autorelease]; |
| 493 | } |
Ted Kremenek | 9a108eb | 2009-08-02 04:12:53 +0000 | [diff] [blame] | 494 | |
| 495 | // Reduced from a crash in StoreManager::CastRegion involving a divide-by-zero. |
| 496 | // This resulted from not properly handling region casts to 'const void*'. |
| 497 | void test_cast_const_voidptr() { |
| 498 | char x[10]; |
| 499 | char *p = &x[1]; |
| 500 | const void* q = p; |
| 501 | } |
Ted Kremenek | 968f0a6 | 2009-08-03 21:41:46 +0000 | [diff] [blame] | 502 | |
| 503 | // Reduced from a crash when analyzing Wine. This test handles loads from |
| 504 | // function addresses. |
| 505 | typedef long (*FARPROC)(); |
| 506 | FARPROC test_load_func(FARPROC origfun) { |
| 507 | if (!*(unsigned char*) origfun) |
| 508 | return origfun; |
| 509 | return 0; |
| 510 | } |
Ted Kremenek | 48775d5 | 2009-08-03 23:22:53 +0000 | [diff] [blame^] | 511 | |
| 512 | // Test passing-by-value an initialized struct variable. |
| 513 | struct test_pass_val { |
| 514 | int x; |
| 515 | int y; |
| 516 | }; |
| 517 | void test_pass_val_aux(struct test_pass_val s); |
| 518 | void test_pass_val() { |
| 519 | struct test_pass_val s; |
| 520 | s.x = 1; |
| 521 | // s.y = 2; |
| 522 | test_pass_val_aux(s); |
| 523 | } |
| 524 | |