blob: ad7393b82d30eb5694a3ec807c72a5f0763d5865 [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.
Stephen Hines651f13c2014-04-23 16:59:28 -07002// RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,alpha.core,osx.cocoa.AtSync -analyzer-store=region -analyzer-constraints=range -verify -fblocks -Wno-unreachable-code -Wno-null-dereference -Wno-objc-root-class %s
3// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,alpha.core,osx.cocoa.AtSync -analyzer-store=region -analyzer-constraints=range -verify -fblocks -Wno-unreachable-code -Wno-null-dereference -Wno-objc-root-class %s
Ted Kremenek2dabd422009-01-22 18:53:15 +00004
Ted Kremenekf0af7772010-05-26 21:36:54 +00005#ifndef __clang_analyzer__
Dylan Noblesmithb44b9662011-12-18 18:07:42 +00006#error __clang_analyzer__ not defined
Ted Kremenekf0af7772010-05-26 21:36:54 +00007#endif
8
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00009typedef struct objc_ivar *Ivar;
Ted Kremenekf684d562009-03-05 18:08:28 +000010typedef struct objc_selector *SEL;
11typedef signed char BOOL;
12typedef int NSInteger;
13typedef unsigned int NSUInteger;
14typedef struct _NSZone NSZone;
Ted Kremenek59978882009-07-08 22:42:46 +000015@class NSInvocation, NSArray, NSMethodSignature, NSCoder, NSString, NSEnumerator;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000016@protocol NSObject
17- (BOOL)isEqual:(id)object;
18- (id)autorelease;
19@end
20@protocol NSCopying
21- (id)copyWithZone:(NSZone *)zone;
22@end
Ted Kremenekf684d562009-03-05 18:08:28 +000023@protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000024@protocol NSCoding
25- (void)encodeWithCoder:(NSCoder *)aCoder;
26@end
27@interface NSObject <NSObject> {}
28- (id)init;
29+ (id)allocWithZone:(NSZone *)zone;
30@end
Ted Kremenekf684d562009-03-05 18:08:28 +000031extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
32@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
33- (NSUInteger)length;
34+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;
35@end extern NSString * const NSBundleDidLoadNotification;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000036@interface NSValue : NSObject <NSCopying, NSCoding>
37- (void)getValue:(void *)value;
38@end
39@interface NSNumber : NSValue
40- (char)charValue;
41- (id)initWithBool:(BOOL)value;
42@end
Ted Kremenekf684d562009-03-05 18:08:28 +000043@interface NSAssertionHandler : NSObject {}
44+ (NSAssertionHandler *)currentHandler;
45- (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...;
46@end
47extern NSString * const NSConnectionReplyMode;
Ted Kremenek693de5d2009-03-23 15:42:58 +000048typedef float CGFloat;
49typedef struct _NSPoint {
50 CGFloat x;
51 CGFloat y;
52} NSPoint;
53typedef struct _NSSize {
54 CGFloat width;
55 CGFloat height;
56} NSSize;
57typedef struct _NSRect {
58 NSPoint origin;
59 NSSize size;
60} NSRect;
Ted Kremenek9f67ede2008-10-01 05:05:46 +000061
62// Reduced test case from crash in <rdar://problem/6253157>
Ted Kremenek9f67ede2008-10-01 05:05:46 +000063@interface A @end
64@implementation A
65- (void)foo:(void (^)(NSObject *x))block {
66 if (!((block != ((void *)0)))) {}
67}
68@end
69
Ted Kremenek6dfe2f52008-10-18 22:20:20 +000070// Reduced test case from crash in PR 2796;
71// http://llvm.org/bugs/show_bug.cgi?id=2796
72
73unsigned foo(unsigned x) { return __alignof__((x)) + sizeof(x); }
Ted Kremenek9253b0f2008-10-20 23:14:31 +000074
75// Improvement to path-sensitivity involving compound assignments.
76// Addresses false positive in <rdar://problem/6268365>
77//
78
79unsigned r6268365Aux();
80
81void r6268365() {
82 unsigned x = 0;
Stephen Hines651f13c2014-04-23 16:59:28 -070083 x &= r6268365Aux();
Ted Kremenek9253b0f2008-10-20 23:14:31 +000084 unsigned j = 0;
85
86 if (x == 0) ++j;
Stephen Hines651f13c2014-04-23 16:59:28 -070087 if (x == 0) x = x / j;
Ted Kremenek9253b0f2008-10-20 23:14:31 +000088}
89
Ted Kremenekc13b6e22008-10-20 23:40:25 +000090void divzeroassume(unsigned x, unsigned j) {
91 x /= j;
Chris Lattner7ef655a2010-01-12 21:23:57 +000092 if (j == 0) x /= 0; // no static-analyzer warning expected-warning {{division by zero is undefined}}
93 if (j == 0) x /= j; // no static-analyzer warning
94 if (j == 0) x = x / 0; // no static-analyzer warning expected-warning {{division by zero is undefined}}
Ted Kremenekc13b6e22008-10-20 23:40:25 +000095}
96
97void divzeroassumeB(unsigned x, unsigned j) {
98 x = x / j;
Chris Lattner7ef655a2010-01-12 21:23:57 +000099 if (j == 0) x /= 0; // no static-analyzer warning expected-warning {{division by zero is undefined}}
100 if (j == 0) x /= j; // no static-analyzer warning
101 if (j == 0) x = x / 0; // no static-analyzer warning expected-warning {{division by zero is undefined}}
Ted Kremenekc13b6e22008-10-20 23:40:25 +0000102}
103
Ted Kremenek76dba7b2008-11-13 05:05:34 +0000104// InitListExpr processing
105
106typedef float __m128 __attribute__((__vector_size__(16), __may_alias__));
107__m128 return128() {
Ted Kremenek062e2f92008-11-13 06:10:40 +0000108 // This compound literal has a Vector type. We currently just
109 // return UnknownVal.
Ted Kremenek76dba7b2008-11-13 05:05:34 +0000110 return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f };
111}
112
Ted Kremenek062e2f92008-11-13 06:10:40 +0000113typedef long long __v2di __attribute__ ((__vector_size__ (16)));
114typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__));
115__m128i vec128i(long long __q1, long long __q0) {
116 // This compound literal returns true for both isVectorType() and
117 // isIntegerType().
118 return __extension__ (__m128i)(__v2di){ __q0, __q1 };
119}
120
Ted Kremenek55f7bcb2008-12-15 18:51:00 +0000121// sizeof(void)
122// - Tests a regression reported in PR 3211: http://llvm.org/bugs/show_bug.cgi?id=3211
123void handle_sizeof_void(unsigned flag) {
124 int* p = 0;
125
126 if (flag) {
127 if (sizeof(void) == 1)
128 return;
129 // Infeasible.
130 *p = 1; // no-warning
131 }
132
133 void* q;
134
135 if (!flag) {
136 if (sizeof(*q) == 1)
137 return;
138 // Infeasibe.
139 *p = 1; // no-warning
140 }
141
142 // Infeasible.
143 *p = 1; // no-warning
144}
145
Ted Kremenek3f214b32009-10-29 05:33:39 +0000146// check deference of undefined values
147void check_deref_undef(void) {
148 int *p;
149 *p = 0xDEADBEEF; // expected-warning{{Dereference of undefined pointer value}}
150}
151
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000152// PR 3422
153void pr3422_helper(char *p);
154void pr3422() {
155 char buf[100];
156 char *q = &buf[10];
157 pr3422_helper(&q[1]);
158}
159
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000160// PR 3543 (handle empty statement expressions)
Mike Stump006105d2009-07-21 18:59:16 +0000161void pr_3543(void) {
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000162 ({});
163}
164
Ted Kremenek265a3052009-02-24 02:23:11 +0000165// <rdar://problem/6611677>
166// This test case test the use of a vector type within an array subscript
167// expression.
168typedef long long __a64vector __attribute__((__vector_size__(8)));
169typedef long long __a128vector __attribute__((__vector_size__(16)));
170static inline __a64vector __attribute__((__always_inline__, __nodebug__))
171my_test_mm_movepi64_pi64(__a128vector a) {
172 return (__a64vector)a[0];
173}
174
Ted Kremenekf684d562009-03-05 18:08:28 +0000175// Test basic tracking of ivars associated with 'self'.
176@interface SelfIvarTest : NSObject {
177 int flag;
178}
179- (void)test_self_tracking;
180@end
181
182@implementation SelfIvarTest
183- (void)test_self_tracking {
184 char *p = 0;
185 char c;
186
187 if (flag)
188 p = "hello";
189
190 if (flag)
191 c = *p; // no-warning
192}
193@end
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000194
Ted Kremenek7de20fe2009-03-11 02:29:48 +0000195// PR 3770
196char pr3770(int x) {
197 int y = x & 0x2;
198 char *p = 0;
199 if (y == 1)
200 p = "hello";
201
202 if (y == 1)
203 return p[0]; // no-warning
204
205 return 'a';
206}
207
Ted Kremenek344d4c82009-03-11 18:17:16 +0000208// PR 3772
Ted Kremenekfa6228d2009-03-11 02:52:39 +0000209// - We just want to test that this doesn't crash the analyzer.
210typedef struct st ST;
211struct st { char *name; };
212extern ST *Cur_Pu;
213
Ted Kremenek344d4c82009-03-11 18:17:16 +0000214void pr3772(void)
Ted Kremenekfa6228d2009-03-11 02:52:39 +0000215{
216 static ST *last_Cur_Pu;
217 if (last_Cur_Pu == Cur_Pu) {
218 return;
219 }
220}
221
Ted Kremenek344d4c82009-03-11 18:17:16 +0000222// PR 3780 - This tests that StmtIterator isn't broken for VLAs in DeclGroups.
223void pr3780(int sz) { typedef double MAT[sz][sz]; }
Ted Kremenekfa6228d2009-03-11 02:52:39 +0000224
Ted Kremenekec099f12009-03-18 22:10:22 +0000225// <rdar://problem/6695527> - Test that we don't symbolicate doubles before
226// we are ready to do something with them.
227int rdar6695527(double x) {
228 if (!x) { return 0; }
229 return 1;
230}
Ted Kremenek693de5d2009-03-23 15:42:58 +0000231
232// <rdar://problem/6708148> - Test that we properly invalidate structs
233// passed-by-reference to a function.
234void pr6708148_invalidate(NSRect *x);
235void pr6708148_use(NSRect x);
236void pr6708148_test(void) {
237 NSRect x;
238 pr6708148_invalidate(&x);
239 pr6708148_use(x); // no-warning
240}
241
Ted Kremenekb7252322009-04-10 00:01:14 +0000242// Handle both kinds of noreturn attributes for pruning paths.
243void rdar_6777003_noret() __attribute__((noreturn));
244void rdar_6777003_analyzer_noret() __attribute__((analyzer_noreturn));
245
246void rdar_6777003(int x) {
247 int *p = 0;
248
249 if (x == 1) {
250 rdar_6777003_noret();
251 *p = 1; // no-warning;
252 }
253
254 if (x == 2) {
255 rdar_6777003_analyzer_noret();
256 *p = 1; // no-warning;
257 }
258
259 *p = 1; // expected-warning{{Dereference of null pointer}}
260}
261
Anna Zaks2cbe7912011-12-20 22:35:30 +0000262// Check that the pointer-to-conts arguments do not get invalidated by Obj C
263// interfaces. radar://10595327
264int rdar_10595327(char *str) {
265 char fl = str[0];
266 int *p = 0;
267 NSString *s = [NSString stringWithUTF8String:str];
268 if (str[0] != fl)
269 return *p; // no-warning
270 return 0;
271}
272
Ted Kremenekaf48fdd2009-04-21 22:38:05 +0000273// For pointer arithmetic, --/++ should be treated as preserving non-nullness,
274// regardless of how well the underlying StoreManager reasons about pointer
275// arithmetic.
276// <rdar://problem/6777209>
Ted Kremenekaf48fdd2009-04-21 22:38:05 +0000277void rdar_6777209(char *p) {
278 if (p == 0)
279 return;
280
281 ++p;
282
283 // This branch should always be infeasible.
284 if (p == 0)
285 *p = 'c'; // no-warning
286}
Ted Kremenekb3cfd582009-04-23 17:49:43 +0000287
288// PR 4033. A symbolic 'void *' pointer can be used as the address for a
289// computed goto.
290typedef void *Opcode;
291Opcode pr_4033_getOpcode();
292void pr_4033(void) {
John McCallb60a77e2010-08-01 00:26:45 +0000293 void *lbl = &&next_opcode;
Ted Kremenekb3cfd582009-04-23 17:49:43 +0000294next_opcode:
295 {
296 Opcode op = pr_4033_getOpcode();
297 if (op) goto *op;
298 }
299}
300
Ted Kremenek956a37d2009-05-01 23:35:18 +0000301// Test invalidating pointers-to-pointers with slightly different types. This
302// example came from a recent false positive due to a regression where the
303// branch condition was falsely reported as being uninitialized.
304void invalidate_by_ref(char **x);
305int test_invalidate_by_ref() {
306 unsigned short y;
307 invalidate_by_ref((char**) &y);
308 if (y) // no-warning
309 return 1;
310 return 0;
311}
312
Ted Kremeneked47fc62009-07-03 00:10:50 +0000313// Test for <rdar://problem/7027684>. This just tests that the CFG is
314// constructed correctly. Previously, the successor block of the entrance
315// was the block containing the merge for '?', which would trigger an
316// assertion failure.
317int rdar_7027684_aux();
Anders Carlsson5d1d7ae2010-09-03 00:25:02 +0000318int rdar_7027684_aux_2() __attribute__((noreturn));
Ted Kremeneked47fc62009-07-03 00:10:50 +0000319void rdar_7027684(int x, int y) {
320 {}; // this empty compound statement is critical.
321 (rdar_7027684_aux() ? rdar_7027684_aux_2() : (void) 0);
322}
323
Ted Kremenek411af402009-07-06 22:23:45 +0000324// Test that we handle casts of string literals to arbitrary types.
325unsigned const char *string_literal_test1() {
326 return (const unsigned char*) "hello";
327}
328
329const float *string_literal_test2() {
330 return (const float*) "hello";
331}
332
Ted Kremenek169077d2009-07-06 23:47:19 +0000333// Test that we handle casts *from* incomplete struct types.
334extern const struct _FooAssertStruct _cmd;
335void test_cast_from_incomplete_struct_aux(volatile const void *x);
336void test_cast_from_incomplete_struct() {
337 test_cast_from_incomplete_struct_aux(&_cmd);
338}
Ted Kremeneked47fc62009-07-03 00:10:50 +0000339
Ted Kremenek59978882009-07-08 22:42:46 +0000340// Test for <rdar://problem/7034511>
341// "ValueManager::makeIntVal(uint64_t X, QualType T) should return a 'Loc'
342// when 'T' is a pointer"
343//
344// Previously this case would crash.
345void test_rdar_7034511(NSArray *y) {
346 NSObject *x;
347 for (x in y) {}
348 if (x == ((void*) 0)) {}
349}
350
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000351// Handle casts of function pointers (CodeTextRegions) to arbitrary pointer
352// types. This was previously causing a crash in CastRegion.
353void handle_funcptr_voidptr_casts() {
Ted Kremenek3f9811b2009-07-10 21:11:16 +0000354 void **ptr;
355 typedef void *PVOID;
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000356 typedef void *PCHAR;
Ted Kremenek3f9811b2009-07-10 21:11:16 +0000357 typedef long INT_PTR, *PINT_PTR;
358 typedef INT_PTR (*FARPROC)();
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000359 FARPROC handle_funcptr_voidptr_casts_aux();
360 PVOID handle_funcptr_voidptr_casts_aux_2(PVOID volatile *x);
361 PVOID handle_funcptr_voidptr_casts_aux_3(PCHAR volatile *x);
Ted Kremenek3f9811b2009-07-10 21:11:16 +0000362
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000363 ptr = (void**) handle_funcptr_voidptr_casts_aux();
364 handle_funcptr_voidptr_casts_aux_2(ptr);
365 handle_funcptr_voidptr_casts_aux_3(ptr);
Ted Kremenek3f9811b2009-07-10 21:11:16 +0000366}
367
Ted Kremenek31ef2b62009-07-10 21:43:30 +0000368// RegionStore::Retrieve previously crashed on this example. This example
369// was previously in the test file 'xfail_regionstore_wine_crash.c'.
370void testA() {
371 long x = 0;
372 char *y = (char *) &x;
373 if (!*y)
374 return;
375}
376
Ted Kremenek43d74a52009-07-11 04:38:49 +0000377// RegionStoreManager previously crashed on this example. The problem is that
378// the value bound to the field of b->grue after the call to testB_aux is
379// a symbolic region. The second '*__gruep__' involves performing a load
380// from a 'int*' that really is a 'void**'. The loaded location must be
381// implicitly converted to an integer that wraps a location. Previosly we would
382// get a crash here due to an assertion failure.
383typedef struct _BStruct { void *grue; } BStruct;
384void testB_aux(void *ptr);
385void testB(BStruct *b) {
386 {
387 int *__gruep__ = ((int *)&((b)->grue));
388 int __gruev__ = *__gruep__;
389 testB_aux(__gruep__);
390 }
391 {
392 int *__gruep__ = ((int *)&((b)->grue));
393 int __gruev__ = *__gruep__;
394 if (~0 != __gruev__) {}
395 }
396}
397
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000398void test_trivial_symbolic_comparison(int *x) {
399 int test_trivial_symbolic_comparison_aux();
400 int a = test_trivial_symbolic_comparison_aux();
401 int b = a;
Stephen Hines651f13c2014-04-23 16:59:28 -0700402 if (a != b) {
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000403 int *p = 0;
404 *p = 0xDEADBEEF; // no-warning
405 }
406
407 a = a == 1;
408 b = b == 1;
Stephen Hines651f13c2014-04-23 16:59:28 -0700409 if (a != b) {
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000410 int *p = 0;
411 *p = 0xDEADBEEF; // no-warning
412 }
413}
414
Ted Kremenekfde2efe2009-07-15 22:09:25 +0000415// Test for:
416// <rdar://problem/7062158> false positive null dereference due to
417// BasicStoreManager not tracking *static* globals
418//
419// This just tests the proper tracking of symbolic values for globals (both
420// static and non-static).
421//
422static int* x_rdar_7062158;
423void rdar_7062158() {
424 int *current = x_rdar_7062158;
425 if (current == x_rdar_7062158)
426 return;
427
428 int *p = 0;
429 *p = 0xDEADBEEF; // no-warning
430}
431
432int* x_rdar_7062158_2;
433void rdar_7062158_2() {
434 int *current = x_rdar_7062158_2;
435 if (current == x_rdar_7062158_2)
436 return;
437
438 int *p = 0;
439 *p = 0xDEADBEEF; // no-warning
440}
441
Ted Kremenek46537392009-07-16 01:33:37 +0000442// This test reproduces a case for a crash when analyzing ClamAV using
443// RegionStoreManager (the crash doesn't exhibit in BasicStoreManager because
444// it isn't doing anything smart about arrays). The problem is that on the
445// second line, 'p = &p[i]', p is assigned an ElementRegion whose index
446// is a 16-bit integer. On the third line, a new ElementRegion is created
447// based on the previous region, but there the region uses a 32-bit integer,
448// resulting in a clash of values (an assertion failure at best). We resolve
449// this problem by implicitly converting index values to 'int' when the
450// ElementRegion is created.
451unsigned char test_array_index_bitwidth(const unsigned char *p) {
452 unsigned short i = 0;
Tom Care6216dc02010-08-30 19:25:43 +0000453 for (i = 0; i < 2; i++) p = &p[i];
Ted Kremenek46537392009-07-16 01:33:37 +0000454 return p[i+1];
455}
456
Ted Kremenek63b9cfe2009-07-18 06:27:51 +0000457// This case tests that CastRegion handles casts involving BlockPointerTypes.
458// It should not crash.
459void test_block_cast() {
460 id test_block_cast_aux();
Argyrios Kyrtzidis1b2ad2f2010-09-19 23:03:35 +0000461 (void (^)(void *))test_block_cast_aux(); // expected-warning{{expression result unused}}
Ted Kremenek63b9cfe2009-07-18 06:27:51 +0000462}
463
Chris Lattnere0303582010-01-09 20:43:19 +0000464int OSAtomicCompareAndSwap32Barrier();
465
Ted Kremenek293769a2009-07-20 21:00:55 +0000466// Test comparison of 'id' instance variable to a null void* constant after
467// performing an OSAtomicCompareAndSwap32Barrier.
468// This previously was a crash in RegionStoreManager.
469@interface TestIdNull {
470 id x;
471}
472-(int)foo;
473@end
474@implementation TestIdNull
475-(int)foo {
476 OSAtomicCompareAndSwap32Barrier(0, (signed)2, (signed*)&x);
477 if (x == (void*) 0) { return 0; }
478 return 1;
479}
480@end
481
Anna Zaks6400f022012-05-07 19:40:49 +0000482// Do not crash when performing compare and swap on symbolic values.
483typedef int int32_t;
484typedef int int32;
485typedef int32 Atomic32;
486int OSAtomicCompareAndSwap32( int32_t __oldValue, int32_t __newValue, volatile int32_t *__theValue);
487void radar11390991_NoBarrier_CompareAndSwap(volatile Atomic32 *ptr,
488 Atomic32 old_value,
489 Atomic32 new_value) {
490 OSAtomicCompareAndSwap32(old_value, new_value, ptr);
491}
492
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000493// PR 4594 - This was a crash when handling casts in SimpleSValuator.
494void PR4594() {
495 char *buf[1];
496 char **foo = buf;
497 *foo = "test";
498}
Ted Kremenek0b331e32009-07-22 04:23:20 +0000499
500// Test invalidation logic where an integer is casted to an array with a
501// different sign and then invalidated.
502void test_invalidate_cast_int() {
503 void test_invalidate_cast_int_aux(unsigned *i);
504 signed i;
505 test_invalidate_cast_int_aux((unsigned*) &i);
506 if (i < 0)
507 return;
508}
509
Chris Lattnere0303582010-01-09 20:43:19 +0000510int ivar_getOffset();
511
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000512// Reduced from a crash involving the cast of an Objective-C symbolic region to
513// 'char *'
514static NSNumber *test_ivar_offset(id self, SEL _cmd, Ivar inIvar) {
515 return [[[NSNumber allocWithZone:((void*)0)] initWithBool:*(_Bool *)((char *)self + ivar_getOffset(inIvar))] autorelease];
516}
Ted Kremenek9a108eb2009-08-02 04:12:53 +0000517
518// Reduced from a crash in StoreManager::CastRegion involving a divide-by-zero.
519// This resulted from not properly handling region casts to 'const void*'.
520void test_cast_const_voidptr() {
521 char x[10];
522 char *p = &x[1];
523 const void* q = p;
524}
Ted Kremenek968f0a62009-08-03 21:41:46 +0000525
526// Reduced from a crash when analyzing Wine. This test handles loads from
527// function addresses.
528typedef long (*FARPROC)();
529FARPROC test_load_func(FARPROC origfun) {
530 if (!*(unsigned char*) origfun)
531 return origfun;
532 return 0;
533}
Ted Kremenek48775d52009-08-03 23:22:53 +0000534
535// Test passing-by-value an initialized struct variable.
536struct test_pass_val {
537 int x;
538 int y;
539};
540void test_pass_val_aux(struct test_pass_val s);
541void test_pass_val() {
542 struct test_pass_val s;
543 s.x = 1;
Ted Kremenek8eec7c02009-08-04 00:58:45 +0000544 s.y = 2;
Ted Kremenek48775d52009-08-03 23:22:53 +0000545 test_pass_val_aux(s);
546}
547
Ted Kremenek4ed45982009-08-05 05:31:02 +0000548// This is a reduced test case of a false positive that previously appeared
549// in RegionStoreManager. Previously the array access resulted in dereferencing
550// an undefined value.
551int test_array_compound(int *q, int *r, int *z) {
552 int *array[] = { q, r, z };
553 int j = 0;
554 for (unsigned i = 0; i < 3 ; ++i)
555 if (*array[i]) ++j; // no-warning
556 return j;
557}
558
Ted Kremenek1894dce2009-08-25 20:51:30 +0000559// symbolic value stored in 'x' wouldn't be implicitly casted to a signed value
560// during the comparison.
561int rdar_7124210(unsigned int x) {
562 enum { SOME_CONSTANT = 123 };
563 int compare = ((signed) SOME_CONSTANT) == *((signed *) &x);
564 return compare ? 0 : 1; // Forces the evaluation of the symbolic constraint.
565}
566
Ted Kremenekab2f43c2009-08-25 23:29:04 +0000567void pr4781(unsigned long *raw1) {
568 unsigned long *cook, *raw0;
569 unsigned long dough[32];
570 int i;
571 cook = dough;
572 for( i = 0; i < 16; i++, raw1++ ) {
573 raw0 = raw1++;
574 *cook = (*raw0 & 0x00fc0000L) << 6;
575 *cook |= (*raw0 & 0x00000fc0L) << 10;
576 }
577}
578
Ted Kremenekcfcd7fd2009-09-09 20:36:12 +0000579// <rdar://problem/7185647> - 'self' should be treated as being non-null
580// upon entry to an objective-c method.
581@interface RDar7185647
582- (id)foo;
583@end
584@implementation RDar7185647
585- (id) foo {
586 if (self)
587 return self;
Chris Lattner8b5dec32010-07-07 06:14:23 +0000588 *((volatile int *) 0x0) = 0xDEADBEEF; // no-warning
Ted Kremenekcfcd7fd2009-09-09 20:36:12 +0000589 return self;
590}
591@end
Ted Kremeneke2b57442009-09-15 00:40:32 +0000592
593// Test reasoning of __builtin_offsetof;
594struct test_offsetof_A {
595 int x;
596 int y;
597};
598struct test_offsetof_B {
599 int w;
600 int z;
601};
602void test_offsetof_1() {
603 if (__builtin_offsetof(struct test_offsetof_A, x) ==
604 __builtin_offsetof(struct test_offsetof_B, w))
605 return;
606 int *p = 0;
607 *p = 0xDEADBEEF; // no-warning
608}
609void test_offsetof_2() {
610 if (__builtin_offsetof(struct test_offsetof_A, y) ==
611 __builtin_offsetof(struct test_offsetof_B, z))
612 return;
613 int *p = 0;
614 *p = 0xDEADBEEF; // no-warning
615}
616void test_offsetof_3() {
617 if (__builtin_offsetof(struct test_offsetof_A, y) -
618 __builtin_offsetof(struct test_offsetof_A, x)
619 ==
620 __builtin_offsetof(struct test_offsetof_B, z) -
621 __builtin_offsetof(struct test_offsetof_B, w))
622 return;
623 int *p = 0;
624 *p = 0xDEADBEEF; // no-warning
625}
626void test_offsetof_4() {
627 if (__builtin_offsetof(struct test_offsetof_A, y) ==
628 __builtin_offsetof(struct test_offsetof_B, w))
629 return;
630 int *p = 0;
631 *p = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}}
632}
633
Ted Kremenekc32b2442009-09-21 23:22:11 +0000634// <rdar://problem/6829164> "nil receiver" false positive: make tracking
635// of the MemRegion for 'self' path-sensitive
636@interface RDar6829164 : NSObject {
637 double x; int y;
638}
639- (id) init;
640@end
641
642id rdar_6829164_1();
643double rdar_6829164_2();
644
645@implementation RDar6829164
646- (id) init {
647 if((self = [super init]) != 0) {
648 id z = rdar_6829164_1();
649 y = (z != 0);
650 if (y)
651 x = rdar_6829164_2();
652 }
653 return self;
654}
655@end
Ted Kremenekcc969fd2009-09-22 04:48:39 +0000656
657// <rdar://problem/7242015> - Invalidate values passed-by-reference
658// to functions when the pointer to the value is passed as an integer.
659void test_7242015_aux(unsigned long);
660int rdar_7242015() {
661 int x;
662 test_7242015_aux((unsigned long) &x); // no-warning
663 return x; // Previously we return and uninitialized value when
664 // using RegionStore.
665}
666
Ted Kremenekcf549592009-09-22 21:19:14 +0000667// <rdar://problem/7242006> [RegionStore] compound literal assignment with
668// floats not honored
669CGFloat rdar7242006(CGFloat x) {
670 NSSize y = (NSSize){x, 10};
671 return y.width; // no-warning
672}
673
Ted Kremenek657406d2009-09-23 01:30:01 +0000674// PR 4988 - This test exhibits a case where a function can be referenced
675// when not explicitly used in an "lvalue" context (as far as the analyzer is
676// concerned). This previously triggered a crash due to an invalid assertion.
677void pr_4988(void) {
Argyrios Kyrtzidis1b2ad2f2010-09-19 23:03:35 +0000678 pr_4988; // expected-warning{{expression result unused}}
Ted Kremenek657406d2009-09-23 01:30:01 +0000679}
680
Ted Kremenek022a1252009-09-26 17:18:44 +0000681// <rdar://problem/7152418> - A 'signed char' is used as a flag, which is
682// implicitly converted to an int.
683void *rdar7152418_bar();
684@interface RDar7152418 {
685 signed char x;
686}
687-(char)foo;
688@end;
689@implementation RDar7152418
690-(char)foo {
691 char *p = 0;
692 void *result = 0;
693 if (x) {
694 result = rdar7152418_bar();
695 p = "hello";
696 }
697 if (!result) {
698 result = rdar7152418_bar();
699 if (result && x)
700 return *p; // no-warning
701 }
702 return 1;
703}
704
Ted Kremenek9b020342009-10-17 07:39:35 +0000705//===----------------------------------------------------------------------===//
Ted Kremenekb1d04222009-10-06 03:44:49 +0000706// Test constant-folding of symbolic values, automatically handling type
Ted Kremenek9b020342009-10-17 07:39:35 +0000707// conversions of the symbol as necessary.
708//===----------------------------------------------------------------------===//
709
Ted Kremenek9b020342009-10-17 07:39:35 +0000710// Previously this would crash once we started eagerly evaluating symbols whose
711// values were constrained to a single value.
712void test_symbol_fold_1(signed char x) {
Ted Kremenekb1d04222009-10-06 03:44:49 +0000713 while (1) {
714 if (x == ((signed char) 0)) {}
715 }
716}
717
Ted Kremenek9b020342009-10-17 07:39:35 +0000718// This previously caused a crash because it triggered an assertion in APSInt.
719void test_symbol_fold_2(unsigned int * p, unsigned int n,
720 const unsigned int * grumpkin, unsigned int dn) {
Ted Kremenekb5deae52009-10-16 20:46:24 +0000721 unsigned int i;
722 unsigned int tempsub[8];
723 unsigned int *solgrumpkin = tempsub + n;
724 for (i = 0; i < n; i++)
725 solgrumpkin[i] = (i < dn) ? ~grumpkin[i] : 0xFFFFFFFF;
726 for (i <<= 5; i < (n << 5); i++) {}
727}
Ted Kremenekb1d04222009-10-06 03:44:49 +0000728
Ted Kremenek9b020342009-10-17 07:39:35 +0000729// This previously caused a crash because it triggered an assertion in APSInt.
730// 'x' would evaluate to a 8-bit constant (because of the return value of
731// test_symbol_fold_3_aux()) which would not get properly promoted to an
732// integer.
733char test_symbol_fold_3_aux(void);
734unsigned test_symbol_fold_3(void) {
735 unsigned x = test_symbol_fold_3_aux();
736 if (x == 54)
737 return (x << 8) | 0x5;
738 return 0;
Ted Kremenekde7d8002009-11-11 06:43:42 +0000739}
740
741//===----------------------------------------------------------------------===//
742// Tests for the warning of casting a non-struct type to a struct type
743//===----------------------------------------------------------------------===//
744
745typedef struct {unsigned int v;} NSSwappedFloat;
746
747NSSwappedFloat test_cast_nonstruct_to_struct(float x) {
748 struct hodor {
749 float number;
750 NSSwappedFloat sf;
751 };
752 return ((struct hodor *)&x)->sf; // expected-warning{{Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption}}
753}
754
755NSSwappedFloat test_cast_nonstruct_to_union(float x) {
756 union bran {
757 float number;
758 NSSwappedFloat sf;
759 };
760 return ((union bran *)&x)->sf; // no-warning
761}
Ted Kremenek9b020342009-10-17 07:39:35 +0000762
Zhongxing Xud6944852009-11-11 13:42:54 +0000763void test_undefined_array_subscript() {
764 int i, a[10];
765 int *p = &a[i]; // expected-warning{{Array subscript is undefined}}
766}
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000767@end
Ted Kremenek64fa8582009-11-21 00:49:41 +0000768
769//===----------------------------------------------------------------------===//
Ted Kremenek998c1332009-11-23 17:58:48 +0000770// Test using an uninitialized value as a branch condition.
771//===----------------------------------------------------------------------===//
772
773int test_uninit_branch(void) {
774 int x;
775 if (x) // expected-warning{{Branch condition evaluates to a garbage value}}
776 return 1;
777 return 0;
778}
779
780int test_uninit_branch_b(void) {
781 int x;
782 return x ? 1 : 0; // expected-warning{{Branch condition evaluates to a garbage value}}
783}
784
Ted Kremenek616cf052009-11-23 18:12:03 +0000785int test_uninit_branch_c(void) {
786 int x;
787 if ((short)x) // expected-warning{{Branch condition evaluates to a garbage value}}
788 return 1;
789 return 0;
790}
Ted Kremenek998c1332009-11-23 17:58:48 +0000791
792//===----------------------------------------------------------------------===//
Ted Kremenek64fa8582009-11-21 00:49:41 +0000793// Test passing an undefined value in a message or function call.
794//===----------------------------------------------------------------------===//
795
796void test_bad_call_aux(int x);
797void test_bad_call(void) {
798 int y;
Ted Kremenek818b4332010-09-09 22:51:55 +0000799 test_bad_call_aux(y); // expected-warning{{Function call argument is an uninitialized value}}
Ted Kremenek64fa8582009-11-21 00:49:41 +0000800}
801
802@interface TestBadArg {}
803- (void) testBadArg:(int) x;
804@end
805
806void test_bad_msg(TestBadArg *p) {
807 int y;
Ted Kremenek818b4332010-09-09 22:51:55 +0000808 [p testBadArg:y]; // expected-warning{{Argument in message expression is an uninitialized value}}
Ted Kremenek64fa8582009-11-21 00:49:41 +0000809}
810
Ted Kremenekc213b482010-01-15 07:56:51 +0000811//===----------------------------------------------------------------------===//
812// PR 6033 - Test emitting the correct output in a warning where we use '%'
813// with operands that are undefined.
814//===----------------------------------------------------------------------===//
815
816int pr6033(int x) {
817 int y;
818 return x % y; // expected-warning{{The right operand of '%' is a garbage value}}
819}
820
Zhongxing Xu45eb4062010-01-19 12:11:55 +0000821struct trie {
822 struct trie* next;
823};
824
825struct kwset {
826 struct trie *trie;
Ted Kremenekac518ec2011-02-11 20:13:27 +0000827 unsigned char y[10];
Zhongxing Xu45eb4062010-01-19 12:11:55 +0000828 struct trie* next[10];
829 int d;
830};
831
832typedef struct trie trie_t;
833typedef struct kwset kwset_t;
834
835void f(kwset_t *kws, char const *p, char const *q) {
836 struct trie const *trie;
837 struct trie * const *next = kws->next;
838 register unsigned char c;
839 register char const *end = p;
840 register char const *lim = q;
841 register int d = 1;
Ted Kremenekac518ec2011-02-11 20:13:27 +0000842 register unsigned char const *y = kws->y;
Zhongxing Xu45eb4062010-01-19 12:11:55 +0000843
Ted Kremenekac518ec2011-02-11 20:13:27 +0000844 d = y[c = (end+=d)[-1]]; // no-warning
Zhongxing Xu45eb4062010-01-19 12:11:55 +0000845 trie = next[c];
846}
Ted Kremenek61e8e1b2010-02-02 02:01:51 +0000847
848//===----------------------------------------------------------------------===//
849// <rdar://problem/7593875> When handling sizeof(VLA) it leads to a hole in
850// the ExplodedGraph (causing a false positive)
851//===----------------------------------------------------------------------===//
852
853int rdar_7593875_aux(int x);
854int rdar_7593875(int n) {
855 int z[n > 10 ? 10 : n]; // VLA.
856 int v;
857 v = rdar_7593875_aux(sizeof(z));
858 // Previously we got a false positive about 'v' being uninitialized.
859 return v; // no-warning
860}
Ted Kremenekf6817042010-02-02 21:11:40 +0000861
862//===----------------------------------------------------------------------===//
863// Handle casts from symbolic regions (packaged as integers) to doubles.
864// Previously this caused an assertion failure.
865//===----------------------------------------------------------------------===//
866
867void *foo_rev95119();
868void baz_rev95119(double x);
869void bar_rev95119() {
870 // foo_rev95119() returns a symbolic pointer. It is then
871 // cast to an int which is then cast to a double.
872 int value = (int) foo_rev95119();
873 baz_rev95119((double)value);
874}
875
Ted Kremenek5b290652010-02-03 04:16:00 +0000876//===----------------------------------------------------------------------===//
877// Handle loading a symbolic pointer from a symbolic region that was
878// invalidated by a call to an unknown function.
879//===----------------------------------------------------------------------===//
880
881void bar_rev95192(int **x);
882void foo_rev95192(int **x) {
883 *x = 0;
884 bar_rev95192(x);
885 // Not a null dereference.
886 **x = 1; // no-warning
887}
888
Ted Kremenek40c37e12010-02-04 00:47:48 +0000889//===----------------------------------------------------------------------===//
890// Handle casts of a function to a function pointer with a different return
891// value. We don't yet emit an error for such cases, but we now we at least
892// don't crash when the return value gets interpreted in a way that
893// violates our invariants.
894//===----------------------------------------------------------------------===//
895
896void *foo_rev95267();
897int bar_rev95267() {
898 char (*Callback_rev95267)(void) = (char (*)(void)) foo_rev95267;
899 if ((*Callback_rev95267)() == (char) 0)
900 return 1;
901 return 0;
902}
Ted Kremenek95a01122010-02-04 04:18:55 +0000903
904// Same as previous case, but handle casts to 'void'.
905int bar_rev95274() {
906 void (*Callback_rev95274)(void) = (void (*)(void)) foo_rev95267;
907 (*Callback_rev95274)();
908 return 0;
909}
910
Ted Kremenek4dc15662010-02-06 03:57:59 +0000911void rdar7582031_test_static_init_zero() {
912 static unsigned x;
913 if (x == 0)
914 return;
915 int *p = 0;
916 *p = 0xDEADBEEF;
917}
Ted Kremenek81861ab2010-02-06 04:04:46 +0000918void rdar7582031_test_static_init_zero_b() {
919 static void* x;
920 if (x == 0)
921 return;
922 int *p = 0;
923 *p = 0xDEADBEEF;
924}
Ted Kremenek8ec4aac2010-02-09 19:11:53 +0000925
926//===----------------------------------------------------------------------===//
927// Test handling of parameters that are structs that contain floats and //
928// nested fields. //
929//===----------------------------------------------------------------------===//
930
931struct s_rev95547_nested { float x, y; };
932struct s_rev95547 {
933 struct s_rev95547_nested z1;
934 struct s_rev95547_nested z2;
935};
936float foo_rev95547(struct s_rev95547 w) {
937 return w.z1.x + 20.0; // no-warning
938}
939void foo_rev95547_b(struct s_rev95547 w) {
940 struct s_rev95547 w2 = w;
941 w2.z1.x += 20.0; // no-warning
942}
Ted Kremenek862b24f2010-04-29 01:10:26 +0000943
944//===----------------------------------------------------------------------===//
945// Test handling statement expressions that don't populate a CFG block that
946// is used to represent the computation of the RHS of a logical operator.
947// This previously triggered a crash.
948//===----------------------------------------------------------------------===//
949
950void pr6938() {
951 if (1 && ({
952 while (0);
953 0;
954 }) == 0) {
955 }
956}
957
958void pr6938_b() {
959 if (1 && *({ // expected-warning{{Dereference of null pointer}}
960 while (0) {}
961 ({
962 (int *) 0;
963 });
964 }) == 0) {
965 }
966}
Ted Kremenekfadebba2010-05-13 16:38:08 +0000967
968//===----------------------------------------------------------------------===//
969// <rdar://problem/7979430> - The CFG for code containing an empty
970// @synchronized block was previously broken (and would crash the analyzer).
971//===----------------------------------------------------------------------===//
972
973void r7979430(id x) {
974 @synchronized(x) {}
975}
976
Ted Kremenekfcd783d2010-06-15 00:55:40 +0000977//===----------------------------------------------------------------------===
978// PR 7361 - Test that functions wrapped in macro instantiations are analyzed.
979//===----------------------------------------------------------------------===
980#define MAKE_TEST_FN() \
981 void test_pr7361 (char a) {\
982 char* b = 0x0; *b = a;\
983 }
984
985MAKE_TEST_FN() // expected-warning{{null pointer}}
986
Jordy Rose5ca129c2010-06-27 01:20:56 +0000987//===----------------------------------------------------------------------===
988// PR 7491 - Test that symbolic expressions can be used as conditions.
989//===----------------------------------------------------------------------===
990
991void pr7491 () {
992 extern int getint();
993 int a = getint()-1;
994 if (a) {
995 return;
996 }
997 if (!a) {
998 return;
999 } else {
1000 // Should be unreachable
1001 (void)*(char*)0; // no-warning
1002 }
1003}
1004
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001005//===----------------------------------------------------------------------===
1006// PR 7475 - Test that assumptions about global variables are reset after
1007// calling a global function.
1008//===----------------------------------------------------------------------===
1009
1010int *pr7475_someGlobal;
1011void pr7475_setUpGlobal();
1012
1013void pr7475() {
1014 if (pr7475_someGlobal == 0)
1015 pr7475_setUpGlobal();
1016 *pr7475_someGlobal = 0; // no-warning
1017}
1018
1019void pr7475_warn() {
1020 static int *someStatic = 0;
1021 if (someStatic == 0)
1022 pr7475_setUpGlobal();
1023 *someStatic = 0; // expected-warning{{null pointer}}
1024}
1025
Ted Kremenekcb077882010-07-17 01:28:55 +00001026// <rdar://problem/8202272> - __imag passed non-complex should not crash
1027float f0(_Complex float x) {
1028 float l0 = __real x;
1029 return __real l0 + __imag l0;
1030}
1031
Jordy Rosea277e772010-08-09 20:31:57 +00001032
1033//===----------------------------------------------------------------------===
1034// Test that we can reduce symbols to constants whether they are on the left
1035// or right side of an expression.
1036//===----------------------------------------------------------------------===
1037
1038void reduce_to_constant(int x, int y) {
1039 if (x != 20)
1040 return;
1041
1042 int a = x + y;
1043 int b = y + x;
1044
1045 if (y == -20 && a != 0)
1046 (void)*(char*)0; // no-warning
1047 if (y == -20 && b != 0)
1048 (void)*(char*)0; // no-warning
1049}
Ted Kremenek34feff62010-08-26 22:19:33 +00001050
1051// <rdar://problem/8360854> - Test that code after a switch statement with no
1052// 'case:' labels is correctly evaluated.
1053void r8360854(int n) {
1054 switch (n) {
1055 default: ;
1056 }
1057 int *p = 0;
1058 *p = 0xDEADBEEF; // expected-warning{{null pointer}}
1059}
1060
Ted Kremenekf69cf182010-09-01 20:35:38 +00001061// PR 8050 - crash in CastSizeChecker when pointee is an incomplete type
1062typedef long unsigned int __darwin_size_t;
1063typedef __darwin_size_t size_t;
1064void *malloc(size_t);
1065
1066struct PR8050;
1067
1068void pr8050(struct PR8050 **arg)
1069{
1070 *arg = malloc(1);
1071}
1072
Ted Kremenek977a58a2010-09-09 00:40:43 +00001073// <rdar://problem/5880430> Switch on enum should not consider default case live
1074// if all enum values are covered
1075enum Cases { C1, C2, C3, C4 };
1076void test_enum_cases(enum Cases C) {
1077 switch (C) {
1078 case C1:
1079 case C2:
1080 case C4:
1081 case C3:
1082 return;
1083 }
1084 int *p = 0;
1085 *p = 0xDEADBEEF; // no-warning
1086}
1087
1088void test_enum_cases_positive(enum Cases C) {
1089 switch (C) { // expected-warning{{enumeration value 'C4' not handled in switch}}
1090 case C1:
1091 case C2:
1092 case C3:
1093 return;
1094 }
1095 int *p = 0;
1096 *p = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}}
1097}
1098
Ted Kremenek4f6aa772010-09-10 03:05:40 +00001099// <rdar://problem/6351970> rule request: warn if synchronization mutex can be nil
1100void rdar6351970() {
1101 id x = 0;
1102 @synchronized(x) {} // expected-warning{{Nil value used as mutex for @synchronized() (no synchronization will occur)}}
1103}
1104
1105void rdar6351970_b(id x) {
1106 if (!x)
1107 @synchronized(x) {} // expected-warning{{Nil value used as mutex for @synchronized() (no synchronization will occur)}}
1108}
1109
1110void rdar6351970_c() {
1111 id x;
1112 @synchronized(x) {} // expected-warning{{Uninitialized value used as mutex for @synchronized}}
1113}
Ted Kremenek977a58a2010-09-09 00:40:43 +00001114
Ted Kremenek1adee4b2010-10-21 15:38:55 +00001115@interface Rdar8578650
1116- (id) foo8578650;
1117@end
1118
1119void rdar8578650(id x) {
1120 @synchronized (x) {
1121 [x foo8578650];
1122 }
1123 // At this point we should assume that 'x' is not nil, not
1124 // the inverse.
1125 @synchronized (x) { // no-warning
1126 }
1127}
1128
Ted Kremenek0853c7f2010-09-10 20:20:49 +00001129// <rdar://problem/6352035> rule request: direct structure member access null pointer dereference
1130@interface RDar6352035 {
1131 int c;
1132}
1133- (void)foo;
1134- (void)bar;
1135@end
1136
1137@implementation RDar6352035
1138- (void)foo {
1139 RDar6352035 *friend = 0;
Jordan Rose991bcb42012-09-22 01:24:38 +00001140 friend->c = 7; // expected-warning{{Access to instance variable 'c' results in a dereference of a null pointer (loaded from variable 'friend')}}
Ted Kremenek0853c7f2010-09-10 20:20:49 +00001141}
1142- (void)bar {
1143 self = 0;
Jordan Rose991bcb42012-09-22 01:24:38 +00001144 c = 7; // expected-warning{{Access to instance variable 'c' results in a dereference of a null pointer (loaded from variable 'self')}}
Ted Kremenek0853c7f2010-09-10 20:20:49 +00001145}
1146@end
1147
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001148// PR 8149 - GNU statement expression in condition of ForStmt.
1149// This previously triggered an assertion failure in CFGBuilder.
1150void pr8149(void) {
1151 for (; ({ do { } while (0); 0; });) { }
1152}
1153
Ted Kremenek0d4f7672010-10-25 20:20:56 +00001154// PR 8458 - Make sure @synchronized doesn't crash with properties.
1155@interface PR8458 {}
1156@property(readonly) id lock;
1157@end
1158
1159static
1160void __PR8458(PR8458 *x) {
1161 @synchronized(x.lock) {} // no-warning
1162}
1163
Ted Kremenekc1143e52010-10-26 00:06:17 +00001164// PR 8440 - False null dereference during store to array-in-field-in-global.
1165// This test case previously resulted in a bogus null deref warning from
1166// incorrect lazy symbolication logic in RegionStore.
1167static struct {
1168 int num;
1169 char **data;
1170} saved_pr8440;
1171
1172char *foo_pr8440();
1173char **bar_pr8440();
1174void baz_pr8440(int n)
1175{
1176 saved_pr8440.num = n;
1177 if (saved_pr8440.data)
1178 return;
1179 saved_pr8440.data = bar_pr8440();
1180 for (int i = 0 ; i < n ; i ++)
1181 saved_pr8440.data[i] = foo_pr8440(); // no-warning
1182}
1183
Ted Kremenek29836f92010-11-11 23:10:10 +00001184// Support direct accesses to non-null memory. Reported in:
1185// PR 5272
1186// <rdar://problem/6839683>
1187int test_direct_address_load() {
1188 int *p = (int*) 0x4000;
1189 return *p; // no-warning
1190}
1191
1192void pr5272_test() {
1193 struct pr5272 { int var2; };
1194 (*(struct pr5272*)0xBC000000).var2 = 0; // no-warning
1195 (*(struct pr5272*)0xBC000000).var2 += 2; // no-warning
1196}
1197
Ted Kremenek948163b2010-11-15 20:09:42 +00001198// Support casting the return value of function to another different type
1199// This previously caused a crash, although we likely need more precise
1200// reasoning here. <rdar://problem/8663544>
1201void* rdar8663544();
1202typedef struct {} Val8663544;
1203Val8663544 bazR8663544() {
1204 Val8663544(*func) () = (Val8663544(*) ()) rdar8663544;
1205 return func();
1206}
1207
Ted Kremeneke4ae4dc2010-11-15 22:59:22 +00001208// PR 8619 - Handle ternary expressions with a call to a noreturn function.
1209// This previously resulted in a crash.
1210void pr8619_noreturn(int x) __attribute__((noreturn));
1211
1212void pr8619(int a, int b, int c) {
1213 a ?: pr8619_noreturn(b || c);
1214}
1215
1216
Ted Kremenekac518ec2011-02-11 20:13:27 +00001217// PR 8646 - crash in the analyzer when handling unions.
1218union pr8648_union {
1219 signed long long pr8648_union_field;
1220};
1221void pr8648() {
1222 long long y;
1223 union pr8648_union x = { .pr8648_union_field = 0LL };
1224 y = x.pr8648_union_field;
1225
1226 union pr8648_union z;
1227 z = (union pr8648_union) { .pr8648_union_field = 0LL };
1228
1229 union pr8648_union w;
1230 w = ({ (union pr8648_union) { .pr8648_union_field = 0LL }; });
1231
1232 // crash, no assignment
1233 (void) ({ (union pr8648_union) { .pr8648_union_field = 0LL }; }).pr8648_union_field;
1234
1235 // crash with assignment
1236 y = ({ (union pr8648_union) { .pr8648_union_field = 0LL }; }).pr8648_union_field;
1237}
Ted Kremenek6b12da92011-02-21 22:11:26 +00001238
1239// PR 9269 - don't assert when building the following CFG. The for statement
1240// contains a condition with multiple basic blocks, and the value of the
1241// statement expression is then indexed as part of a bigger condition expression.
1242// This example exposed a bug in child traversal in the CFGBuilder.
1243void pr9269() {
1244 struct s { char *bar[10]; } baz[2] = { 0 };
1245 unsigned i = 0;
1246 for (i = 0;
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001247 (* ({ while(0); ({ &baz[0]; }); })).bar[0] != 0; // expected-warning {{while loop has empty body}} expected-note {{put the semicolon on a separate line to silence this warning}}
Ted Kremenek6b12da92011-02-21 22:11:26 +00001248 ++i) {}
1249}
1250
Ted Kremenekf226d182011-02-24 03:09:15 +00001251// Test evaluation of GNU-style ?:.
1252int pr9287(int type) { return type ? : 0; } // no-warning
1253
1254void pr9287_b(int type, int *p) {
1255 int x = type ? : 0;
1256 if (x) {
1257 p = 0;
1258 }
1259 if (type) {
1260 *p = 0xDEADBEEF; // expected-warning {{null pointer}}
1261 }
1262}
1263
1264void pr9287_c(int type, int *p) {
1265 int x = type ? : 0;
1266 if (x) {
1267 p = 0;
1268 }
1269 if (!type) {
1270 *p = 0xDEADBEEF; // no-warning
1271 }
1272}
1273
Ted Kremeneke71f3d52011-03-01 23:12:55 +00001274void test_switch() {
1275 switch (4) {
1276 case 1: {
1277 int *p = 0;
1278 *p = 0xDEADBEEF; // no-warning
1279 break;
1280 }
1281 case 4: {
1282 int *p = 0;
1283 *p = 0xDEADBEEF; // expected-warning {{null}}
1284 break;
1285 }
1286 default: {
1287 int *p = 0;
1288 *p = 0xDEADBEEF; // no-warning
1289 break;
1290 }
1291 }
Ted Kremeneke9cd9c02011-03-13 03:48:04 +00001292}
1293
1294// PR 9467. Tests various CFG optimizations. This previously crashed.
1295static void test(unsigned int bit_mask)
1296{
1297 unsigned int bit_index;
1298 for (bit_index = 0;
1299 bit_index < 24;
1300 bit_index++) {
1301 switch ((0x01 << bit_index) & bit_mask) {
1302 case 0x100000: ;
1303 }
1304 }
1305}
Ted Kremenekbc869de2011-05-10 18:42:15 +00001306
1307// Don't crash on code containing __label__.
1308int radar9414427_aux();
1309void radar9414427() {
1310 __label__ mylabel;
1311 if (radar9414427_aux()) {
1312 mylabel: do {}
1313 while (0);
1314 }
1315}
1316
Ted Kremenek6e744db2011-05-19 00:56:53 +00001317// Analyze methods in @implementation (category)
1318@interface RDar9465344
1319@end
1320
1321@implementation RDar9465344 (MyCategory)
1322- (void) testcategoryImpl {
1323 int *p = 0x0;
1324 *p = 0xDEADBEEF; // expected-warning {{null}}
1325}
1326@end
1327
1328@implementation RDar9465344
1329@end
1330
Ted Kremenekccf1bfd2011-11-14 19:36:08 +00001331// Don't crash when analyzing access to 'self' within a block.
1332@interface Rdar10380300Base
1333- (void) foo;
1334@end
1335@interface Rdar10380300 : Rdar10380300Base @end
1336@implementation Rdar10380300
1337- (void)foo {
1338 ^{
1339 [super foo];
1340 }();
1341}
1342@end
1343
Ted Kremenek6da60492012-07-26 22:23:41 +00001344// Don't crash when a ?: is only preceded by a statement (not an expression)
1345// in the CFG.
1346void __assert_fail();
1347
1348enum rdar1196620_e { E_A, E_B, E_C, E_D };
1349struct rdar1196620_s { int ints[E_D+1]; };
1350
Ted Kremenek61b135a2012-07-26 22:32:35 +00001351static void rdar1196620_call_assert(struct rdar1196620_s* s) {
Ted Kremenek6da60492012-07-26 22:23:41 +00001352 int i = 0;
1353 s?(void)0:__assert_fail();
1354}
1355
1356static void rdar1196620(struct rdar1196620_s* s) {
Ted Kremenek61b135a2012-07-26 22:32:35 +00001357 rdar1196620_call_assert(s);
Ted Kremenek6da60492012-07-26 22:23:41 +00001358}
1359
1360