1 // RUN: %clang_analyze_cc1 -triple i386-apple-darwin9 -analyzer-checker=core,alpha.core.CastToStruct,alpha.security.ReturnPtrRange,alpha.security.ArrayBound -verify -fblocks -Wno-objc-root-class -Wno-strict-prototypes -Wno-error=implicit-function-declaration %s
2 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin9 -DTEST_64 -analyzer-checker=core,alpha.core.CastToStruct,alpha.security.ReturnPtrRange,alpha.security.ArrayBound -verify -fblocks -Wno-objc-root-class -Wno-strict-prototypes -Wno-error=implicit-function-declaration %s
4 typedef long unsigned int size_t;
5 void *memcpy(void *, const void *, size_t);
8 typedef struct objc_selector *SEL;
9 typedef signed char BOOL;
10 typedef int NSInteger;
11 typedef unsigned int NSUInteger;
12 typedef struct _NSZone NSZone;
13 @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
14 @protocol NSObject - (BOOL)isEqual:(id)object; @end
15 @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end
16 @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end
17 @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end
18 @interface NSObject <NSObject> {} - (id)init; @end
19 extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
20 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
22 + (id)stringWithUTF8String:(const char *)nullTerminatedCString;
23 @end extern NSString * const NSBundleDidLoadNotification;
24 @interface NSAssertionHandler : NSObject {}
25 + (NSAssertionHandler *)currentHandler;
26 - (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...;
28 extern NSString * const NSConnectionReplyMode;
31 typedef long long int64_t;
32 typedef int64_t intptr_t;
35 typedef int32_t intptr_t;
38 //---------------------------------------------------------------------------
39 // Test case 'checkaccess_union' differs for region store and basic store.
40 // The basic store doesn't reason about compound literals, so the code
41 // below won't fire an "uninitialized value" warning.
42 //---------------------------------------------------------------------------
44 // PR 2948 (testcase; crash on VisitLValue for union types)
45 // http://llvm.org/bugs/show_bug.cgi?id=2948
46 void checkaccess_union(void) {
48 // Since RegionStore doesn't handle unions yet,
49 // this branch condition won't be triggered
50 // as involving an uninitialized value.
51 if (((((__extension__ (((union { // no-warning
52 __typeof (status) __in; int __i;}
56 ).__i))) & 0xff00) >> 8) == 1)
60 // Check our handling of fields being invalidated by function calls.
61 struct test2_struct { int x; int y; char* s; };
62 void test2_help(struct test2_struct* p);
65 struct test2_struct s;
84 // BasicStore handles this case incorrectly because it doesn't reason about
85 // the value pointed to by 'x' and thus creates different symbolic values
86 // at the declarations of 'a' and 'b' respectively. RegionStore handles
87 // it correctly. See the companion test in 'misc-ps-basic-store.m'.
88 void test_trivial_symbolic_comparison_pointer_parameter(int *x) {
93 *p = 0xDEADBEEF; // no-warning
97 // This is a modified test from 'misc-ps.m'. Here we have the extra
98 // NULL dereferences which are pruned out by RegionStore's symbolic reasoning
100 typedef struct _BStruct { void *grue; } BStruct;
101 void testB_aux(void *ptr);
103 void testB(BStruct *b) {
105 int *__gruep__ = ((int *)&((b)->grue));
106 int __gruev__ = *__gruep__;
107 int __gruev2__ = *__gruep__;
108 if (__gruev__ != __gruev2__) {
110 *p = 0xDEADBEEF; // no-warning
113 testB_aux(__gruep__);
116 int *__gruep__ = ((int *)&((b)->grue));
117 int __gruev__ = *__gruep__;
118 int __gruev2__ = *__gruep__;
119 if (__gruev__ != __gruev2__) {
121 *p = 0xDEADBEEF; // no-warning
124 if (~0 != __gruev__) {}
128 void testB_2(BStruct *b) {
130 int **__gruep__ = ((int **)&((b)->grue));
131 int *__gruev__ = *__gruep__;
132 testB_aux(__gruep__);
135 int **__gruep__ = ((int **)&((b)->grue));
136 int *__gruev__ = *__gruep__;
137 if ((int*)~0 != __gruev__) {}
141 // This test case is a reduced case of a caching bug discovered by an
142 // assertion failure in RegionStoreManager::BindArray. Essentially the
143 // DeclStmt is evaluated twice, but on the second loop iteration the
144 // engine caches out. Previously a false transition would cause UnknownVal
145 // to bind to the variable, firing an assertion failure. This bug was fixed
147 void test_declstmt_caching(void) {
150 const char a[] = "I like to crash";
155 //===----------------------------------------------------------------------===//
156 // Basically a null check is performed on the field value, which is then
157 // assigned to a variable and then checked again.
158 //===----------------------------------------------------------------------===//
159 struct s_7114618 { int *p; };
160 void test_rdar_7114618(struct s_7114618 *s) {
166 *dead = 0xDEADBEEF; // no-warning
171 // Test pointers increment correctly.
179 *q = 3; // no-warning
183 //===----------------------------------------------------------------------===//
184 // Bit-fields of a struct should be invalidated when blasting the entire
185 // struct with an integer constant.
186 //===----------------------------------------------------------------------===//
187 struct test_7185607 {
191 int rdar_test_7185607(void) {
192 struct test_7185607 s; // Uninitialized.
193 *((unsigned *) &s) = 0U;
194 return s.x; // no-warning
197 //===----------------------------------------------------------------------===//
198 // [RegionStore] compound literal assignment with floats not honored
199 // This test case is mirrored in misc-ps.m, but this case is a negative.
200 //===----------------------------------------------------------------------===//
201 typedef float CGFloat;
202 typedef struct _NSSize {
207 CGFloat rdar7242006_negative(CGFloat x) {
209 return y.width; // expected-warning{{garbage}}
212 //===----------------------------------------------------------------------===//
213 // Allow binding of values to symbolic regions. This test case shows how
214 // RegionStore tracks the value bound to 'x' after the assignment.
215 //===----------------------------------------------------------------------===//
216 typedef int* ptr_rdar_7249340;
217 void rdar_7249340(ptr_rdar_7249340 x) {
221 int *p = 0; // This is unreachable.
222 *p = 0xDEADBEEF; // no-warning
225 //===----------------------------------------------------------------------===//
226 // This test case tests both value tracking of array values and that we handle
227 // symbolic values that are casted between different integer types. Note the
228 // assignment 'n = *a++'; here 'n' is and 'int' and '*a' is 'unsigned'.
229 // Previously we got a false positive at 'x += *b++' (undefined value) because
230 // we got a false path.
231 //===----------------------------------------------------------------------===//
232 int rdar_7249327_aux(void);
234 void rdar_7249327(unsigned int A[2*32]) {
247 *b++ = rdar_7249327_aux();
254 x += *b++; // no-warning
257 //===----------------------------------------------------------------------===//
258 // Check that 'x' is invalidated because its address is passed in as a value to
260 //===----------------------------------------------------------------------===//
261 struct doodad_6914474 { int *v; };
262 extern void prod_6914474(struct doodad_6914474 *d);
263 int rdar_6914474(void) {
265 struct doodad_6914474 d;
268 return x; // no-warning
271 // Test invalidation of a single field.
272 struct s_test_field_invalidate {
275 extern void test_invalidate_field(int *x);
276 int test_invalidate_field_test(void) {
277 struct s_test_field_invalidate y;
278 test_invalidate_field(&y.x);
279 return y.x; // no-warning
281 int test_invalidate_field_test_positive(void) {
282 struct s_test_field_invalidate y;
283 return y.x; // expected-warning{{garbage}}
286 // This test case illustrates how a typeless array of bytes casted to a
287 // struct should be treated as initialized. RemoveDeadBindings previously
288 // had a bug that caused 'x' to lose its default symbolic value after the
289 // assignment to 'p', thus causing 'p->z' to evaluate to "undefined".
290 struct ArrayWrapper { unsigned char y[16]; };
291 struct WrappedStruct { unsigned z; };
293 void test_handle_array_wrapper_helper();
295 int test_handle_array_wrapper(void) {
296 struct ArrayWrapper x;
297 test_handle_array_wrapper_helper(&x);
298 struct WrappedStruct *p = (struct WrappedStruct*) x.y; // expected-warning{{Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption}}
299 return p->z; // no-warning
302 //===----------------------------------------------------------------------===//
303 // [RegionStore] crash when handling load: '*((unsigned int *)"????")'
304 //===----------------------------------------------------------------------===//
306 int rdar_7261075(void) {
307 unsigned int var = 0;
308 if (var == *((unsigned int *)"????"))
313 //===----------------------------------------------------------------------===//
314 // False path due to limited pointer arithmetic constraints.
315 //===----------------------------------------------------------------------===//
317 void rdar_7275774(void *data, unsigned n) {
318 if (!(data || n == 0))
321 unsigned short *p = (unsigned short*) data;
322 unsigned short *q = p + (n / 2);
325 // If we reach here, 'p' cannot be null. If 'p' is null, then 'n' must
326 // be '0', meaning that this branch is not feasible.
327 *p = *q; // no-warning
331 //===----------------------------------------------------------------------===//
332 // Test that Objective-C instance variables aren't prematurely pruned
333 // from the analysis state.
334 //===----------------------------------------------------------------------===//
336 struct rdar_7312221_value { int x; };
338 @interface RDar7312221
340 struct rdar_7312221_value *y;
342 - (void) doSomething_7312221;
345 extern struct rdar_7312221_value *rdar_7312221_helper(void);
346 extern int rdar_7312221_helper_2(id o);
347 extern void rdar_7312221_helper_3(int z);
349 @implementation RDar7312221
350 - (void) doSomething_7312221 {
352 y = rdar_7312221_helper();
354 y->x = rdar_7312221_helper_2(self);
355 // The following use of 'y->x' previously triggered a null dereference, as the value of 'y'
356 // before 'y = rdar_7312221_helper()' would be used.
357 rdar_7312221_helper_3(y->x); // no-warning
363 struct rdar_7312221_container {
364 struct rdar_7312221_value *y;
367 extern int rdar_7312221_helper_4(struct rdar_7312221_container *s);
369 // This test case essentially matches the one in [RDar7312221 doSomething_7312221].
370 void doSomething_7312221_with_struct(struct rdar_7312221_container *Self) {
372 Self->y = rdar_7312221_helper();
374 Self->y->x = rdar_7312221_helper_4(Self);
375 rdar_7312221_helper_3(Self->y->x); // no-warning
380 //===----------------------------------------------------------------------===//
381 // Just more tests cases for regions
382 //===----------------------------------------------------------------------===//
384 void rdar_7332673_test1(void) {
386 if ( *(value) != 1 ) {} // expected-warning{{The left operand of '!=' is a garbage value}}
388 int rdar_7332673_test2_aux(char *x);
389 void rdar_7332673_test2(void) {
391 if ( rdar_7332673_test2_aux(value) != 1 ) {} // expected-warning{{1st function call argument is an uninitialized value}}
394 //===----------------------------------------------------------------------===//
395 // Because of a bug in RegionStoreManager::RemoveDeadBindings(), the symbol for
396 // s->session->p would incorrectly be pruned from the state after the call to
397 // rdar7347252_malloc1(), and would incorrectly result in a warning about
398 // passing a null pointer to rdar7347252_memcpy().
399 //===----------------------------------------------------------------------===//
401 struct rdar7347252_AA { char *p;};
403 struct rdar7347252_AA *session;
408 int rdar7347252_f(rdar7347252_SSL1 *s);
409 char *rdar7347252_malloc1(int);
410 char *rdar7347252_memcpy1(char *d, char *s, int n) __attribute__((nonnull (1,2)));
412 int rdar7347252(rdar7347252_SSL1 *s) {
413 rdar7347252_f(s); // the SymbolicRegion of 's' is set a default binding of conjured symbol
414 if (s->session->p == ((void*)0)) {
415 if ((s->session->p = rdar7347252_malloc1(10)) == ((void*)0)) {
418 rdar7347252_memcpy1(s->session->p, "aa", 2); // no-warning
423 //===----------------------------------------------------------------------===//
424 // PR 5316 - "crash when accessing field of lazy compound value"
425 // Previously this caused a crash at the MemberExpr '.chr' when loading
426 // a field value from a LazyCompoundVal
427 //===----------------------------------------------------------------------===//
429 typedef unsigned int pr5316_wint_t;
430 typedef pr5316_wint_t pr5316_REFRESH_CHAR;
432 pr5316_REFRESH_CHAR chr;
434 pr5316_REFRESH_ELEMENT;
435 static void pr5316(pr5316_REFRESH_ELEMENT *dst, const pr5316_REFRESH_ELEMENT *src) {
436 while ((*dst++ = *src++).chr != L'\0') ;
439 //===----------------------------------------------------------------------===//
440 // Exercise creating ElementRegion with symbolic super region.
441 //===----------------------------------------------------------------------===//
442 void element_region_with_symbolic_superregion(int* p) {
448 (void)*x; // no-warning
451 //===----------------------------------------------------------------------===//
452 // Test returning an out-of-bounds pointer (CWE-466)
453 //===----------------------------------------------------------------------===//
455 static int test_cwe466_return_outofbounds_pointer_a[10]; // expected-note{{Original object declared here}}
456 int *test_cwe466_return_outofbounds_pointer(void) {
457 int *p = test_cwe466_return_outofbounds_pointer_a+11;
458 return p; // expected-warning{{Returned pointer value points outside the original object}}
459 // expected-note@-1{{Original object 'test_cwe466_return_outofbounds_pointer_a' is an array of 10 'int' objects, returned pointer points at index 11}}
462 //===----------------------------------------------------------------------===//
463 // PR 3135 - Test case that shows that a variable may get invalidated when its
464 // address is included in a structure that is passed-by-value to an unknown function.
465 //===----------------------------------------------------------------------===//
467 typedef struct { int *a; } pr3135_structure;
468 int pr3135_bar(pr3135_structure *x);
471 pr3135_structure y = { &x };
472 // the call to pr3135_bar may initialize x
473 if (pr3135_bar(&y) && x) // no-warning
478 //===----------------------------------------------------------------------===//
479 // Test that we handle compound initializers with partially unspecified array
480 // values. Previously this caused a crash.
481 //===----------------------------------------------------------------------===//
483 typedef struct RDar7403269 {
488 void rdar7403269(void) {
489 RDar7403269 z = { .y = 0 };
493 *p = 0xDEADBEEF; // no-warning
496 typedef struct RDar7403269_b {
497 struct zorg { int w; int k; } x[10];
501 void rdar7403269_b(void) {
502 RDar7403269_b z = { .y = 0 };
506 *p = 0xDEADBEEF; // no-warning
509 void rdar7403269_b_pos(void) {
510 RDar7403269_b z = { .y = 0 };
514 *p = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}}
518 //===----------------------------------------------------------------------===//
519 // Test that incrementing a non-null pointer results in a non-null pointer.
520 //===----------------------------------------------------------------------===//
522 void test_increment_nonnull_rdar_7191542(const char *path) {
526 // When using basic-store, we get a null dereference here because we lose information
527 // about path after the pointer increment.
528 char c = *path++; // no-warning
538 //===----------------------------------------------------------------------===//
539 // Test that the store (implicitly) tracks values for doubles/floats that are
541 //===----------------------------------------------------------------------===//
543 double rdar_6811085(void) {
545 return u + 10; // expected-warning{{The left operand of '+' is a garbage value}}
548 //===----------------------------------------------------------------------===//
549 // Path-sensitive tests for blocks.
550 //===----------------------------------------------------------------------===//
552 void indirect_block_call(void (^f)(void));
554 int blocks_1(int *p, int z) {
556 void (^bar)(void) = ^{ q = p; };
559 // The call to 'bar' might cause 'q' to be invalidated.
561 *q = 0x1; // no-warning
564 // The function 'indirect_block_call' might invoke bar, thus causing
565 // 'q' to possibly be invalidated.
566 indirect_block_call(bar);
567 *q = 0x1; // no-warning
570 *q = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}}
575 int blocks_2(int *p, int z) {
577 void (^bar)(int **) = ^(int **r){ *r = p; };
580 // The call to 'bar' might cause 'q' to be invalidated.
582 *q = 0x1; // no-warning
585 *q = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}}
590 // Test that the value of 'x' is considered invalidated after the block
591 // is passed as an argument to the message expression.
592 typedef void (^RDar7582031CB)(void);
593 @interface RDar7582031
594 - rdar7582031:RDar7582031CB;
595 - rdar7582031_b:RDar7582031CB;
598 // Test with one block.
599 unsigned rdar7582031(RDar7582031 *o) {
601 [o rdar7582031:^{ x = 1; }];
602 return x; // no-warning
605 // Test with two blocks.
606 unsigned long rdar7582031_b(RDar7582031 *o) {
608 __block unsigned long x;
609 [o rdar7582031:^{ y = 1; }];
610 [o rdar7582031_b:^{ x = 1LL; }];
611 return x + (unsigned long) y; // no-warning
614 // Show we get an error when 'o' is null because the message
615 // expression has no effect.
616 unsigned long rdar7582031_b2(RDar7582031 *o) {
618 __block unsigned long x;
621 [o rdar7582031:^{ y = 1; }];
622 [o rdar7582031_b:^{ x = 1LL; }];
623 return x + (unsigned long) y; // expected-warning{{The left operand of '+' is a garbage value}}
626 // Show that we handle static variables also getting invalidated.
627 void rdar7582031_aux(void (^)(void));
628 RDar7582031 *rdar7582031_aux_2(void);
630 unsigned rdar7582031_static(void) {
631 static RDar7582031 *o = 0;
632 rdar7582031_aux(^{ o = rdar7582031_aux_2(); });
635 [o rdar7582031:^{ x = 1; }];
636 return x; // no-warning
639 //===----------------------------------------------------------------------===//
640 // Test that variables passed using __blocks are not treated as being
642 //===----------------------------------------------------------------------===//
644 typedef void (^RDar_7462324_Callback)(id obj);
646 @interface RDar7462324
647 - (void) foo:(id)target;
648 - (void) foo_positive:(id)target;
652 @implementation RDar7462324
653 - (void) foo:(id)target {
654 __block RDar_7462324_Callback builder = ((void*) 0);
655 builder = ^(id object) {
657 builder(self); // no-warning
662 - (void) foo_positive:(id)target {
663 __block RDar_7462324_Callback builder = ((void*) 0);
664 builder = ^(id object) {
667 builder(x); // expected-warning{{1st block call argument is an uninitialized value}}
674 //===----------------------------------------------------------------------===//
675 // Scanning for live variables within a block should not crash on variables
676 // passed by reference via __block.
677 //===----------------------------------------------------------------------===//
679 int rdar7468209_aux(void);
680 void rdar7468209_aux_2(void);
682 void rdar7468209(void) {
685 x = rdar7468209_aux();
686 // We need a second statement so that 'x' would be removed from the store if it wasn't
687 // passed by reference.
692 //===----------------------------------------------------------------------===//
693 // PR 5857 - Test loading an integer from a byte array that has also been
694 // reinterpreted to be loaded as a field.
695 //===----------------------------------------------------------------------===//
697 typedef struct { int x; } TestFieldLoad;
698 int pr5857(char *src) {
699 TestFieldLoad *tfl = (TestFieldLoad *) (intptr_t) src;
701 long long *z = (long long *) (intptr_t) src;
704 for (n = 0; n < y; ++n) {
705 // Previously we crashed analyzing this statement.
711 //===----------------------------------------------------------------------===//
712 // PR 4358 - Without field-sensitivity, this code previously triggered
713 // a false positive that 'uninit' could be uninitialized at the call
715 //===----------------------------------------------------------------------===//
721 void pr4358_aux(int x);
722 void pr4358(struct pr4358 *pnt) {
726 } else if (pnt->baz > 2) {
728 } else if (pnt->baz <= 2) {
731 pr4358_aux(uninit); // no-warning
734 //===----------------------------------------------------------------------===//
735 // Test handling fields of values returned from function calls or
736 // message expressions.
737 //===----------------------------------------------------------------------===//
739 typedef struct testReturn_rdar_7526777 {
742 } testReturn_rdar_7526777;
744 @interface TestReturnStruct_rdar_7526777
745 - (testReturn_rdar_7526777) foo;
748 int test_return_struct(TestReturnStruct_rdar_7526777 *x) {
752 testReturn_rdar_7526777 test_return_struct_2_aux_rdar_7526777(void);
754 int test_return_struct_2_rdar_7526777(void) {
755 return test_return_struct_2_aux_rdar_7526777().x;
758 //===----------------------------------------------------------------------===//
759 // Assertion failed: (Op == BinaryOperator::Add || Op == BinaryOperator::Sub)
760 // This test case previously triggered an assertion failure due to a discrepancy
761 // been the loaded/stored value in the array
762 //===----------------------------------------------------------------------===//
764 _Bool OSAtomicCompareAndSwapPtrBarrier( void *__oldValue, void *__newValue, void * volatile *__theValue );
766 void rdar_7527292(void) {
767 static id Cache7527292[32];
768 for (signed long idx = 0;
771 id v = Cache7527292[idx];
772 if (v && OSAtomicCompareAndSwapPtrBarrier(v, ((void*)0), (void * volatile *)(Cache7527292 + idx))) {
777 //===----------------------------------------------------------------------===//
778 // Handle initialization of incomplete arrays in structures using a compound
779 // value. Previously this crashed.
780 //===----------------------------------------------------------------------===//
782 struct rdar_7515938 {
787 const struct rdar_7515938 *rdar_7515938(void) {
788 static const struct rdar_7515938 z = { 0, { 1, 2 } };
791 *p = 0xDEADBEEF; // no-warning
796 struct rdar_7515938_str {
801 const struct rdar_7515938_str *rdar_7515938_str(void) {
802 static const struct rdar_7515938_str z = { 0, "hello" };
806 //===----------------------------------------------------------------------===//
807 // Assorted test cases from PR 4172.
808 //===----------------------------------------------------------------------===//
810 struct PR4172A_s { int *a; };
812 void PR4172A_f2(struct PR4172A_s *p);
814 int PR4172A_f1(void) {
819 return b[3]; // no-warning
822 struct PR4172B_s { int *a; };
824 void PR4172B_f2(struct PR4172B_s *p);
826 int PR4172B_f1(void) {
831 return x; // no-warning
834 //===----------------------------------------------------------------------===//
835 // Test invalidation of values in struct literals.
836 //===----------------------------------------------------------------------===//
838 struct s_rev96062 { int *x; int *y; };
839 struct s_rev96062_nested { struct s_rev96062 z; };
841 void test_a_rev96062_aux(struct s_rev96062 *s);
842 void test_a_rev96062_aux2(struct s_rev96062_nested *s);
844 int test_a_rev96062(void) {
846 struct s_rev96062 x = { &a, &b };
847 test_a_rev96062_aux(&x);
848 return a + b; // no-warning
850 int test_b_rev96062(void) {
852 struct s_rev96062 x = { &a, &b };
853 struct s_rev96062 z = x;
854 test_a_rev96062_aux(&z);
855 return a + b; // no-warning
857 int test_c_rev96062(void) {
859 struct s_rev96062 x = { &a, &b };
860 struct s_rev96062_nested w = { x };
861 struct s_rev96062_nested z = w;
862 test_a_rev96062_aux2(&z);
863 return a + b; // no-warning
866 //===----------------------------------------------------------------------===//
867 // The access to y[0] at the bottom previously was reported as an uninitialized
869 //===----------------------------------------------------------------------===//
871 char *rdar_7242010(int count, char **y) {
872 char **x = alloca((count + 4) * sizeof(*x));
877 memcpy(x + 4, y, count * sizeof(*x));
879 return y[0]; // no-warning
882 struct rdar_7770737_s { intptr_t p; };
883 void rdar_7770737_aux(struct rdar_7770737_s *p);
884 int rdar_7770737(void)
888 // Previously 'f' was not properly invalidated, causing the use of
889 // an uninitailized value below.
890 struct rdar_7770737_s f = { .p = (intptr_t)&x };
891 rdar_7770737_aux(&f);
892 return x; // no-warning
894 int rdar_7770737_pos(void)
897 struct rdar_7770737_s f = { .p = (intptr_t)&x };
898 return x; // expected-warning{{Undefined or garbage value returned to caller}}
901 //===----------------------------------------------------------------------===//
902 // Test handling of the implicit 'isa' field. For now we don't do anything
904 //===----------------------------------------------------------------------===//
906 void pr6302(id x, Class y) {
907 // This previously crashed the analyzer (reported in PR 6302)
908 x->isa = y; // expected-warning {{assignment to Objective-C's isa is deprecated in favor of object_setClass()}}
911 //===----------------------------------------------------------------------===//
912 // Specially handle global variables that are declared constant. In the
913 // example below, this forces the loop to take exactly 2 iterations.
914 //===----------------------------------------------------------------------===//
916 const int pr6288_L_N = 2;
921 for (i = 0; i < pr6288_L_N; i++)
923 *(px[0]) = 0; // no-warning
926 void pr6288_pos(int z) {
930 for (i = 0; i < z; i++)
931 px[i] = &x[i]; // expected-warning{{Access out-of-bound array element (buffer overflow)}}
932 *(px[0]) = 0; // expected-warning{{Dereference of undefined pointer value}}
935 void pr6288_b(void) {
940 for (i = 0; i < L_N; i++)
942 *(px[0]) = 0; // no-warning
945 // A bug in RemoveDeadBindings was causing instance variable bindings to get
946 // prematurely pruned from the state.
947 @interface Rdar7817800 {
950 - (void) rdar7817800_baz;
953 char *rdar7817800_foobar(void);
954 void rdar7817800_qux(void*);
956 @implementation Rdar7817800
957 - (void) rdar7817800_baz {
960 x = rdar7817800_foobar();
961 // Previously this triggered a bogus null dereference warning.
962 x[1] = 'a'; // no-warning
966 // PR 6036 - This test case triggered a crash inside StoreManager::CastRegion because the size
967 // of 'unsigned long (*)[0]' is 0.
968 struct pr6036_a { int pr6036_b; };
970 void u132monitk (struct pr6036_c *pr6036_d) {
971 (void) ((struct pr6036_a *) (unsigned long (*)[0]) ((char *) pr6036_d - 1))->pr6036_b; // expected-warning{{Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption}}
974 // ?-expressions used as a base of a member expression should be treated as an lvalue
975 typedef struct rdar7813989_NestedVal { int w; } rdar7813989_NestedVal;
976 typedef struct rdar7813989_Val { rdar7813989_NestedVal nv; } rdar7813989_Val;
978 int rdar7813989(int x, rdar7813989_Val *a, rdar7813989_Val *b) {
979 // This previously crashed with an assertion failure.
980 int z = (x ? a->nv : b->nv).w;
984 // PR 6844 - Don't crash on vaarg expression.
985 typedef __builtin_va_list va_list;
986 void map(int srcID, ...) {
989 for (i = 0; i < srcID; i++) {
990 int v = __builtin_va_arg(ap, int);
994 // PR 6854 - crash when casting symbolic memory address to a float
995 // Handle casting from a symbolic region to a 'float'. This isn't
996 // really all that intelligent, but previously this caused a crash
997 // in SimpleSValuator.
998 void pr6854(void * arg) {
1001 float f = *(float*) a;
1004 // False positive due to symbolic store not find value because of 'const'
1006 double rdar_8032791_2(void);
1007 double rdar_8032791_1(void) {
1008 struct R8032791 { double x[2]; double y; }
1010 {{1.0, 3.0}, 3.0}, // 1 2 3
1011 {{1.0, 1.0}, 0.0}, // 1 1 2 2 3 3
1012 {{1.0, 3.0}, 1.0} // 1 2 3
1016 for (unsigned i = 0 ; i < 3; i++) {
1017 const struct R8032791 *p = &data[i];
1018 x += p->y + rdar_8032791_2(); // no-warning
1023 // PR 7450 - Handle pointer arithmetic with __builtin_alloca
1024 void pr_7450_aux(void *x);
1025 void pr_7450(void) {
1026 void *p = __builtin_alloca(10);
1027 // Don't crash when analyzing the following statement.
1031 // Symbolicate struct values returned by value.
1032 struct s_rdar_8243408 { int x; };
1033 extern struct s_rdar_8243408 rdar_8243408_aux(void);
1034 void rdar_8243408(void) {
1035 struct s_rdar_8243408 a = { 1 }, *b = 0;
1037 a = rdar_8243408_aux();
1039 // Previously there was a false error here with 'b' being null.
1040 (void) (a.x && b->x); // no-warning
1042 // Introduce a null deref to ensure we are checking this path.
1044 *p = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}}
1052 // Do not warn that the value of 'foo' is uninitialized.
1053 return foo; // no-warning
1056 // PR 8052 - Don't crash when reasoning about loads from a function address.\n
1057 typedef unsigned int __uint32_t;
1058 typedef unsigned long vm_offset_t;
1059 typedef __uint32_t pd_entry_t;
1060 typedef unsigned char u_char;
1061 typedef unsigned int u_int;
1062 typedef unsigned long u_long;
1063 extern int bootMP_size;
1066 pr8052(u_int boot_addr)
1069 int size = *(int *) ((u_long) & bootMP_size);
1070 u_char *src = (u_char *) ((u_long) bootMP);
1071 u_char *dst = (u_char *) boot_addr + ((vm_offset_t) ((((((((1 <<
1072 12) / (sizeof(pd_entry_t))) - 1) - 1) - (260 - 2))) << 22) | ((0) << 12)));
1074 // expected-warning@-3 {{cast to 'u_char *' (aka 'unsigned char *') from smaller integer type 'u_int' (aka 'unsigned int')}}
1082 // PR 8015 - don't return undefined values for arrays when using a valid
1085 void pr8015_B(const char *);
1087 void pr8015_C(void) {
1088 int number = pr8015_A();
1089 const char *numbers[] = { "zero" };
1091 pr8015_B(numbers[number]); // no-warning
1095 // Tests that we correctly handle that 'number' is perfectly constrained
1096 // after 'if (number == 0)', allowing us to resolve that
1097 // numbers[number] == numbers[0].
1098 void pr8015_D_FIXME(void) {
1099 int number = pr8015_A();
1100 const char *numbers[] = { "zero" };
1102 if (numbers[number] == numbers[0])
1106 *p = 0xDEADBEEF; // no-warnng
1110 void pr8015_E(void) {
1111 // Similar to pr8015_C, but number is allowed to be a valid range.
1112 unsigned number = pr8015_A();
1113 const char *numbers[] = { "zero", "one", "two" };
1115 pr8015_B(numbers[number]); // no-warning
1119 void pr8015_F_FIXME(void) {
1120 // Similar to pr8015_E, but like pr8015_D we check if the pointer
1121 // is the same as one of the string literals. The null dereference
1122 // here is not feasible in practice, so this is a false positive.
1123 int number = pr8015_A();
1124 const char *numbers[] = { "zero", "one", "two" };
1126 const char *p = numbers[number];
1127 if (p == numbers[0] || p == numbers[1] || p == numbers[2])
1130 *q = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}}
1134 // PR 8141. Previously the statement expression in the for loop caused
1135 // the CFG builder to crash.
1138 struct list_pr8141 *tail;
1141 struct list_pr8141 *
1143 struct list_pr8141 *items;
1144 for (;; items = ({ do { } while (0); items->tail; })) // expected-warning{{dereference of an undefined pointer value}}
1149 // Don't crash when building the CFG.
1150 void do_not_crash(int x) {
1151 while (x - ({do {} while (0); x; })) {
1155 // Handle looking at the size of a VLA in ArrayBoundChecker. Nothing
1156 // intelligent (yet); just don't crash.
1157 typedef struct RDar8424269_A {
1160 static void RDar8424269_B(RDar8424269_A *p, unsigned char *RDar8424269_D,
1161 const unsigned char *RDar8424269_E, int RDar8424269_F,
1162 int b_w, int b_h, int dx, int dy) {
1164 unsigned char tmp2t[3][RDar8424269_F * (32 + 8)];
1165 unsigned char *tmp2 = tmp2t[0];
1166 if (p && !p->RDar8424269_C)
1169 if (b & 2) { // expected-warning{{The left operand of '&' is a garbage value}}
1170 for (y = 0; y < b_h; y++) {
1171 for (x = 0; x < b_w + 1; x++) {
1180 // Handle transparent unions with the NonNullParamChecker.
1182 struct rdar_8642434_typeA *_dq;
1184 rdar_8642434_typeB __attribute__((transparent_union));
1186 __attribute__((visibility("default"))) __attribute__((__nonnull__)) __attribute__((__nothrow__))
1187 void rdar_8642434_funcA(rdar_8642434_typeB object);
1189 void rdar_8642434_funcB(struct rdar_8642434_typeA *x, struct rdar_8642434_typeA *y) {
1190 rdar_8642434_funcA(x);
1192 rdar_8642434_funcA(y); // expected-warning{{Null pointer passed to 1st parameter expecting 'nonnull'}}
1195 // Handle loads and stores from a symbolic index into array without warning
1196 // about an uninitialized value being returned. While RegionStore can't fully
1197 // reason about this example, it shouldn't warn here either.
1198 typedef struct s_test_rdar8848957 {
1200 } s_test_rdar8848957;
1202 s_test_rdar8848957 foo_rdar8848957(void);
1203 int rdar8848957(int index) {
1204 s_test_rdar8848957 vals[10];
1205 vals[index] = foo_rdar8848957();
1206 return vals[index].x; // no-warning
1209 // PR 9049 - crash on symbolicating unions. This test exists solely to
1210 // test that the analyzer doesn't crash.
1211 typedef struct pr9048_cdev *pr9048_cdev_t;
1212 typedef union pr9048_abstracted_disklabel { void *opaque; } pr9048_disklabel_t;
1213 struct pr9048_diskslice { pr9048_disklabel_t ds_label; };
1214 struct pr9048_diskslices {
1216 struct pr9048_diskslice dss_slices[16];
1218 void pr9048(pr9048_cdev_t dev, struct pr9048_diskslices * ssp, unsigned int slice)
1220 pr9048_disklabel_t lp;
1221 struct pr9048_diskslice *sp;
1222 sp = &ssp->dss_slices[slice];
1223 if (ssp->dss_secmult == 1) {
1224 } else if ((lp = sp->ds_label).opaque != ((void *) 0)) {
1228 // Test Store reference counting in the presence of Lazy compound values.
1229 // This previously caused an infinite recursion.
1230 typedef struct {} Rdar_9103310_A;
1231 typedef struct Rdar_9103310_B Rdar_9103310_B_t;
1232 struct Rdar_9103310_B {
1233 unsigned char Rdar_9103310_C[101];
1235 void Rdar_9103310_E(Rdar_9103310_A * x, struct Rdar_9103310_C * b) { // expected-warning {{declaration of 'struct Rdar_9103310_C' will not be visible outside of this function}}
1236 char Rdar_9103310_D[4][4] = { "a", "b", "c", "d"};
1238 Rdar_9103310_B_t *y = (Rdar_9103310_B_t *) x;
1239 for (i = 0; i < 101; i++) {
1240 Rdar_9103310_F(b, "%2d%s ", (y->Rdar_9103310_C[i]) / 4, Rdar_9103310_D[(y->Rdar_9103310_C[i]) % 4]); // expected-warning {{call to undeclared function 'Rdar_9103310_F'; ISO C99 and later do not support implicit function declarations}}
1244 // Test handling binding lazy compound values to a region and then have
1245 // specific elements have other bindings.
1247 char arr[4] = "000";
1252 *p = 0xDEADBEEF; // no-warning
1255 int PR9455_2(void) {
1256 char arr[4] = "000";
1258 if (arr[1] == '0') {
1260 *p = 0xDEADBEEF; // expected-warning {{null}}
1265 // Test initialization of substructs via lazy compound values.
1266 typedef float RDar9163742_Float;
1269 RDar9163742_Float x, y;
1270 } RDar9163742_Point;
1272 RDar9163742_Float width, height;
1275 RDar9163742_Point origin;
1276 RDar9163742_Size size;
1279 extern RDar9163742_Rect RDar9163742_RectIntegral(RDar9163742_Rect);
1281 RDar9163742_Rect RDar9163742_IntegralRect(RDar9163742_Rect frame)
1283 RDar9163742_Rect integralFrame;
1284 integralFrame.origin.x = frame.origin.x;
1285 integralFrame.origin.y = frame.origin.y;
1286 integralFrame.size = frame.size;
1287 return RDar9163742_RectIntegral(integralFrame); // no-warning; all fields initialized
1290 // Test correct handling of prefix '--' operator.
1291 void rdar9444714(void) {
1303 *ptr++ = (char)( '0' + ( x % 10 ) );
1309 *dst++ = *( --( ptr ) ); // no-warning
1314 // Test handling symbolic elements with field accesses.
1319 signed rdar_11127008_index(void);
1321 static unsigned rdar_11127008(void) {
1322 RDar11127008 values[] = {{.value = 0}, {.value = 1}};
1323 signed index = rdar_11127008_index();
1324 if (index < 0) return 0;
1325 if (index >= 2) return 0;
1326 return values[index].value;
1329 // Test handling invalidating arrays passed to a block via captured
1330 // pointer value (not a __block variable).
1331 typedef void (^radar11125868_cb)(int *, unsigned);
1333 void rdar11125868_aux(radar11125868_cb cb);
1335 int rdar11125868(void) {
1336 int integersStackArray[1];
1337 int *integers = integersStackArray;
1338 rdar11125868_aux(^(int *integerValue, unsigned index) {
1339 integers[index] = integerValue[index];
1341 return integers[0] == 0; // no-warning
1344 int rdar11125868_positive(void) {
1345 int integersStackArray[1];
1346 int *integers = integersStackArray;
1347 return integers[0] == 0; // expected-warning {{The left operand of '==' is a}}