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-experimental-internal-checks -analyzer-check-objc-mem -analyzer-checker=core.experimental.IdempotentOps -analyzer-checker=core.experimental.CastToStruct -analyzer-checker=cocoa.AtSync -analyzer-store=basic -fobjc-gc -analyzer-constraints=basic -verify -fblocks -Wno-unreachable-code %s
3 // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-checker=core.experimental.IdempotentOps -analyzer-checker=core.experimental.CastToStruct -analyzer-checker=cocoa.AtSync -analyzer-store=basic -analyzer-constraints=range -verify -fblocks -Wno-unreachable-code %s
4 // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-checker=core.experimental.IdempotentOps -analyzer-checker=core.experimental.CastToStruct -analyzer-checker=cocoa.AtSync -analyzer-store=region -analyzer-constraints=basic -verify -fblocks -Wno-unreachable-code %s
5 // RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-checker=core.experimental.IdempotentOps -analyzer-checker=core.experimental.CastToStruct -analyzer-checker=cocoa.AtSync -analyzer-store=region -analyzer-constraints=range -verify -fblocks -Wno-unreachable-code %s
6 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-checker=core.experimental.IdempotentOps -analyzer-checker=core.experimental.CastToStruct -analyzer-checker=cocoa.AtSync -analyzer-store=basic -fobjc-gc -analyzer-constraints=basic -verify -fblocks -Wno-unreachable-code %s
7 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-checker=core.experimental.IdempotentOps -analyzer-checker=core.experimental.CastToStruct -analyzer-checker=cocoa.AtSync -analyzer-store=basic -analyzer-constraints=range -verify -fblocks -Wno-unreachable-code %s
8 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-checker=core.experimental.IdempotentOps -analyzer-checker=core.experimental.CastToStruct -analyzer-checker=cocoa.AtSync -analyzer-store=region -analyzer-constraints=basic -verify -fblocks -Wno-unreachable-code %s
9 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-checker=core.experimental.IdempotentOps -analyzer-checker=core.experimental.CastToStruct -analyzer-checker=cocoa.AtSync -analyzer-store=region -analyzer-constraints=range -verify -fblocks -Wno-unreachable-code %s
11 #ifndef __clang_analyzer__
12 #error __clang__analyzer__ not defined
15 typedef struct objc_ivar *Ivar;
16 typedef struct objc_selector *SEL;
17 typedef signed char BOOL;
18 typedef int NSInteger;
19 typedef unsigned int NSUInteger;
20 typedef struct _NSZone NSZone;
21 @class NSInvocation, NSArray, NSMethodSignature, NSCoder, NSString, NSEnumerator;
23 - (BOOL)isEqual:(id)object;
27 - (id)copyWithZone:(NSZone *)zone;
29 @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end
31 - (void)encodeWithCoder:(NSCoder *)aCoder;
33 @interface NSObject <NSObject> {}
35 + (id)allocWithZone:(NSZone *)zone;
37 extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
38 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
40 + (id)stringWithUTF8String:(const char *)nullTerminatedCString;
41 @end extern NSString * const NSBundleDidLoadNotification;
42 @interface NSValue : NSObject <NSCopying, NSCoding>
43 - (void)getValue:(void *)value;
45 @interface NSNumber : NSValue
47 - (id)initWithBool:(BOOL)value;
49 @interface NSAssertionHandler : NSObject {}
50 + (NSAssertionHandler *)currentHandler;
51 - (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format,...;
53 extern NSString * const NSConnectionReplyMode;
54 typedef float CGFloat;
55 typedef struct _NSPoint {
59 typedef struct _NSSize {
63 typedef struct _NSRect {
68 // Reduced test case from crash in <rdar://problem/6253157>
71 - (void)foo:(void (^)(NSObject *x))block {
72 if (!((block != ((void *)0)))) {}
76 // Reduced test case from crash in PR 2796;
77 // http://llvm.org/bugs/show_bug.cgi?id=2796
79 unsigned foo(unsigned x) { return __alignof__((x)) + sizeof(x); }
81 // Improvement to path-sensitivity involving compound assignments.
82 // Addresses false positive in <rdar://problem/6268365>
85 unsigned r6268365Aux();
89 x &= r6268365Aux(); // expected-warning{{The left operand to '&=' is always 0}}
93 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}}
96 void divzeroassume(unsigned x, unsigned j) {
98 if (j == 0) x /= 0; // no static-analyzer warning expected-warning {{division by zero is undefined}}
99 if (j == 0) x /= j; // no static-analyzer warning
100 if (j == 0) x = x / 0; // no static-analyzer warning expected-warning {{division by zero is undefined}}
103 void divzeroassumeB(unsigned x, unsigned j) {
105 if (j == 0) x /= 0; // no static-analyzer warning expected-warning {{division by zero is undefined}}
106 if (j == 0) x /= j; // no static-analyzer warning
107 if (j == 0) x = x / 0; // no static-analyzer warning expected-warning {{division by zero is undefined}}
110 // InitListExpr processing
112 typedef float __m128 __attribute__((__vector_size__(16), __may_alias__));
114 // This compound literal has a Vector type. We currently just
115 // return UnknownVal.
116 return __extension__(__m128) { 0.0f, 0.0f, 0.0f, 0.0f };
119 typedef long long __v2di __attribute__ ((__vector_size__ (16)));
120 typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__));
121 __m128i vec128i(long long __q1, long long __q0) {
122 // This compound literal returns true for both isVectorType() and
124 return __extension__ (__m128i)(__v2di){ __q0, __q1 };
128 void check_zero_sized_VLA(int x) {
132 int vla[x]; // expected-warning{{Declared variable-length array (VLA) has zero size}}
135 void check_uninit_sized_VLA() {
137 int vla[x]; // expected-warning{{Declared variable-length array (VLA) uses a garbage value as its size}}
141 // - Tests a regression reported in PR 3211: http://llvm.org/bugs/show_bug.cgi?id=3211
142 void handle_sizeof_void(unsigned flag) {
146 if (sizeof(void) == 1)
149 *p = 1; // no-warning
158 *p = 1; // no-warning
162 *p = 1; // no-warning
165 // check deference of undefined values
166 void check_deref_undef(void) {
168 *p = 0xDEADBEEF; // expected-warning{{Dereference of undefined pointer value}}
172 void pr3422_helper(char *p);
176 pr3422_helper(&q[1]);
179 // PR 3543 (handle empty statement expressions)
184 // <rdar://problem/6611677>
185 // This test case test the use of a vector type within an array subscript
187 typedef long long __a64vector __attribute__((__vector_size__(8)));
188 typedef long long __a128vector __attribute__((__vector_size__(16)));
189 static inline __a64vector __attribute__((__always_inline__, __nodebug__))
190 my_test_mm_movepi64_pi64(__a128vector a) {
191 return (__a64vector)a[0];
194 // Test basic tracking of ivars associated with 'self'.
195 @interface SelfIvarTest : NSObject {
198 - (void)test_self_tracking;
201 @implementation SelfIvarTest
202 - (void)test_self_tracking {
210 c = *p; // no-warning
222 return p[0]; // no-warning
228 // - We just want to test that this doesn't crash the analyzer.
229 typedef struct st ST;
230 struct st { char *name; };
235 static ST *last_Cur_Pu;
236 if (last_Cur_Pu == Cur_Pu) {
241 // PR 3780 - This tests that StmtIterator isn't broken for VLAs in DeclGroups.
242 void pr3780(int sz) { typedef double MAT[sz][sz]; }
244 // <rdar://problem/6695527> - Test that we don't symbolicate doubles before
245 // we are ready to do something with them.
246 int rdar6695527(double x) {
247 if (!x) { return 0; }
251 // <rdar://problem/6708148> - Test that we properly invalidate structs
252 // passed-by-reference to a function.
253 void pr6708148_invalidate(NSRect *x);
254 void pr6708148_use(NSRect x);
255 void pr6708148_test(void) {
257 pr6708148_invalidate(&x);
258 pr6708148_use(x); // no-warning
261 // Handle both kinds of noreturn attributes for pruning paths.
262 void rdar_6777003_noret() __attribute__((noreturn));
263 void rdar_6777003_analyzer_noret() __attribute__((analyzer_noreturn));
265 void rdar_6777003(int x) {
269 rdar_6777003_noret();
270 *p = 1; // no-warning;
274 rdar_6777003_analyzer_noret();
275 *p = 1; // no-warning;
278 *p = 1; // expected-warning{{Dereference of null pointer}}
281 // For pointer arithmetic, --/++ should be treated as preserving non-nullness,
282 // regardless of how well the underlying StoreManager reasons about pointer
284 // <rdar://problem/6777209>
285 void rdar_6777209(char *p) {
291 // This branch should always be infeasible.
293 *p = 'c'; // no-warning
296 // PR 4033. A symbolic 'void *' pointer can be used as the address for a
298 typedef void *Opcode;
299 Opcode pr_4033_getOpcode();
301 void *lbl = &&next_opcode;
304 Opcode op = pr_4033_getOpcode();
309 // Test invalidating pointers-to-pointers with slightly different types. This
310 // example came from a recent false positive due to a regression where the
311 // branch condition was falsely reported as being uninitialized.
312 void invalidate_by_ref(char **x);
313 int test_invalidate_by_ref() {
315 invalidate_by_ref((char**) &y);
321 // Test for <rdar://problem/7027684>. This just tests that the CFG is
322 // constructed correctly. Previously, the successor block of the entrance
323 // was the block containing the merge for '?', which would trigger an
324 // assertion failure.
325 int rdar_7027684_aux();
326 int rdar_7027684_aux_2() __attribute__((noreturn));
327 void rdar_7027684(int x, int y) {
328 {}; // this empty compound statement is critical.
329 (rdar_7027684_aux() ? rdar_7027684_aux_2() : (void) 0);
332 // Test that we handle casts of string literals to arbitrary types.
333 unsigned const char *string_literal_test1() {
334 return (const unsigned char*) "hello";
337 const float *string_literal_test2() {
338 return (const float*) "hello";
341 // Test that we handle casts *from* incomplete struct types.
342 extern const struct _FooAssertStruct _cmd;
343 void test_cast_from_incomplete_struct_aux(volatile const void *x);
344 void test_cast_from_incomplete_struct() {
345 test_cast_from_incomplete_struct_aux(&_cmd);
348 // Test for <rdar://problem/7034511>
349 // "ValueManager::makeIntVal(uint64_t X, QualType T) should return a 'Loc'
350 // when 'T' is a pointer"
352 // Previously this case would crash.
353 void test_rdar_7034511(NSArray *y) {
356 if (x == ((void*) 0)) {}
359 // Handle casts of function pointers (CodeTextRegions) to arbitrary pointer
360 // types. This was previously causing a crash in CastRegion.
361 void handle_funcptr_voidptr_casts() {
365 typedef long INT_PTR, *PINT_PTR;
366 typedef INT_PTR (*FARPROC)();
367 FARPROC handle_funcptr_voidptr_casts_aux();
368 PVOID handle_funcptr_voidptr_casts_aux_2(PVOID volatile *x);
369 PVOID handle_funcptr_voidptr_casts_aux_3(PCHAR volatile *x);
371 ptr = (void**) handle_funcptr_voidptr_casts_aux();
372 handle_funcptr_voidptr_casts_aux_2(ptr);
373 handle_funcptr_voidptr_casts_aux_3(ptr);
376 // RegionStore::Retrieve previously crashed on this example. This example
377 // was previously in the test file 'xfail_regionstore_wine_crash.c'.
380 char *y = (char *) &x;
385 // RegionStoreManager previously crashed on this example. The problem is that
386 // the value bound to the field of b->grue after the call to testB_aux is
387 // a symbolic region. The second '*__gruep__' involves performing a load
388 // from a 'int*' that really is a 'void**'. The loaded location must be
389 // implicitly converted to an integer that wraps a location. Previosly we would
390 // get a crash here due to an assertion failure.
391 typedef struct _BStruct { void *grue; } BStruct;
392 void testB_aux(void *ptr);
393 void testB(BStruct *b) {
395 int *__gruep__ = ((int *)&((b)->grue));
396 int __gruev__ = *__gruep__;
397 testB_aux(__gruep__);
400 int *__gruep__ = ((int *)&((b)->grue));
401 int __gruev__ = *__gruep__;
402 if (~0 != __gruev__) {}
406 void test_trivial_symbolic_comparison(int *x) {
407 int test_trivial_symbolic_comparison_aux();
408 int a = test_trivial_symbolic_comparison_aux();
410 if (a != b) { // expected-warning{{Both operands to '!=' always have the same value}}
412 *p = 0xDEADBEEF; // no-warning
417 if (a != b) { // expected-warning{{Both operands to '!=' always have the same value}}
419 *p = 0xDEADBEEF; // no-warning
424 // <rdar://problem/7062158> false positive null dereference due to
425 // BasicStoreManager not tracking *static* globals
427 // This just tests the proper tracking of symbolic values for globals (both
428 // static and non-static).
430 static int* x_rdar_7062158;
431 void rdar_7062158() {
432 int *current = x_rdar_7062158;
433 if (current == x_rdar_7062158)
437 *p = 0xDEADBEEF; // no-warning
440 int* x_rdar_7062158_2;
441 void rdar_7062158_2() {
442 int *current = x_rdar_7062158_2;
443 if (current == x_rdar_7062158_2)
447 *p = 0xDEADBEEF; // no-warning
450 // This test reproduces a case for a crash when analyzing ClamAV using
451 // RegionStoreManager (the crash doesn't exhibit in BasicStoreManager because
452 // it isn't doing anything smart about arrays). The problem is that on the
453 // second line, 'p = &p[i]', p is assigned an ElementRegion whose index
454 // is a 16-bit integer. On the third line, a new ElementRegion is created
455 // based on the previous region, but there the region uses a 32-bit integer,
456 // resulting in a clash of values (an assertion failure at best). We resolve
457 // this problem by implicitly converting index values to 'int' when the
458 // ElementRegion is created.
459 unsigned char test_array_index_bitwidth(const unsigned char *p) {
460 unsigned short i = 0;
461 for (i = 0; i < 2; i++) p = &p[i];
465 // This case tests that CastRegion handles casts involving BlockPointerTypes.
466 // It should not crash.
467 void test_block_cast() {
468 id test_block_cast_aux();
469 (void (^)(void *))test_block_cast_aux(); // expected-warning{{expression result unused}}
472 int OSAtomicCompareAndSwap32Barrier();
474 // Test comparison of 'id' instance variable to a null void* constant after
475 // performing an OSAtomicCompareAndSwap32Barrier.
476 // This previously was a crash in RegionStoreManager.
477 @interface TestIdNull {
482 @implementation TestIdNull
484 OSAtomicCompareAndSwap32Barrier(0, (signed)2, (signed*)&x);
485 if (x == (void*) 0) { return 0; }
490 // PR 4594 - This was a crash when handling casts in SimpleSValuator.
497 // Test invalidation logic where an integer is casted to an array with a
498 // different sign and then invalidated.
499 void test_invalidate_cast_int() {
500 void test_invalidate_cast_int_aux(unsigned *i);
502 test_invalidate_cast_int_aux((unsigned*) &i);
507 int ivar_getOffset();
509 // Reduced from a crash involving the cast of an Objective-C symbolic region to
511 static NSNumber *test_ivar_offset(id self, SEL _cmd, Ivar inIvar) {
512 return [[[NSNumber allocWithZone:((void*)0)] initWithBool:*(_Bool *)((char *)self + ivar_getOffset(inIvar))] autorelease];
515 // Reduced from a crash in StoreManager::CastRegion involving a divide-by-zero.
516 // This resulted from not properly handling region casts to 'const void*'.
517 void test_cast_const_voidptr() {
523 // Reduced from a crash when analyzing Wine. This test handles loads from
524 // function addresses.
525 typedef long (*FARPROC)();
526 FARPROC test_load_func(FARPROC origfun) {
527 if (!*(unsigned char*) origfun)
532 // Test passing-by-value an initialized struct variable.
533 struct test_pass_val {
537 void test_pass_val_aux(struct test_pass_val s);
538 void test_pass_val() {
539 struct test_pass_val s;
542 test_pass_val_aux(s);
545 // This is a reduced test case of a false positive that previously appeared
546 // in RegionStoreManager. Previously the array access resulted in dereferencing
547 // an undefined value.
548 int test_array_compound(int *q, int *r, int *z) {
549 int *array[] = { q, r, z };
551 for (unsigned i = 0; i < 3 ; ++i)
552 if (*array[i]) ++j; // no-warning
556 // This test case previously crashed with -analyzer-store=basic because the
557 // symbolic value stored in 'x' wouldn't be implicitly casted to a signed value
558 // during the comparison.
559 int rdar_7124210(unsigned int x) {
560 enum { SOME_CONSTANT = 123 };
561 int compare = ((signed) SOME_CONSTANT) == *((signed *) &x);
562 return compare ? 0 : 1; // Forces the evaluation of the symbolic constraint.
565 void pr4781(unsigned long *raw1) {
566 unsigned long *cook, *raw0;
567 unsigned long dough[32];
570 for( i = 0; i < 16; i++, raw1++ ) {
572 *cook = (*raw0 & 0x00fc0000L) << 6;
573 *cook |= (*raw0 & 0x00000fc0L) << 10;
577 // <rdar://problem/7185647> - 'self' should be treated as being non-null
578 // upon entry to an objective-c method.
579 @interface RDar7185647
582 @implementation RDar7185647
586 *((volatile int *) 0x0) = 0xDEADBEEF; // no-warning
591 // Test reasoning of __builtin_offsetof;
592 struct test_offsetof_A {
596 struct test_offsetof_B {
600 void test_offsetof_1() {
601 if (__builtin_offsetof(struct test_offsetof_A, x) ==
602 __builtin_offsetof(struct test_offsetof_B, w))
605 *p = 0xDEADBEEF; // no-warning
607 void test_offsetof_2() {
608 if (__builtin_offsetof(struct test_offsetof_A, y) ==
609 __builtin_offsetof(struct test_offsetof_B, z))
612 *p = 0xDEADBEEF; // no-warning
614 void test_offsetof_3() {
615 if (__builtin_offsetof(struct test_offsetof_A, y) -
616 __builtin_offsetof(struct test_offsetof_A, x)
618 __builtin_offsetof(struct test_offsetof_B, z) -
619 __builtin_offsetof(struct test_offsetof_B, w))
622 *p = 0xDEADBEEF; // no-warning
624 void test_offsetof_4() {
625 if (__builtin_offsetof(struct test_offsetof_A, y) ==
626 __builtin_offsetof(struct test_offsetof_B, w))
629 *p = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}}
632 // <rdar://problem/6829164> "nil receiver" false positive: make tracking
633 // of the MemRegion for 'self' path-sensitive
634 @interface RDar6829164 : NSObject {
641 double rdar_6829164_2();
643 @implementation RDar6829164
645 if((self = [super init]) != 0) {
646 id z = rdar_6829164_1();
649 x = rdar_6829164_2();
655 // <rdar://problem/7242015> - Invalidate values passed-by-reference
656 // to functions when the pointer to the value is passed as an integer.
657 void test_7242015_aux(unsigned long);
660 test_7242015_aux((unsigned long) &x); // no-warning
661 return x; // Previously we return and uninitialized value when
662 // using RegionStore.
665 // <rdar://problem/7242006> [RegionStore] compound literal assignment with
666 // floats not honored
667 CGFloat rdar7242006(CGFloat x) {
668 NSSize y = (NSSize){x, 10};
669 return y.width; // no-warning
672 // PR 4988 - This test exhibits a case where a function can be referenced
673 // when not explicitly used in an "lvalue" context (as far as the analyzer is
674 // concerned). This previously triggered a crash due to an invalid assertion.
676 pr_4988; // expected-warning{{expression result unused}}
679 // <rdar://problem/7152418> - A 'signed char' is used as a flag, which is
680 // implicitly converted to an int.
681 void *rdar7152418_bar();
682 @interface RDar7152418 {
687 @implementation RDar7152418
692 result = rdar7152418_bar();
696 result = rdar7152418_bar();
698 return *p; // no-warning
703 //===----------------------------------------------------------------------===//
704 // Test constant-folding of symbolic values, automatically handling type
705 // conversions of the symbol as necessary.
706 //===----------------------------------------------------------------------===//
708 // Previously this would crash once we started eagerly evaluating symbols whose
709 // values were constrained to a single value.
710 void test_symbol_fold_1(signed char x) {
712 if (x == ((signed char) 0)) {}
716 // This previously caused a crash because it triggered an assertion in APSInt.
717 void test_symbol_fold_2(unsigned int * p, unsigned int n,
718 const unsigned int * grumpkin, unsigned int dn) {
720 unsigned int tempsub[8];
721 unsigned int *solgrumpkin = tempsub + n;
722 for (i = 0; i < n; i++)
723 solgrumpkin[i] = (i < dn) ? ~grumpkin[i] : 0xFFFFFFFF;
724 for (i <<= 5; i < (n << 5); i++) {}
727 // This previously caused a crash because it triggered an assertion in APSInt.
728 // 'x' would evaluate to a 8-bit constant (because of the return value of
729 // test_symbol_fold_3_aux()) which would not get properly promoted to an
731 char test_symbol_fold_3_aux(void);
732 unsigned test_symbol_fold_3(void) {
733 unsigned x = test_symbol_fold_3_aux();
735 return (x << 8) | 0x5;
739 //===----------------------------------------------------------------------===//
740 // Tests for the warning of casting a non-struct type to a struct type
741 //===----------------------------------------------------------------------===//
743 typedef struct {unsigned int v;} NSSwappedFloat;
745 NSSwappedFloat test_cast_nonstruct_to_struct(float x) {
750 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 NSSwappedFloat test_cast_nonstruct_to_union(float x) {
758 return ((union bran *)&x)->sf; // no-warning
761 void test_undefined_array_subscript() {
763 int *p = &a[i]; // expected-warning{{Array subscript is undefined}}
767 //===----------------------------------------------------------------------===//
768 // Test using an uninitialized value as a branch condition.
769 //===----------------------------------------------------------------------===//
771 int test_uninit_branch(void) {
773 if (x) // expected-warning{{Branch condition evaluates to a garbage value}}
778 int test_uninit_branch_b(void) {
780 return x ? 1 : 0; // expected-warning{{Branch condition evaluates to a garbage value}}
783 int test_uninit_branch_c(void) {
785 if ((short)x) // expected-warning{{Branch condition evaluates to a garbage value}}
790 //===----------------------------------------------------------------------===//
791 // Test passing an undefined value in a message or function call.
792 //===----------------------------------------------------------------------===//
794 void test_bad_call_aux(int x);
795 void test_bad_call(void) {
797 test_bad_call_aux(y); // expected-warning{{Function call argument is an uninitialized value}}
800 @interface TestBadArg {}
801 - (void) testBadArg:(int) x;
804 void test_bad_msg(TestBadArg *p) {
806 [p testBadArg:y]; // expected-warning{{Argument in message expression is an uninitialized value}}
809 //===----------------------------------------------------------------------===//
810 // PR 6033 - Test emitting the correct output in a warning where we use '%'
811 // with operands that are undefined.
812 //===----------------------------------------------------------------------===//
816 return x % y; // expected-warning{{The right operand of '%' is a garbage value}}
826 struct trie* next[10];
830 typedef struct trie trie_t;
831 typedef struct kwset kwset_t;
833 void f(kwset_t *kws, char const *p, char const *q) {
834 struct trie const *trie;
835 struct trie * const *next = kws->next;
836 register unsigned char c;
837 register char const *end = p;
838 register char const *lim = q;
840 register unsigned char const *y = kws->y;
842 d = y[c = (end+=d)[-1]]; // no-warning
846 //===----------------------------------------------------------------------===//
847 // <rdar://problem/7593875> When handling sizeof(VLA) it leads to a hole in
848 // the ExplodedGraph (causing a false positive)
849 //===----------------------------------------------------------------------===//
851 int rdar_7593875_aux(int x);
852 int rdar_7593875(int n) {
853 int z[n > 10 ? 10 : n]; // VLA.
855 v = rdar_7593875_aux(sizeof(z));
856 // Previously we got a false positive about 'v' being uninitialized.
857 return v; // no-warning
860 //===----------------------------------------------------------------------===//
861 // Handle casts from symbolic regions (packaged as integers) to doubles.
862 // Previously this caused an assertion failure.
863 //===----------------------------------------------------------------------===//
865 void *foo_rev95119();
866 void baz_rev95119(double x);
867 void bar_rev95119() {
868 // foo_rev95119() returns a symbolic pointer. It is then
869 // cast to an int which is then cast to a double.
870 int value = (int) foo_rev95119();
871 baz_rev95119((double)value);
874 //===----------------------------------------------------------------------===//
875 // Handle loading a symbolic pointer from a symbolic region that was
876 // invalidated by a call to an unknown function.
877 //===----------------------------------------------------------------------===//
879 void bar_rev95192(int **x);
880 void foo_rev95192(int **x) {
883 // Not a null dereference.
884 **x = 1; // no-warning
887 //===----------------------------------------------------------------------===//
888 // Handle casts of a function to a function pointer with a different return
889 // value. We don't yet emit an error for such cases, but we now we at least
890 // don't crash when the return value gets interpreted in a way that
891 // violates our invariants.
892 //===----------------------------------------------------------------------===//
894 void *foo_rev95267();
896 char (*Callback_rev95267)(void) = (char (*)(void)) foo_rev95267;
897 if ((*Callback_rev95267)() == (char) 0)
902 // Same as previous case, but handle casts to 'void'.
904 void (*Callback_rev95274)(void) = (void (*)(void)) foo_rev95267;
905 (*Callback_rev95274)();
909 void rdar7582031_test_static_init_zero() {
916 void rdar7582031_test_static_init_zero_b() {
924 //===----------------------------------------------------------------------===//
925 // Test handling of parameters that are structs that contain floats and //
927 //===----------------------------------------------------------------------===//
929 struct s_rev95547_nested { float x, y; };
931 struct s_rev95547_nested z1;
932 struct s_rev95547_nested z2;
934 float foo_rev95547(struct s_rev95547 w) {
935 return w.z1.x + 20.0; // no-warning
937 void foo_rev95547_b(struct s_rev95547 w) {
938 struct s_rev95547 w2 = w;
939 w2.z1.x += 20.0; // no-warning
942 //===----------------------------------------------------------------------===//
943 // Test handling statement expressions that don't populate a CFG block that
944 // is used to represent the computation of the RHS of a logical operator.
945 // This previously triggered a crash.
946 //===----------------------------------------------------------------------===//
957 if (1 && *({ // expected-warning{{Dereference of null pointer}}
966 //===----------------------------------------------------------------------===//
967 // <rdar://problem/7979430> - The CFG for code containing an empty
968 // @synchronized block was previously broken (and would crash the analyzer).
969 //===----------------------------------------------------------------------===//
971 void r7979430(id x) {
975 //===----------------------------------------------------------------------===
976 // PR 7361 - Test that functions wrapped in macro instantiations are analyzed.
977 //===----------------------------------------------------------------------===
978 #define MAKE_TEST_FN() \
979 void test_pr7361 (char a) {\
980 char* b = 0x0; *b = a;\
983 MAKE_TEST_FN() // expected-warning{{null pointer}}
985 //===----------------------------------------------------------------------===
986 // PR 7491 - Test that symbolic expressions can be used as conditions.
987 //===----------------------------------------------------------------------===
998 // Should be unreachable
999 (void)*(char*)0; // no-warning
1003 //===----------------------------------------------------------------------===
1004 // PR 7475 - Test that assumptions about global variables are reset after
1005 // calling a global function.
1006 //===----------------------------------------------------------------------===
1008 int *pr7475_someGlobal;
1009 void pr7475_setUpGlobal();
1012 if (pr7475_someGlobal == 0)
1013 pr7475_setUpGlobal();
1014 *pr7475_someGlobal = 0; // no-warning
1017 void pr7475_warn() {
1018 static int *someStatic = 0;
1019 if (someStatic == 0)
1020 pr7475_setUpGlobal();
1021 *someStatic = 0; // expected-warning{{null pointer}}
1024 // <rdar://problem/8202272> - __imag passed non-complex should not crash
1025 float f0(_Complex float x) {
1026 float l0 = __real x;
1027 return __real l0 + __imag l0;
1031 //===----------------------------------------------------------------------===
1032 // Test that we can reduce symbols to constants whether they are on the left
1033 // or right side of an expression.
1034 //===----------------------------------------------------------------------===
1036 void reduce_to_constant(int x, int y) {
1043 if (y == -20 && a != 0)
1044 (void)*(char*)0; // no-warning
1045 if (y == -20 && b != 0)
1046 (void)*(char*)0; // no-warning
1049 // <rdar://problem/8360854> - Test that code after a switch statement with no
1050 // 'case:' labels is correctly evaluated.
1051 void r8360854(int n) {
1056 *p = 0xDEADBEEF; // expected-warning{{null pointer}}
1059 // PR 8050 - crash in CastSizeChecker when pointee is an incomplete type
1060 typedef long unsigned int __darwin_size_t;
1061 typedef __darwin_size_t size_t;
1062 void *malloc(size_t);
1066 void pr8050(struct PR8050 **arg)
1071 // <rdar://problem/5880430> Switch on enum should not consider default case live
1072 // if all enum values are covered
1073 enum Cases { C1, C2, C3, C4 };
1074 void test_enum_cases(enum Cases C) {
1083 *p = 0xDEADBEEF; // no-warning
1086 void test_enum_cases_positive(enum Cases C) {
1087 switch (C) { // expected-warning{{enumeration value 'C4' not handled in switch}}
1094 *p = 0xDEADBEEF; // expected-warning{{Dereference of null pointer}}
1097 // <rdar://problem/6351970> rule request: warn if synchronization mutex can be nil
1098 void rdar6351970() {
1100 @synchronized(x) {} // expected-warning{{Nil value used as mutex for @synchronized() (no synchronization will occur)}}
1103 void rdar6351970_b(id x) {
1105 @synchronized(x) {} // expected-warning{{Nil value used as mutex for @synchronized() (no synchronization will occur)}}
1108 void rdar6351970_c() {
1110 @synchronized(x) {} // expected-warning{{Uninitialized value used as mutex for @synchronized}}
1113 @interface Rdar8578650
1117 void rdar8578650(id x) {
1121 // At this point we should assume that 'x' is not nil, not
1123 @synchronized (x) { // no-warning
1127 // <rdar://problem/6352035> rule request: direct structure member access null pointer dereference
1128 @interface RDar6352035 {
1135 @implementation RDar6352035
1137 RDar6352035 *friend = 0;
1138 friend->c = 7; // expected-warning{{Instance variable access (via 'friend') results in a null pointer dereference}}
1142 c = 7; // expected-warning{{Instance variable access (via 'self') results in a null pointer dereference}}
1146 // PR 8149 - GNU statement expression in condition of ForStmt.
1147 // This previously triggered an assertion failure in CFGBuilder.
1149 for (; ({ do { } while (0); 0; });) { }
1152 // PR 8458 - Make sure @synchronized doesn't crash with properties.
1153 @interface PR8458 {}
1154 @property(readonly) id lock;
1158 void __PR8458(PR8458 *x) {
1159 @synchronized(x.lock) {} // no-warning
1162 // PR 8440 - False null dereference during store to array-in-field-in-global.
1163 // This test case previously resulted in a bogus null deref warning from
1164 // incorrect lazy symbolication logic in RegionStore.
1171 char **bar_pr8440();
1172 void baz_pr8440(int n)
1174 saved_pr8440.num = n;
1175 if (saved_pr8440.data)
1177 saved_pr8440.data = bar_pr8440();
1178 for (int i = 0 ; i < n ; i ++)
1179 saved_pr8440.data[i] = foo_pr8440(); // no-warning
1182 // Support direct accesses to non-null memory. Reported in:
1184 // <rdar://problem/6839683>
1185 int test_direct_address_load() {
1186 int *p = (int*) 0x4000;
1187 return *p; // no-warning
1190 void pr5272_test() {
1191 struct pr5272 { int var2; };
1192 (*(struct pr5272*)0xBC000000).var2 = 0; // no-warning
1193 (*(struct pr5272*)0xBC000000).var2 += 2; // no-warning
1196 // Support casting the return value of function to another different type
1197 // This previously caused a crash, although we likely need more precise
1198 // reasoning here. <rdar://problem/8663544>
1199 void* rdar8663544();
1200 typedef struct {} Val8663544;
1201 Val8663544 bazR8663544() {
1202 Val8663544(*func) () = (Val8663544(*) ()) rdar8663544;
1206 // PR 8619 - Handle ternary expressions with a call to a noreturn function.
1207 // This previously resulted in a crash.
1208 void pr8619_noreturn(int x) __attribute__((noreturn));
1210 void pr8619(int a, int b, int c) {
1211 a ?: pr8619_noreturn(b || c);
1215 // PR 8646 - crash in the analyzer when handling unions.
1216 union pr8648_union {
1217 signed long long pr8648_union_field;
1221 union pr8648_union x = { .pr8648_union_field = 0LL };
1222 y = x.pr8648_union_field;
1224 union pr8648_union z;
1225 z = (union pr8648_union) { .pr8648_union_field = 0LL };
1227 union pr8648_union w;
1228 w = ({ (union pr8648_union) { .pr8648_union_field = 0LL }; });
1230 // crash, no assignment
1231 (void) ({ (union pr8648_union) { .pr8648_union_field = 0LL }; }).pr8648_union_field;
1233 // crash with assignment
1234 y = ({ (union pr8648_union) { .pr8648_union_field = 0LL }; }).pr8648_union_field;