1 // NOTE: Use '-fobjc-gc' to test the analysis being run twice, and multiple reports are not issued.
2 // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,deadcode.IdempotentOperations,experimental.core,osx.cocoa.AtSync -analyzer-store=region -analyzer-constraints=basic -verify -fblocks -Wno-unreachable-code -Wno-null-dereference %s
3 // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,deadcode.IdempotentOperations,experimental.core,osx.cocoa.AtSync -analyzer-store=region -analyzer-constraints=range -verify -fblocks -Wno-unreachable-code -Wno-null-dereference %s
4 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,deadcode.IdempotentOperations,experimental.core,osx.cocoa.AtSync -analyzer-store=region -analyzer-constraints=basic -verify -fblocks -Wno-unreachable-code -Wno-null-dereference %s
5 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core,deadcode.IdempotentOperations,experimental.core,osx.cocoa.AtSync -analyzer-store=region -analyzer-constraints=range -verify -fblocks -Wno-unreachable-code -Wno-null-dereference %s
7 #ifndef __clang_analyzer__
8 #error __clang__analyzer__ not defined
11 typedef struct objc_ivar *Ivar;
12 typedef struct objc_selector *SEL;
13 typedef signed char BOOL;
14 typedef int NSInteger;
15 typedef unsigned int NSUInteger;
16 typedef struct _NSZone NSZone;
17 @class NSInvocation, NSArray, NSMethodSignature, NSCoder, NSString, NSEnumerator;
19 - (BOOL)isEqual:(id)object;
23 - (id)copyWithZone:(NSZone *)zone;
25 @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end
27 - (void)encodeWithCoder:(NSCoder *)aCoder;
29 @interface NSObject <NSObject> {}
31 + (id)allocWithZone:(NSZone *)zone;
33 extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
34 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
36 + (id)stringWithUTF8String:(const char *)nullTerminatedCString;
37 @end extern NSString * const NSBundleDidLoadNotification;
38 @interface NSValue : NSObject <NSCopying, NSCoding>
39 - (void)getValue:(void *)value;
41 @interface NSNumber : NSValue
43 - (id)initWithBool:(BOOL)value;
45 @interface NSAssertionHandler : NSObject {}
46 + (NSAssertionHandler *)currentHandler;
47 - (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...;
49 extern NSString * const NSConnectionReplyMode;
50 typedef float CGFloat;
51 typedef struct _NSPoint {
55 typedef struct _NSSize {
59 typedef struct _NSRect {
64 // Reduced test case from crash in <rdar://problem/6253157>
67 - (void)foo:(void (^)(NSObject *x))block {
68 if (!((block != ((void *)0)))) {}
72 // Reduced test case from crash in PR 2796;
73 // http://llvm.org/bugs/show_bug.cgi?id=2796
75 unsigned foo(unsigned x) { return __alignof__((x)) + sizeof(x); }
77 // Improvement to path-sensitivity involving compound assignments.
78 // Addresses false positive in <rdar://problem/6268365>
81 unsigned r6268365Aux();
85 x &= r6268365Aux(); // expected-warning{{The left operand to '&=' is always 0}}
89 if (x == 0) x = x / j; // expected-warning{{Assigned value is always the same as the existing value}} expected-warning{{The right operand to '/' is always 1}}
92 void divzeroassume(unsigned x, unsigned j) {
94 if (j == 0) x /= 0; // no static-analyzer warning expected-warning {{division by zero is undefined}}
95 if (j == 0) x /= j; // no static-analyzer warning
96 if (j == 0) x = x / 0; // no static-analyzer warning expected-warning {{division by zero is undefined}}
99 void divzeroassumeB(unsigned x, unsigned j) {
101 if (j == 0) x /= 0; // no static-analyzer warning expected-warning {{division by zero is undefined}}
102 if (j == 0) x /= j; // no static-analyzer warning
103 if (j == 0) x = x / 0; // no static-analyzer warning expected-warning {{division by zero is undefined}}
106 // InitListExpr processing
108 typedef float __m128 __attribute__((__vector_size__(16), __may_alias__));
110 // This compound literal has a Vector type. We currently just
111 // return UnknownVal.
112 return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f };
115 typedef long long __v2di __attribute__ ((__vector_size__ (16)));
116 typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__));
117 __m128i vec128i(long long __q1, long long __q0) {
118 // This compound literal returns true for both isVectorType() and
120 return __extension__ (__m128i)(__v2di){ __q0, __q1 };
124 void check_zero_sized_VLA(int x) {
128 int vla[x]; // expected-warning{{Declared variable-length array (VLA) has zero size}}
131 void check_uninit_sized_VLA() {
133 int vla[x]; // expected-warning{{Declared variable-length array (VLA) uses a garbage value as its size}}
137 // - Tests a regression reported in PR 3211: http://llvm.org/bugs/show_bug.cgi?id=3211
138 void handle_sizeof_void(unsigned flag) {
142 if (sizeof(void) == 1)
145 *p = 1; // no-warning
154 *p = 1; // no-warning
158 *p = 1; // no-warning
161 // check deference of undefined values
162 void check_deref_undef(void) {
164 *p = 0xDEADBEEF; // expected-warning{{Dereference of undefined pointer value}}
168 void pr3422_helper(char *p);
172 pr3422_helper(&q[1]);
175 // PR 3543 (handle empty statement expressions)
180 // <rdar://problem/6611677>
181 // This test case test the use of a vector type within an array subscript
183 typedef long long __a64vector __attribute__((__vector_size__(8)));
184 typedef long long __a128vector __attribute__((__vector_size__(16)));
185 static inline __a64vector __attribute__((__always_inline__, __nodebug__))
186 my_test_mm_movepi64_pi64(__a128vector a) {
187 return (__a64vector)a[0];
190 // Test basic tracking of ivars associated with 'self'.
191 @interface SelfIvarTest : NSObject {
194 - (void)test_self_tracking;
197 @implementation SelfIvarTest
198 - (void)test_self_tracking {
206 c = *p; // no-warning
218 return p[0]; // no-warning
224 // - We just want to test that this doesn't crash the analyzer.
225 typedef struct st ST;
226 struct st { char *name; };
231 static ST *last_Cur_Pu;
232 if (last_Cur_Pu == Cur_Pu) {
237 // PR 3780 - This tests that StmtIterator isn't broken for VLAs in DeclGroups.
238 void pr3780(int sz) { typedef double MAT[sz][sz]; }
240 // <rdar://problem/6695527> - Test that we don't symbolicate doubles before
241 // we are ready to do something with them.
242 int rdar6695527(double x) {
243 if (!x) { return 0; }
247 // <rdar://problem/6708148> - Test that we properly invalidate structs
248 // passed-by-reference to a function.
249 void pr6708148_invalidate(NSRect *x);
250 void pr6708148_use(NSRect x);
251 void pr6708148_test(void) {
253 pr6708148_invalidate(&x);
254 pr6708148_use(x); // no-warning
257 // Handle both kinds of noreturn attributes for pruning paths.
258 void rdar_6777003_noret() __attribute__((noreturn));
259 void rdar_6777003_analyzer_noret() __attribute__((analyzer_noreturn));
261 void rdar_6777003(int x) {
265 rdar_6777003_noret();
266 *p = 1; // no-warning;
270 rdar_6777003_analyzer_noret();
271 *p = 1; // no-warning;
274 *p = 1; // expected-warning{{Dereference of null pointer}}
277 // For pointer arithmetic, --/++ should be treated as preserving non-nullness,
278 // regardless of how well the underlying StoreManager reasons about pointer
280 // <rdar://problem/6777209>
281 void rdar_6777209(char *p) {
287 // This branch should always be infeasible.
289 *p = 'c'; // no-warning
292 // PR 4033. A symbolic 'void *' pointer can be used as the address for a
294 typedef void *Opcode;
295 Opcode pr_4033_getOpcode();
297 void *lbl = &&next_opcode;
300 Opcode op = pr_4033_getOpcode();
305 // Test invalidating pointers-to-pointers with slightly different types. This
306 // example came from a recent false positive due to a regression where the
307 // branch condition was falsely reported as being uninitialized.
308 void invalidate_by_ref(char **x);
309 int test_invalidate_by_ref() {
311 invalidate_by_ref((char**) &y);
317 // Test for <rdar://problem/7027684>. This just tests that the CFG is
318 // constructed correctly. Previously, the successor block of the entrance
319 // was the block containing the merge for '?', which would trigger an
320 // assertion failure.
321 int rdar_7027684_aux();
322 int rdar_7027684_aux_2() __attribute__((noreturn));
323 void rdar_7027684(int x, int y) {
324 {}; // this empty compound statement is critical.
325 (rdar_7027684_aux() ? rdar_7027684_aux_2() : (void) 0);
328 // Test that we handle casts of string literals to arbitrary types.
329 unsigned const char *string_literal_test1() {
330 return (const unsigned char*) "hello";
333 const float *string_literal_test2() {
334 return (const float*) "hello";
337 // Test that we handle casts *from* incomplete struct types.
338 extern const struct _FooAssertStruct _cmd;
339 void test_cast_from_incomplete_struct_aux(volatile const void *x);
340 void test_cast_from_incomplete_struct() {
341 test_cast_from_incomplete_struct_aux(&_cmd);
344 // Test for <rdar://problem/7034511>
345 // "ValueManager::makeIntVal(uint64_t X, QualType T) should return a 'Loc'
346 // when 'T' is a pointer"
348 // Previously this case would crash.
349 void test_rdar_7034511(NSArray *y) {
352 if (x == ((void*) 0)) {}
355 // Handle casts of function pointers (CodeTextRegions) to arbitrary pointer
356 // types. This was previously causing a crash in CastRegion.
357 void handle_funcptr_voidptr_casts() {
361 typedef long INT_PTR, *PINT_PTR;
362 typedef INT_PTR (*FARPROC)();
363 FARPROC handle_funcptr_voidptr_casts_aux();
364 PVOID handle_funcptr_voidptr_casts_aux_2(PVOID volatile *x);
365 PVOID handle_funcptr_voidptr_casts_aux_3(PCHAR volatile *x);
367 ptr = (void**) handle_funcptr_voidptr_casts_aux();
368 handle_funcptr_voidptr_casts_aux_2(ptr);
369 handle_funcptr_voidptr_casts_aux_3(ptr);
372 // RegionStore::Retrieve previously crashed on this example. This example
373 // was previously in the test file 'xfail_regionstore_wine_crash.c'.
376 char *y = (char *) &x;
381 // RegionStoreManager previously crashed on this example. The problem is that
382 // the value bound to the field of b->grue after the call to testB_aux is
383 // a symbolic region. The second '*__gruep__' involves performing a load
384 // from a 'int*' that really is a 'void**'. The loaded location must be
385 // implicitly converted to an integer that wraps a location. Previosly we would
386 // get a crash here due to an assertion failure.
387 typedef struct _BStruct { void *grue; } BStruct;
388 void testB_aux(void *ptr);
389 void testB(BStruct *b) {
391 int *__gruep__ = ((int *)&((b)->grue));
392 int __gruev__ = *__gruep__;
393 testB_aux(__gruep__);
396 int *__gruep__ = ((int *)&((b)->grue));
397 int __gruev__ = *__gruep__;
398 if (~0 != __gruev__) {}
402 void test_trivial_symbolic_comparison(int *x) {
403 int test_trivial_symbolic_comparison_aux();
404 int a = test_trivial_symbolic_comparison_aux();
406 if (a != b) { // expected-warning{{Both operands to '!=' always have the same value}}
408 *p = 0xDEADBEEF; // no-warning
413 if (a != b) { // expected-warning{{Both operands to '!=' always have the same value}}
415 *p = 0xDEADBEEF; // no-warning
420 // <rdar://problem/7062158> false positive null dereference due to
421 // BasicStoreManager not tracking *static* globals
423 // This just tests the proper tracking of symbolic values for globals (both
424 // static and non-static).
426 static int* x_rdar_7062158;
427 void rdar_7062158() {
428 int *current = x_rdar_7062158;
429 if (current == x_rdar_7062158)
433 *p = 0xDEADBEEF; // no-warning
436 int* x_rdar_7062158_2;
437 void rdar_7062158_2() {
438 int *current = x_rdar_7062158_2;
439 if (current == x_rdar_7062158_2)
443 *p = 0xDEADBEEF; // no-warning
446 // This test reproduces a case for a crash when analyzing ClamAV using
447 // RegionStoreManager (the crash doesn't exhibit in BasicStoreManager because
448 // it isn't doing anything smart about arrays). The problem is that on the
449 // second line, 'p = &p[i]', p is assigned an ElementRegion whose index
450 // is a 16-bit integer. On the third line, a new ElementRegion is created
451 // based on the previous region, but there the region uses a 32-bit integer,
452 // resulting in a clash of values (an assertion failure at best). We resolve
453 // this problem by implicitly converting index values to 'int' when the
454 // ElementRegion is created.
455 unsigned char test_array_index_bitwidth(const unsigned char *p) {
456 unsigned short i = 0;
457 for (i = 0; i < 2; i++) p = &p[i];
461 // This case tests that CastRegion handles casts involving BlockPointerTypes.
462 // It should not crash.
463 void test_block_cast() {
464 id test_block_cast_aux();
465 (void (^)(void *))test_block_cast_aux(); // expected-warning{{expression result unused}}
468 int OSAtomicCompareAndSwap32Barrier();
470 // Test comparison of 'id' instance variable to a null void* constant after
471 // performing an OSAtomicCompareAndSwap32Barrier.
472 // This previously was a crash in RegionStoreManager.
473 @interface TestIdNull {
478 @implementation TestIdNull
480 OSAtomicCompareAndSwap32Barrier(0, (signed)2, (signed*)&x);
481 if (x == (void*) 0) { return 0; }
486 // PR 4594 - This was a crash when handling casts in SimpleSValuator.
493 // Test invalidation logic where an integer is casted to an array with a
494 // different sign and then invalidated.
495 void test_invalidate_cast_int() {
496 void test_invalidate_cast_int_aux(unsigned *i);
498 test_invalidate_cast_int_aux((unsigned*) &i);
503 int ivar_getOffset();
505 // Reduced from a crash involving the cast of an Objective-C symbolic region to
507 static NSNumber *test_ivar_offset(id self, SEL _cmd, Ivar inIvar) {
508 return [[[NSNumber allocWithZone:((void*)0)] initWithBool:*(_Bool *)((char *)self + ivar_getOffset(inIvar))] autorelease];
511 // Reduced from a crash in StoreManager::CastRegion involving a divide-by-zero.
512 // This resulted from not properly handling region casts to 'const void*'.
513 void test_cast_const_voidptr() {
519 // Reduced from a crash when analyzing Wine. This test handles loads from
520 // function addresses.
521 typedef long (*FARPROC)();
522 FARPROC test_load_func(FARPROC origfun) {
523 if (!*(unsigned char*) origfun)
528 // Test passing-by-value an initialized struct variable.
529 struct test_pass_val {
533 void test_pass_val_aux(struct test_pass_val s);
534 void test_pass_val() {
535 struct test_pass_val s;
538 test_pass_val_aux(s);
541 // This is a reduced test case of a false positive that previously appeared
542 // in RegionStoreManager. Previously the array access resulted in dereferencing
543 // an undefined value.
544 int test_array_compound(int *q, int *r, int *z) {
545 int *array[] = { q, r, z };
547 for (unsigned i = 0; i < 3 ; ++i)
548 if (*array[i]) ++j; // no-warning
552 // symbolic value stored in 'x' wouldn't be implicitly casted to a signed value
553 // during the comparison.
554 int rdar_7124210(unsigned int x) {
555 enum { SOME_CONSTANT = 123 };
556 int compare = ((signed) SOME_CONSTANT) == *((signed *) &x);
557 return compare ? 0 : 1; // Forces the evaluation of the symbolic constraint.
560 void pr4781(unsigned long *raw1) {
561 unsigned long *cook, *raw0;
562 unsigned long dough[32];
565 for( i = 0; i < 16; i++, raw1++ ) {
567 *cook = (*raw0 & 0x00fc0000L) << 6;
568 *cook |= (*raw0 & 0x00000fc0L) << 10;
572 // <rdar://problem/7185647> - 'self' should be treated as being non-null
573 // upon entry to an objective-c method.
574 @interface RDar7185647
577 @implementation RDar7185647
581 *((volatile int *) 0x0) = 0xDEADBEEF; // no-warning
586 // Test reasoning of __builtin_offsetof;
587 struct test_offsetof_A {
591 struct test_offsetof_B {
595 void test_offsetof_1() {
596 if (__builtin_offsetof(struct test_offsetof_A, x) ==
597 __builtin_offsetof(struct test_offsetof_B, w))
600 *p = 0xDEADBEEF; // no-warning
602 void test_offsetof_2() {
603 if (__builtin_offsetof(struct test_offsetof_A, y) ==
604 __builtin_offsetof(struct test_offsetof_B, z))
607 *p = 0xDEADBEEF; // no-warning
609 void test_offsetof_3() {
610 if (__builtin_offsetof(struct test_offsetof_A, y) -
611 __builtin_offsetof(struct test_offsetof_A, x)
613 __builtin_offsetof(struct test_offsetof_B, z) -
614 __builtin_offsetof(struct test_offsetof_B, w))
617 *p = 0xDEADBEEF; // no-warning
619 void test_offsetof_4() {
620 if (__builtin_offsetof(struct test_offsetof_A, y) ==
621 __builtin_offsetof(struct test_offsetof_B, w))
624 *p = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}}
627 // <rdar://problem/6829164> "nil receiver" false positive: make tracking
628 // of the MemRegion for 'self' path-sensitive
629 @interface RDar6829164 : NSObject {
636 double rdar_6829164_2();
638 @implementation RDar6829164
640 if((self = [super init]) != 0) {
641 id z = rdar_6829164_1();
644 x = rdar_6829164_2();
650 // <rdar://problem/7242015> - Invalidate values passed-by-reference
651 // to functions when the pointer to the value is passed as an integer.
652 void test_7242015_aux(unsigned long);
655 test_7242015_aux((unsigned long) &x); // no-warning
656 return x; // Previously we return and uninitialized value when
657 // using RegionStore.
660 // <rdar://problem/7242006> [RegionStore] compound literal assignment with
661 // floats not honored
662 CGFloat rdar7242006(CGFloat x) {
663 NSSize y = (NSSize){x, 10};
664 return y.width; // no-warning
667 // PR 4988 - This test exhibits a case where a function can be referenced
668 // when not explicitly used in an "lvalue" context (as far as the analyzer is
669 // concerned). This previously triggered a crash due to an invalid assertion.
671 pr_4988; // expected-warning{{expression result unused}}
674 // <rdar://problem/7152418> - A 'signed char' is used as a flag, which is
675 // implicitly converted to an int.
676 void *rdar7152418_bar();
677 @interface RDar7152418 {
682 @implementation RDar7152418
687 result = rdar7152418_bar();
691 result = rdar7152418_bar();
693 return *p; // no-warning
698 //===----------------------------------------------------------------------===//
699 // Test constant-folding of symbolic values, automatically handling type
700 // conversions of the symbol as necessary.
701 //===----------------------------------------------------------------------===//
703 // Previously this would crash once we started eagerly evaluating symbols whose
704 // values were constrained to a single value.
705 void test_symbol_fold_1(signed char x) {
707 if (x == ((signed char) 0)) {}
711 // This previously caused a crash because it triggered an assertion in APSInt.
712 void test_symbol_fold_2(unsigned int * p, unsigned int n,
713 const unsigned int * grumpkin, unsigned int dn) {
715 unsigned int tempsub[8];
716 unsigned int *solgrumpkin = tempsub + n;
717 for (i = 0; i < n; i++)
718 solgrumpkin[i] = (i < dn) ? ~grumpkin[i] : 0xFFFFFFFF;
719 for (i <<= 5; i < (n << 5); i++) {}
722 // This previously caused a crash because it triggered an assertion in APSInt.
723 // 'x' would evaluate to a 8-bit constant (because of the return value of
724 // test_symbol_fold_3_aux()) which would not get properly promoted to an
726 char test_symbol_fold_3_aux(void);
727 unsigned test_symbol_fold_3(void) {
728 unsigned x = test_symbol_fold_3_aux();
730 return (x << 8) | 0x5;
734 //===----------------------------------------------------------------------===//
735 // Tests for the warning of casting a non-struct type to a struct type
736 //===----------------------------------------------------------------------===//
738 typedef struct {unsigned int v;} NSSwappedFloat;
740 NSSwappedFloat test_cast_nonstruct_to_struct(float x) {
745 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}}
748 NSSwappedFloat test_cast_nonstruct_to_union(float x) {
753 return ((union bran *)&x)->sf; // no-warning
756 void test_undefined_array_subscript() {
758 int *p = &a[i]; // expected-warning{{Array subscript is undefined}}
762 //===----------------------------------------------------------------------===//
763 // Test using an uninitialized value as a branch condition.
764 //===----------------------------------------------------------------------===//
766 int test_uninit_branch(void) {
768 if (x) // expected-warning{{Branch condition evaluates to a garbage value}}
773 int test_uninit_branch_b(void) {
775 return x ? 1 : 0; // expected-warning{{Branch condition evaluates to a garbage value}}
778 int test_uninit_branch_c(void) {
780 if ((short)x) // expected-warning{{Branch condition evaluates to a garbage value}}
785 //===----------------------------------------------------------------------===//
786 // Test passing an undefined value in a message or function call.
787 //===----------------------------------------------------------------------===//
789 void test_bad_call_aux(int x);
790 void test_bad_call(void) {
792 test_bad_call_aux(y); // expected-warning{{Function call argument is an uninitialized value}}
795 @interface TestBadArg {}
796 - (void) testBadArg:(int) x;
799 void test_bad_msg(TestBadArg *p) {
801 [p testBadArg:y]; // expected-warning{{Argument in message expression is an uninitialized value}}
804 //===----------------------------------------------------------------------===//
805 // PR 6033 - Test emitting the correct output in a warning where we use '%'
806 // with operands that are undefined.
807 //===----------------------------------------------------------------------===//
811 return x % y; // expected-warning{{The right operand of '%' is a garbage value}}
821 struct trie* next[10];
825 typedef struct trie trie_t;
826 typedef struct kwset kwset_t;
828 void f(kwset_t *kws, char const *p, char const *q) {
829 struct trie const *trie;
830 struct trie * const *next = kws->next;
831 register unsigned char c;
832 register char const *end = p;
833 register char const *lim = q;
835 register unsigned char const *y = kws->y;
837 d = y[c = (end+=d)[-1]]; // no-warning
841 //===----------------------------------------------------------------------===//
842 // <rdar://problem/7593875> When handling sizeof(VLA) it leads to a hole in
843 // the ExplodedGraph (causing a false positive)
844 //===----------------------------------------------------------------------===//
846 int rdar_7593875_aux(int x);
847 int rdar_7593875(int n) {
848 int z[n > 10 ? 10 : n]; // VLA.
850 v = rdar_7593875_aux(sizeof(z));
851 // Previously we got a false positive about 'v' being uninitialized.
852 return v; // no-warning
855 //===----------------------------------------------------------------------===//
856 // Handle casts from symbolic regions (packaged as integers) to doubles.
857 // Previously this caused an assertion failure.
858 //===----------------------------------------------------------------------===//
860 void *foo_rev95119();
861 void baz_rev95119(double x);
862 void bar_rev95119() {
863 // foo_rev95119() returns a symbolic pointer. It is then
864 // cast to an int which is then cast to a double.
865 int value = (int) foo_rev95119();
866 baz_rev95119((double)value);
869 //===----------------------------------------------------------------------===//
870 // Handle loading a symbolic pointer from a symbolic region that was
871 // invalidated by a call to an unknown function.
872 //===----------------------------------------------------------------------===//
874 void bar_rev95192(int **x);
875 void foo_rev95192(int **x) {
878 // Not a null dereference.
879 **x = 1; // no-warning
882 //===----------------------------------------------------------------------===//
883 // Handle casts of a function to a function pointer with a different return
884 // value. We don't yet emit an error for such cases, but we now we at least
885 // don't crash when the return value gets interpreted in a way that
886 // violates our invariants.
887 //===----------------------------------------------------------------------===//
889 void *foo_rev95267();
891 char (*Callback_rev95267)(void) = (char (*)(void)) foo_rev95267;
892 if ((*Callback_rev95267)() == (char) 0)
897 // Same as previous case, but handle casts to 'void'.
899 void (*Callback_rev95274)(void) = (void (*)(void)) foo_rev95267;
900 (*Callback_rev95274)();
904 void rdar7582031_test_static_init_zero() {
911 void rdar7582031_test_static_init_zero_b() {
919 //===----------------------------------------------------------------------===//
920 // Test handling of parameters that are structs that contain floats and //
922 //===----------------------------------------------------------------------===//
924 struct s_rev95547_nested { float x, y; };
926 struct s_rev95547_nested z1;
927 struct s_rev95547_nested z2;
929 float foo_rev95547(struct s_rev95547 w) {
930 return w.z1.x + 20.0; // no-warning
932 void foo_rev95547_b(struct s_rev95547 w) {
933 struct s_rev95547 w2 = w;
934 w2.z1.x += 20.0; // no-warning
937 //===----------------------------------------------------------------------===//
938 // Test handling statement expressions that don't populate a CFG block that
939 // is used to represent the computation of the RHS of a logical operator.
940 // This previously triggered a crash.
941 //===----------------------------------------------------------------------===//
952 if (1 && *({ // expected-warning{{Dereference of null pointer}}
961 //===----------------------------------------------------------------------===//
962 // <rdar://problem/7979430> - The CFG for code containing an empty
963 // @synchronized block was previously broken (and would crash the analyzer).
964 //===----------------------------------------------------------------------===//
966 void r7979430(id x) {
970 //===----------------------------------------------------------------------===
971 // PR 7361 - Test that functions wrapped in macro instantiations are analyzed.
972 //===----------------------------------------------------------------------===
973 #define MAKE_TEST_FN() \
974 void test_pr7361 (char a) {\
975 char* b = 0x0; *b = a;\
978 MAKE_TEST_FN() // expected-warning{{null pointer}}
980 //===----------------------------------------------------------------------===
981 // PR 7491 - Test that symbolic expressions can be used as conditions.
982 //===----------------------------------------------------------------------===
993 // Should be unreachable
994 (void)*(char*)0; // no-warning
998 //===----------------------------------------------------------------------===
999 // PR 7475 - Test that assumptions about global variables are reset after
1000 // calling a global function.
1001 //===----------------------------------------------------------------------===
1003 int *pr7475_someGlobal;
1004 void pr7475_setUpGlobal();
1007 if (pr7475_someGlobal == 0)
1008 pr7475_setUpGlobal();
1009 *pr7475_someGlobal = 0; // no-warning
1012 void pr7475_warn() {
1013 static int *someStatic = 0;
1014 if (someStatic == 0)
1015 pr7475_setUpGlobal();
1016 *someStatic = 0; // expected-warning{{null pointer}}
1019 // <rdar://problem/8202272> - __imag passed non-complex should not crash
1020 float f0(_Complex float x) {
1021 float l0 = __real x;
1022 return __real l0 + __imag l0;
1026 //===----------------------------------------------------------------------===
1027 // Test that we can reduce symbols to constants whether they are on the left
1028 // or right side of an expression.
1029 //===----------------------------------------------------------------------===
1031 void reduce_to_constant(int x, int y) {
1038 if (y == -20 && a != 0)
1039 (void)*(char*)0; // no-warning
1040 if (y == -20 && b != 0)
1041 (void)*(char*)0; // no-warning
1044 // <rdar://problem/8360854> - Test that code after a switch statement with no
1045 // 'case:' labels is correctly evaluated.
1046 void r8360854(int n) {
1051 *p = 0xDEADBEEF; // expected-warning{{null pointer}}
1054 // PR 8050 - crash in CastSizeChecker when pointee is an incomplete type
1055 typedef long unsigned int __darwin_size_t;
1056 typedef __darwin_size_t size_t;
1057 void *malloc(size_t);
1061 void pr8050(struct PR8050 **arg)
1066 // <rdar://problem/5880430> Switch on enum should not consider default case live
1067 // if all enum values are covered
1068 enum Cases { C1, C2, C3, C4 };
1069 void test_enum_cases(enum Cases C) {
1078 *p = 0xDEADBEEF; // no-warning
1081 void test_enum_cases_positive(enum Cases C) {
1082 switch (C) { // expected-warning{{enumeration value 'C4' not handled in switch}}
1089 *p = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}}
1092 // <rdar://problem/6351970> rule request: warn if synchronization mutex can be nil
1093 void rdar6351970() {
1095 @synchronized(x) {} // expected-warning{{Nil value used as mutex for @synchronized() (no synchronization will occur)}}
1098 void rdar6351970_b(id x) {
1100 @synchronized(x) {} // expected-warning{{Nil value used as mutex for @synchronized() (no synchronization will occur)}}
1103 void rdar6351970_c() {
1105 @synchronized(x) {} // expected-warning{{Uninitialized value used as mutex for @synchronized}}
1108 @interface Rdar8578650
1112 void rdar8578650(id x) {
1116 // At this point we should assume that 'x' is not nil, not
1118 @synchronized (x) { // no-warning
1122 // <rdar://problem/6352035> rule request: direct structure member access null pointer dereference
1123 @interface RDar6352035 {
1130 @implementation RDar6352035
1132 RDar6352035 *friend = 0;
1133 friend->c = 7; // expected-warning{{Instance variable access (via 'friend') results in a null pointer dereference}}
1137 c = 7; // expected-warning{{Instance variable access (via 'self') results in a null pointer dereference}}
1141 // PR 8149 - GNU statement expression in condition of ForStmt.
1142 // This previously triggered an assertion failure in CFGBuilder.
1144 for (; ({ do { } while (0); 0; });) { }
1147 // PR 8458 - Make sure @synchronized doesn't crash with properties.
1148 @interface PR8458 {}
1149 @property(readonly) id lock;
1153 void __PR8458(PR8458 *x) {
1154 @synchronized(x.lock) {} // no-warning
1157 // PR 8440 - False null dereference during store to array-in-field-in-global.
1158 // This test case previously resulted in a bogus null deref warning from
1159 // incorrect lazy symbolication logic in RegionStore.
1166 char **bar_pr8440();
1167 void baz_pr8440(int n)
1169 saved_pr8440.num = n;
1170 if (saved_pr8440.data)
1172 saved_pr8440.data = bar_pr8440();
1173 for (int i = 0 ; i < n ; i ++)
1174 saved_pr8440.data[i] = foo_pr8440(); // no-warning
1177 // Support direct accesses to non-null memory. Reported in:
1179 // <rdar://problem/6839683>
1180 int test_direct_address_load() {
1181 int *p = (int*) 0x4000;
1182 return *p; // no-warning
1185 void pr5272_test() {
1186 struct pr5272 { int var2; };
1187 (*(struct pr5272*)0xBC000000).var2 = 0; // no-warning
1188 (*(struct pr5272*)0xBC000000).var2 += 2; // no-warning
1191 // Support casting the return value of function to another different type
1192 // This previously caused a crash, although we likely need more precise
1193 // reasoning here. <rdar://problem/8663544>
1194 void* rdar8663544();
1195 typedef struct {} Val8663544;
1196 Val8663544 bazR8663544() {
1197 Val8663544(*func) () = (Val8663544(*) ()) rdar8663544;
1201 // PR 8619 - Handle ternary expressions with a call to a noreturn function.
1202 // This previously resulted in a crash.
1203 void pr8619_noreturn(int x) __attribute__((noreturn));
1205 void pr8619(int a, int b, int c) {
1206 a ?: pr8619_noreturn(b || c);
1210 // PR 8646 - crash in the analyzer when handling unions.
1211 union pr8648_union {
1212 signed long long pr8648_union_field;
1216 union pr8648_union x = { .pr8648_union_field = 0LL };
1217 y = x.pr8648_union_field;
1219 union pr8648_union z;
1220 z = (union pr8648_union) { .pr8648_union_field = 0LL };
1222 union pr8648_union w;
1223 w = ({ (union pr8648_union) { .pr8648_union_field = 0LL }; });
1225 // crash, no assignment
1226 (void) ({ (union pr8648_union) { .pr8648_union_field = 0LL }; }).pr8648_union_field;
1228 // crash with assignment
1229 y = ({ (union pr8648_union) { .pr8648_union_field = 0LL }; }).pr8648_union_field;
1232 // PR 9269 - don't assert when building the following CFG. The for statement
1233 // contains a condition with multiple basic blocks, and the value of the
1234 // statement expression is then indexed as part of a bigger condition expression.
1235 // This example exposed a bug in child traversal in the CFGBuilder.
1237 struct s { char *bar[10]; } baz[2] = { 0 };
1240 (* ({ while(0); ({ &baz[0]; }); })).bar[0] != 0;
1244 // Test evaluation of GNU-style ?:.
1245 int pr9287(int type) { return type ? : 0; } // no-warning
1247 void pr9287_b(int type, int *p) {
1253 *p = 0xDEADBEEF; // expected-warning {{null pointer}}
1257 void pr9287_c(int type, int *p) {
1263 *p = 0xDEADBEEF; // no-warning
1267 void test_switch() {
1271 *p = 0xDEADBEEF; // no-warning
1276 *p = 0xDEADBEEF; // expected-warning {{null}}
1281 *p = 0xDEADBEEF; // no-warning
1287 // PR 9467. Tests various CFG optimizations. This previously crashed.
1288 static void test(unsigned int bit_mask)
1290 unsigned int bit_index;
1294 switch ((0x01 << bit_index) & bit_mask) {
1300 // Don't crash on code containing __label__.
1301 int radar9414427_aux();
1302 void radar9414427() {
1304 if (radar9414427_aux()) {
1310 // Analyze methods in @implementation (category)
1311 @interface RDar9465344
1314 @implementation RDar9465344 (MyCategory)
1315 - (void) testcategoryImpl {
1317 *p = 0xDEADBEEF; // expected-warning {{null}}
1321 @implementation RDar9465344