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