blob: 48d1111a601e4e9f4174216f64afe26d23d46cd5 [file] [log] [blame]
Ted Kremeneke0a58072009-09-18 22:37:37 +00001// NOTE: Use '-fobjc-gc' to test the analysis being run twice, and multiple reports are not issued.
2// RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic -fobjc-gc -analyzer-constraints=basic --verify -fblocks %s &&
Ted Kremenek921109a2009-07-01 23:19:52 +00003// RUN: clang-cc -analyze -checker-cfref --analyzer-store=basic -analyzer-constraints=range --verify -fblocks %s &&
4// RUN: clang-cc -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=basic --verify -fblocks %s &&
5// RUN: clang-cc -analyze -checker-cfref --analyzer-store=region -analyzer-constraints=range --verify -fblocks %s
Ted Kremenek2dabd422009-01-22 18:53:15 +00006
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00007typedef struct objc_ivar *Ivar;
Ted Kremenekf684d562009-03-05 18:08:28 +00008typedef struct objc_selector *SEL;
9typedef signed char BOOL;
10typedef int NSInteger;
11typedef unsigned int NSUInteger;
12typedef struct _NSZone NSZone;
Ted Kremenek59978882009-07-08 22:42:46 +000013@class NSInvocation, NSArray, NSMethodSignature, NSCoder, NSString, NSEnumerator;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000014@protocol NSObject
15- (BOOL)isEqual:(id)object;
16- (id)autorelease;
17@end
18@protocol NSCopying
19- (id)copyWithZone:(NSZone *)zone;
20@end
Ted Kremenekf684d562009-03-05 18:08:28 +000021@protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000022@protocol NSCoding
23- (void)encodeWithCoder:(NSCoder *)aCoder;
24@end
25@interface NSObject <NSObject> {}
26- (id)init;
27+ (id)allocWithZone:(NSZone *)zone;
28@end
Ted Kremenekf684d562009-03-05 18:08:28 +000029extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
30@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
31- (NSUInteger)length;
32+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
33@end extern NSString * const NSBundleDidLoadNotification;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000034@interface NSValue : NSObject <NSCopying, NSCoding>
35- (void)getValue:(void *)value;
36@end
37@interface NSNumber : NSValue
38- (char)charValue;
39- (id)initWithBool:(BOOL)value;
40@end
Ted Kremenekf684d562009-03-05 18:08:28 +000041@interface NSAssertionHandler : NSObject {}
42+ (NSAssertionHandler *)currentHandler;
43- (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...;
44@end
45extern NSString * const NSConnectionReplyMode;
Ted Kremenek693de5d2009-03-23 15:42:58 +000046typedef float CGFloat;
47typedef struct _NSPoint {
48 CGFloat x;
49 CGFloat y;
50} NSPoint;
51typedef struct _NSSize {
52 CGFloat width;
53 CGFloat height;
54} NSSize;
55typedef struct _NSRect {
56 NSPoint origin;
57 NSSize size;
58} NSRect;
Ted Kremenek9f67ede2008-10-01 05:05:46 +000059
60// Reduced test case from crash in <rdar://problem/6253157>
Ted Kremenek9f67ede2008-10-01 05:05:46 +000061@interface A @end
62@implementation A
63- (void)foo:(void (^)(NSObject *x))block {
64 if (!((block != ((void *)0)))) {}
65}
66@end
67
Ted Kremenek6dfe2f52008-10-18 22:20:20 +000068// Reduced test case from crash in PR 2796;
69// http://llvm.org/bugs/show_bug.cgi?id=2796
70
71unsigned foo(unsigned x) { return __alignof__((x)) + sizeof(x); }
Ted Kremenek9253b0f2008-10-20 23:14:31 +000072
73// Improvement to path-sensitivity involving compound assignments.
74// Addresses false positive in <rdar://problem/6268365>
75//
76
77unsigned r6268365Aux();
78
79void r6268365() {
80 unsigned x = 0;
81 x &= r6268365Aux();
82 unsigned j = 0;
83
84 if (x == 0) ++j;
85 if (x == 0) x = x / j; // no-warning
86}
87
Ted Kremenekc13b6e22008-10-20 23:40:25 +000088void divzeroassume(unsigned x, unsigned j) {
89 x /= j;
90 if (j == 0) x /= 0; // no-warning
91 if (j == 0) x /= j; // no-warning
92 if (j == 0) x = x / 0; // no-warning
93}
94
95void divzeroassumeB(unsigned x, unsigned j) {
96 x = x / j;
97 if (j == 0) x /= 0; // no-warning
98 if (j == 0) x /= j; // no-warning
99 if (j == 0) x = x / 0; // no-warning
100}
101
Ted Kremenek76dba7b2008-11-13 05:05:34 +0000102// InitListExpr processing
103
104typedef float __m128 __attribute__((__vector_size__(16), __may_alias__));
105__m128 return128() {
Ted Kremenek062e2f92008-11-13 06:10:40 +0000106 // This compound literal has a Vector type. We currently just
107 // return UnknownVal.
Ted Kremenek76dba7b2008-11-13 05:05:34 +0000108 return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f };
109}
110
Ted Kremenek062e2f92008-11-13 06:10:40 +0000111typedef long long __v2di __attribute__ ((__vector_size__ (16)));
112typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__));
113__m128i vec128i(long long __q1, long long __q0) {
114 // This compound literal returns true for both isVectorType() and
115 // isIntegerType().
116 return __extension__ (__m128i)(__v2di){ __q0, __q1 };
117}
118
Ted Kremenek8322d6a2008-12-09 00:14:48 +0000119// Zero-sized VLAs.
120void check_zero_sized_VLA(int x) {
121 if (x)
122 return;
123
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000124 int vla[x]; // expected-warning{{Variable-length array 'vla' has zero elements (undefined behavior)}}
Ted Kremenek159d2482008-12-09 00:44:16 +0000125}
126
127void check_uninit_sized_VLA() {
128 int x;
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000129 int vla[x]; // expected-warning{{Variable-length array 'vla' garbage value for array size}}
Ted Kremenek8322d6a2008-12-09 00:14:48 +0000130}
Ted Kremenek062e2f92008-11-13 06:10:40 +0000131
Ted Kremenek55f7bcb2008-12-15 18:51:00 +0000132// sizeof(void)
133// - Tests a regression reported in PR 3211: http://llvm.org/bugs/show_bug.cgi?id=3211
134void handle_sizeof_void(unsigned flag) {
135 int* p = 0;
136
137 if (flag) {
138 if (sizeof(void) == 1)
139 return;
140 // Infeasible.
141 *p = 1; // no-warning
142 }
143
144 void* q;
145
146 if (!flag) {
147 if (sizeof(*q) == 1)
148 return;
149 // Infeasibe.
150 *p = 1; // no-warning
151 }
152
153 // Infeasible.
154 *p = 1; // no-warning
155}
156
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000157// PR 3422
158void pr3422_helper(char *p);
159void pr3422() {
160 char buf[100];
161 char *q = &buf[10];
162 pr3422_helper(&q[1]);
163}
164
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000165// PR 3543 (handle empty statement expressions)
Mike Stump006105d2009-07-21 18:59:16 +0000166void pr_3543(void) {
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000167 ({});
168}
169
Ted Kremenek265a3052009-02-24 02:23:11 +0000170// <rdar://problem/6611677>
171// This test case test the use of a vector type within an array subscript
172// expression.
173typedef long long __a64vector __attribute__((__vector_size__(8)));
174typedef long long __a128vector __attribute__((__vector_size__(16)));
175static inline __a64vector __attribute__((__always_inline__, __nodebug__))
176my_test_mm_movepi64_pi64(__a128vector a) {
177 return (__a64vector)a[0];
178}
179
Ted Kremenekf684d562009-03-05 18:08:28 +0000180// Test basic tracking of ivars associated with 'self'.
181@interface SelfIvarTest : NSObject {
182 int flag;
183}
184- (void)test_self_tracking;
185@end
186
187@implementation SelfIvarTest
188- (void)test_self_tracking {
189 char *p = 0;
190 char c;
191
192 if (flag)
193 p = "hello";
194
195 if (flag)
196 c = *p; // no-warning
197}
198@end
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000199
Ted Kremenek7de20fe2009-03-11 02:29:48 +0000200// PR 3770
201char pr3770(int x) {
202 int y = x & 0x2;
203 char *p = 0;
204 if (y == 1)
205 p = "hello";
206
207 if (y == 1)
208 return p[0]; // no-warning
209
210 return 'a';
211}
212
Ted Kremenek344d4c82009-03-11 18:17:16 +0000213// PR 3772
Ted Kremenekfa6228d2009-03-11 02:52:39 +0000214// - We just want to test that this doesn't crash the analyzer.
215typedef struct st ST;
216struct st { char *name; };
217extern ST *Cur_Pu;
218
Ted Kremenek344d4c82009-03-11 18:17:16 +0000219void pr3772(void)
Ted Kremenekfa6228d2009-03-11 02:52:39 +0000220{
221 static ST *last_Cur_Pu;
222 if (last_Cur_Pu == Cur_Pu) {
223 return;
224 }
225}
226
Ted Kremenek344d4c82009-03-11 18:17:16 +0000227// PR 3780 - This tests that StmtIterator isn't broken for VLAs in DeclGroups.
228void pr3780(int sz) { typedef double MAT[sz][sz]; }
Ted Kremenekfa6228d2009-03-11 02:52:39 +0000229
Ted Kremenekec099f12009-03-18 22:10:22 +0000230// <rdar://problem/6695527> - Test that we don't symbolicate doubles before
231// we are ready to do something with them.
232int rdar6695527(double x) {
233 if (!x) { return 0; }
234 return 1;
235}
Ted Kremenek693de5d2009-03-23 15:42:58 +0000236
237// <rdar://problem/6708148> - Test that we properly invalidate structs
238// passed-by-reference to a function.
239void pr6708148_invalidate(NSRect *x);
240void pr6708148_use(NSRect x);
241void pr6708148_test(void) {
242 NSRect x;
243 pr6708148_invalidate(&x);
244 pr6708148_use(x); // no-warning
245}
246
Ted Kremenekb7252322009-04-10 00:01:14 +0000247// Handle both kinds of noreturn attributes for pruning paths.
248void rdar_6777003_noret() __attribute__((noreturn));
249void rdar_6777003_analyzer_noret() __attribute__((analyzer_noreturn));
250
251void rdar_6777003(int x) {
252 int *p = 0;
253
254 if (x == 1) {
255 rdar_6777003_noret();
256 *p = 1; // no-warning;
257 }
258
259 if (x == 2) {
260 rdar_6777003_analyzer_noret();
261 *p = 1; // no-warning;
262 }
263
264 *p = 1; // expected-warning{{Dereference of null pointer}}
265}
266
Ted Kremenekaf48fdd2009-04-21 22:38:05 +0000267// For pointer arithmetic, --/++ should be treated as preserving non-nullness,
268// regardless of how well the underlying StoreManager reasons about pointer
269// arithmetic.
270// <rdar://problem/6777209>
Ted Kremenekaf48fdd2009-04-21 22:38:05 +0000271void rdar_6777209(char *p) {
272 if (p == 0)
273 return;
274
275 ++p;
276
277 // This branch should always be infeasible.
278 if (p == 0)
279 *p = 'c'; // no-warning
280}
Ted Kremenekb3cfd582009-04-23 17:49:43 +0000281
282// PR 4033. A symbolic 'void *' pointer can be used as the address for a
283// computed goto.
284typedef void *Opcode;
285Opcode pr_4033_getOpcode();
286void pr_4033(void) {
287next_opcode:
288 {
289 Opcode op = pr_4033_getOpcode();
290 if (op) goto *op;
291 }
292}
293
Ted Kremenek956a37d2009-05-01 23:35:18 +0000294// Test invalidating pointers-to-pointers with slightly different types. This
295// example came from a recent false positive due to a regression where the
296// branch condition was falsely reported as being uninitialized.
297void invalidate_by_ref(char **x);
298int test_invalidate_by_ref() {
299 unsigned short y;
300 invalidate_by_ref((char**) &y);
301 if (y) // no-warning
302 return 1;
303 return 0;
304}
305
Ted Kremeneked47fc62009-07-03 00:10:50 +0000306// Test for <rdar://problem/7027684>. This just tests that the CFG is
307// constructed correctly. Previously, the successor block of the entrance
308// was the block containing the merge for '?', which would trigger an
309// assertion failure.
310int rdar_7027684_aux();
311int rdar_7027684_aux_2() __attribute__((noreturn));
312void rdar_7027684(int x, int y) {
313 {}; // this empty compound statement is critical.
314 (rdar_7027684_aux() ? rdar_7027684_aux_2() : (void) 0);
315}
316
Ted Kremenek411af402009-07-06 22:23:45 +0000317// Test that we handle casts of string literals to arbitrary types.
318unsigned const char *string_literal_test1() {
319 return (const unsigned char*) "hello";
320}
321
322const float *string_literal_test2() {
323 return (const float*) "hello";
324}
325
Ted Kremenek169077d2009-07-06 23:47:19 +0000326// Test that we handle casts *from* incomplete struct types.
327extern const struct _FooAssertStruct _cmd;
328void test_cast_from_incomplete_struct_aux(volatile const void *x);
329void test_cast_from_incomplete_struct() {
330 test_cast_from_incomplete_struct_aux(&_cmd);
331}
Ted Kremeneked47fc62009-07-03 00:10:50 +0000332
Ted Kremenek59978882009-07-08 22:42:46 +0000333// Test for <rdar://problem/7034511>
334// "ValueManager::makeIntVal(uint64_t X, QualType T) should return a 'Loc'
335// when 'T' is a pointer"
336//
337// Previously this case would crash.
338void test_rdar_7034511(NSArray *y) {
339 NSObject *x;
340 for (x in y) {}
341 if (x == ((void*) 0)) {}
342}
343
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000344// Handle casts of function pointers (CodeTextRegions) to arbitrary pointer
345// types. This was previously causing a crash in CastRegion.
346void handle_funcptr_voidptr_casts() {
Ted Kremenek3f9811b2009-07-10 21:11:16 +0000347 void **ptr;
348 typedef void *PVOID;
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000349 typedef void *PCHAR;
Ted Kremenek3f9811b2009-07-10 21:11:16 +0000350 typedef long INT_PTR, *PINT_PTR;
351 typedef INT_PTR (*FARPROC)();
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000352 FARPROC handle_funcptr_voidptr_casts_aux();
353 PVOID handle_funcptr_voidptr_casts_aux_2(PVOID volatile *x);
354 PVOID handle_funcptr_voidptr_casts_aux_3(PCHAR volatile *x);
Ted Kremenek3f9811b2009-07-10 21:11:16 +0000355
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000356 ptr = (void**) handle_funcptr_voidptr_casts_aux();
357 handle_funcptr_voidptr_casts_aux_2(ptr);
358 handle_funcptr_voidptr_casts_aux_3(ptr);
Ted Kremenek3f9811b2009-07-10 21:11:16 +0000359}
360
Ted Kremenek31ef2b62009-07-10 21:43:30 +0000361// RegionStore::Retrieve previously crashed on this example. This example
362// was previously in the test file 'xfail_regionstore_wine_crash.c'.
363void testA() {
364 long x = 0;
365 char *y = (char *) &x;
366 if (!*y)
367 return;
368}
369
Ted Kremenek43d74a52009-07-11 04:38:49 +0000370// RegionStoreManager previously crashed on this example. The problem is that
371// the value bound to the field of b->grue after the call to testB_aux is
372// a symbolic region. The second '*__gruep__' involves performing a load
373// from a 'int*' that really is a 'void**'. The loaded location must be
374// implicitly converted to an integer that wraps a location. Previosly we would
375// get a crash here due to an assertion failure.
376typedef struct _BStruct { void *grue; } BStruct;
377void testB_aux(void *ptr);
378void testB(BStruct *b) {
379 {
380 int *__gruep__ = ((int *)&((b)->grue));
381 int __gruev__ = *__gruep__;
382 testB_aux(__gruep__);
383 }
384 {
385 int *__gruep__ = ((int *)&((b)->grue));
386 int __gruev__ = *__gruep__;
387 if (~0 != __gruev__) {}
388 }
389}
390
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000391void test_trivial_symbolic_comparison(int *x) {
392 int test_trivial_symbolic_comparison_aux();
393 int a = test_trivial_symbolic_comparison_aux();
394 int b = a;
395 if (a != b) {
396 int *p = 0;
397 *p = 0xDEADBEEF; // no-warning
398 }
399
400 a = a == 1;
401 b = b == 1;
402 if (a != b) {
403 int *p = 0;
404 *p = 0xDEADBEEF; // no-warning
405 }
406}
407
Ted Kremenekfde2efe2009-07-15 22:09:25 +0000408// Test for:
409// <rdar://problem/7062158> false positive null dereference due to
410// BasicStoreManager not tracking *static* globals
411//
412// This just tests the proper tracking of symbolic values for globals (both
413// static and non-static).
414//
415static int* x_rdar_7062158;
416void rdar_7062158() {
417 int *current = x_rdar_7062158;
418 if (current == x_rdar_7062158)
419 return;
420
421 int *p = 0;
422 *p = 0xDEADBEEF; // no-warning
423}
424
425int* x_rdar_7062158_2;
426void rdar_7062158_2() {
427 int *current = x_rdar_7062158_2;
428 if (current == x_rdar_7062158_2)
429 return;
430
431 int *p = 0;
432 *p = 0xDEADBEEF; // no-warning
433}
434
Ted Kremenek46537392009-07-16 01:33:37 +0000435// This test reproduces a case for a crash when analyzing ClamAV using
436// RegionStoreManager (the crash doesn't exhibit in BasicStoreManager because
437// it isn't doing anything smart about arrays). The problem is that on the
438// second line, 'p = &p[i]', p is assigned an ElementRegion whose index
439// is a 16-bit integer. On the third line, a new ElementRegion is created
440// based on the previous region, but there the region uses a 32-bit integer,
441// resulting in a clash of values (an assertion failure at best). We resolve
442// this problem by implicitly converting index values to 'int' when the
443// ElementRegion is created.
444unsigned char test_array_index_bitwidth(const unsigned char *p) {
445 unsigned short i = 0;
446 for (i = 0; i < 2; i++) p = &p[i];
447 return p[i+1];
448}
449
Ted Kremenek63b9cfe2009-07-18 06:27:51 +0000450// This case tests that CastRegion handles casts involving BlockPointerTypes.
451// It should not crash.
452void test_block_cast() {
453 id test_block_cast_aux();
454 (void (^)(void *))test_block_cast_aux(); // expected-warning{{expression result unused}}
455}
456
Ted Kremenek293769a2009-07-20 21:00:55 +0000457// Test comparison of 'id' instance variable to a null void* constant after
458// performing an OSAtomicCompareAndSwap32Barrier.
459// This previously was a crash in RegionStoreManager.
460@interface TestIdNull {
461 id x;
462}
463-(int)foo;
464@end
465@implementation TestIdNull
466-(int)foo {
467 OSAtomicCompareAndSwap32Barrier(0, (signed)2, (signed*)&x);
468 if (x == (void*) 0) { return 0; }
469 return 1;
470}
471@end
472
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000473// PR 4594 - This was a crash when handling casts in SimpleSValuator.
474void PR4594() {
475 char *buf[1];
476 char **foo = buf;
477 *foo = "test";
478}
Ted Kremenek0b331e32009-07-22 04:23:20 +0000479
480// Test invalidation logic where an integer is casted to an array with a
481// different sign and then invalidated.
482void test_invalidate_cast_int() {
483 void test_invalidate_cast_int_aux(unsigned *i);
484 signed i;
485 test_invalidate_cast_int_aux((unsigned*) &i);
486 if (i < 0)
487 return;
488}
489
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000490// Reduced from a crash involving the cast of an Objective-C symbolic region to
491// 'char *'
492static NSNumber *test_ivar_offset(id self, SEL _cmd, Ivar inIvar) {
493 return [[[NSNumber allocWithZone:((void*)0)] initWithBool:*(_Bool *)((char *)self + ivar_getOffset(inIvar))] autorelease];
494}
Ted Kremenek9a108eb2009-08-02 04:12:53 +0000495
496// Reduced from a crash in StoreManager::CastRegion involving a divide-by-zero.
497// This resulted from not properly handling region casts to 'const void*'.
498void test_cast_const_voidptr() {
499 char x[10];
500 char *p = &x[1];
501 const void* q = p;
502}
Ted Kremenek968f0a62009-08-03 21:41:46 +0000503
504// Reduced from a crash when analyzing Wine. This test handles loads from
505// function addresses.
506typedef long (*FARPROC)();
507FARPROC test_load_func(FARPROC origfun) {
508 if (!*(unsigned char*) origfun)
509 return origfun;
510 return 0;
511}
Ted Kremenek48775d52009-08-03 23:22:53 +0000512
513// Test passing-by-value an initialized struct variable.
514struct test_pass_val {
515 int x;
516 int y;
517};
518void test_pass_val_aux(struct test_pass_val s);
519void test_pass_val() {
520 struct test_pass_val s;
521 s.x = 1;
Ted Kremenek8eec7c02009-08-04 00:58:45 +0000522 s.y = 2;
Ted Kremenek48775d52009-08-03 23:22:53 +0000523 test_pass_val_aux(s);
524}
525
Ted Kremenek4ed45982009-08-05 05:31:02 +0000526// This is a reduced test case of a false positive that previously appeared
527// in RegionStoreManager. Previously the array access resulted in dereferencing
528// an undefined value.
529int test_array_compound(int *q, int *r, int *z) {
530 int *array[] = { q, r, z };
531 int j = 0;
532 for (unsigned i = 0; i < 3 ; ++i)
533 if (*array[i]) ++j; // no-warning
534 return j;
535}
536
Ted Kremenek1894dce2009-08-25 20:51:30 +0000537// This test case previously crashed with -analyzer-store=basic because the
538// symbolic value stored in 'x' wouldn't be implicitly casted to a signed value
539// during the comparison.
540int rdar_7124210(unsigned int x) {
541 enum { SOME_CONSTANT = 123 };
542 int compare = ((signed) SOME_CONSTANT) == *((signed *) &x);
543 return compare ? 0 : 1; // Forces the evaluation of the symbolic constraint.
544}
545
Ted Kremenekab2f43c2009-08-25 23:29:04 +0000546void pr4781(unsigned long *raw1) {
547 unsigned long *cook, *raw0;
548 unsigned long dough[32];
549 int i;
550 cook = dough;
551 for( i = 0; i < 16; i++, raw1++ ) {
552 raw0 = raw1++;
553 *cook = (*raw0 & 0x00fc0000L) << 6;
554 *cook |= (*raw0 & 0x00000fc0L) << 10;
555 }
556}
557
Ted Kremenekcfcd7fd2009-09-09 20:36:12 +0000558// <rdar://problem/7185647> - 'self' should be treated as being non-null
559// upon entry to an objective-c method.
560@interface RDar7185647
561- (id)foo;
562@end
563@implementation RDar7185647
564- (id) foo {
565 if (self)
566 return self;
567 *((int *) 0x0) = 0xDEADBEEF; // no-warning
568 return self;
569}
570@end
Ted Kremeneke2b57442009-09-15 00:40:32 +0000571
572// Test reasoning of __builtin_offsetof;
573struct test_offsetof_A {
574 int x;
575 int y;
576};
577struct test_offsetof_B {
578 int w;
579 int z;
580};
581void test_offsetof_1() {
582 if (__builtin_offsetof(struct test_offsetof_A, x) ==
583 __builtin_offsetof(struct test_offsetof_B, w))
584 return;
585 int *p = 0;
586 *p = 0xDEADBEEF; // no-warning
587}
588void test_offsetof_2() {
589 if (__builtin_offsetof(struct test_offsetof_A, y) ==
590 __builtin_offsetof(struct test_offsetof_B, z))
591 return;
592 int *p = 0;
593 *p = 0xDEADBEEF; // no-warning
594}
595void test_offsetof_3() {
596 if (__builtin_offsetof(struct test_offsetof_A, y) -
597 __builtin_offsetof(struct test_offsetof_A, x)
598 ==
599 __builtin_offsetof(struct test_offsetof_B, z) -
600 __builtin_offsetof(struct test_offsetof_B, w))
601 return;
602 int *p = 0;
603 *p = 0xDEADBEEF; // no-warning
604}
605void test_offsetof_4() {
606 if (__builtin_offsetof(struct test_offsetof_A, y) ==
607 __builtin_offsetof(struct test_offsetof_B, w))
608 return;
609 int *p = 0;
610 *p = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}}
611}
612
Ted Kremenekc32b2442009-09-21 23:22:11 +0000613// <rdar://problem/6829164> "nil receiver" false positive: make tracking
614// of the MemRegion for 'self' path-sensitive
615@interface RDar6829164 : NSObject {
616 double x; int y;
617}
618- (id) init;
619@end
620
621id rdar_6829164_1();
622double rdar_6829164_2();
623
624@implementation RDar6829164
625- (id) init {
626 if((self = [super init]) != 0) {
627 id z = rdar_6829164_1();
628 y = (z != 0);
629 if (y)
630 x = rdar_6829164_2();
631 }
632 return self;
633}
634@end
Ted Kremenekcc969fd2009-09-22 04:48:39 +0000635
636// <rdar://problem/7242015> - Invalidate values passed-by-reference
637// to functions when the pointer to the value is passed as an integer.
638void test_7242015_aux(unsigned long);
639int rdar_7242015() {
640 int x;
641 test_7242015_aux((unsigned long) &x); // no-warning
642 return x; // Previously we return and uninitialized value when
643 // using RegionStore.
644}
645
Ted Kremenekcf549592009-09-22 21:19:14 +0000646// <rdar://problem/7242006> [RegionStore] compound literal assignment with
647// floats not honored
648CGFloat rdar7242006(CGFloat x) {
649 NSSize y = (NSSize){x, 10};
650 return y.width; // no-warning
651}
652
Ted Kremenek657406d2009-09-23 01:30:01 +0000653// PR 4988 - This test exhibits a case where a function can be referenced
654// when not explicitly used in an "lvalue" context (as far as the analyzer is
655// concerned). This previously triggered a crash due to an invalid assertion.
656void pr_4988(void) {
657 pr_4988; // expected-warning{{expression result unused}}
658}
659
Ted Kremenek022a1252009-09-26 17:18:44 +0000660// <rdar://problem/7152418> - A 'signed char' is used as a flag, which is
661// implicitly converted to an int.
662void *rdar7152418_bar();
663@interface RDar7152418 {
664 signed char x;
665}
666-(char)foo;
667@end;
668@implementation RDar7152418
669-(char)foo {
670 char *p = 0;
671 void *result = 0;
672 if (x) {
673 result = rdar7152418_bar();
674 p = "hello";
675 }
676 if (!result) {
677 result = rdar7152418_bar();
678 if (result && x)
679 return *p; // no-warning
680 }
681 return 1;
682}
683
Ted Kremenekb1d04222009-10-06 03:44:49 +0000684// Test constant-folding of symbolic values, automatically handling type
685// conversions of the symbol as necessary. Previously this would crash
686// once we started eagerly evaluating symbols whose values were constrained
687// to a single value.
688void test_constant_symbol(signed char x) {
689 while (1) {
690 if (x == ((signed char) 0)) {}
691 }
692}
693
Ted Kremenekb5deae52009-10-16 20:46:24 +0000694// Test constant-folding of symbolic values, where a folded symbolic value is used in a
695// bitshift operation. This previously caused a crash because it triggered an assertion
696// in APSInt.
697void test_symbol_fold_with_shift(unsigned int * p, unsigned int n,
698 const unsigned int * grumpkin, unsigned int dn) {
699 unsigned int i;
700 unsigned int tempsub[8];
701 unsigned int *solgrumpkin = tempsub + n;
702 for (i = 0; i < n; i++)
703 solgrumpkin[i] = (i < dn) ? ~grumpkin[i] : 0xFFFFFFFF;
704 for (i <<= 5; i < (n << 5); i++) {}
705}
Ted Kremenekb1d04222009-10-06 03:44:49 +0000706