184 lines · plain
1// RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount -analyzer-output=text -analyzer-config suppress-null-return-paths=false -fblocks -verify %s2// RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.cocoa.NilArg,osx.cocoa.RetainCount -analyzer-output=plist-multi-file -analyzer-config suppress-null-return-paths=false -fblocks %s -o %t.plist3// RUN: %normalize_plist <%t.plist | diff -ub %S/Inputs/expected-plists/path-notes.m.plist -4 5typedef struct dispatch_queue_s *dispatch_queue_t;6typedef void (^dispatch_block_t)(void);7void dispatch_sync(dispatch_queue_t, dispatch_block_t);8 9typedef long dispatch_once_t;10// Note: The real dispatch_once has all parameters marked nonnull.11// We don't do that here so that we can trigger a null dereference inside12// the synthesized body.13void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);14 15 16@interface Test17@property int *p;18@end19 20typedef unsigned long NSUInteger;21typedef signed char BOOL;22typedef struct _NSZone NSZone;23@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;24@protocol NSObject25@end26@protocol NSCopying27- (id)copyWithZone:(NSZone *)zone;28@end29@protocol NSMutableCopying30- (id)mutableCopyWithZone:(NSZone *)zone;31@end32@protocol NSCoding33- (void)encodeWithCoder:(NSCoder *)aCoder;34@end35@protocol NSFastEnumeration36@end37@protocol NSSecureCoding <NSCoding>38@required39+ (BOOL)supportsSecureCoding;40@end41@interface NSObject <NSObject> {}42- (id)init;43+ (id)alloc;44- (id)autorelease;45@end46@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>47 48- (NSUInteger)count;49- (id)objectAtIndex:(NSUInteger)index;50 51@end52 53@interface NSArray (NSExtendedArray)54- (NSArray *)arrayByAddingObject:(id)anObject;55- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx __attribute__((availability(macosx,introduced=10.8)));56@end57 58@interface NSArray (NSArrayCreation)59+ (instancetype)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;60@end61 62@interface NSMutableArray : NSArray63 64- (void)addObject:(id)anObject;65- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;66- (void)removeLastObject;67- (void)removeObjectAtIndex:(NSUInteger)index;68- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;69 70@end71 72int *getZeroIfNil(Test *x) {73 return x.p;74 // expected-note@-1 {{'p' not called because the receiver is nil}}75 // expected-note@-2 {{Returning null pointer}}76}77 78void testReturnZeroIfNil(void) {79 *getZeroIfNil(0) = 1; // expected-warning{{Dereference of null pointer}}80 // expected-note@-1 {{Calling 'getZeroIfNil'}}81 // expected-note@-2 {{Passing nil object reference via 1st parameter 'x'}}82 // expected-note@-3 {{Returning from 'getZeroIfNil'}}83 // expected-note@-4 {{Dereference of null pointer}}84}85 86 87int testDispatchSyncInlining(void) {88 extern dispatch_queue_t globalQueue;89 90 __block int x;91 92 // expected-note@+2 {{Calling 'dispatch_sync'}}93 // expected-note@+1 {{Returning from 'dispatch_sync'}}94 dispatch_sync(globalQueue, ^{95 // expected-note@-1 {{Calling anonymous block}}96 // expected-note@-2 {{Returning to caller}}97 x = 0;98 // expected-note@-1 {{The value 0 is assigned to 'x'}}99 });100 101 return 1 / x; // expected-warning{{Division by zero}}102 // expected-note@-1 {{Division by zero}}103}104 105int testDispatchSyncInliningNoPruning(int coin) {106 // This tests exactly the same case as above, except on a bug report where107 // path pruning is disabled (an uninitialized variable capture).108 // In this case 109 extern dispatch_queue_t globalQueue;110 111 __block int y;112 113 // expected-note@+1 {{Calling 'dispatch_sync'}}114 dispatch_sync(globalQueue, ^{115 // expected-note@-1 {{Calling anonymous block}}116 int x;117 // expected-note@-1 {{'x' declared without an initial value}}118 ^{ y = x; }(); // expected-warning{{Variable 'x' is uninitialized when captured by block}}119 // expected-note@-1 {{'x' is uninitialized when captured by block}}120 });121 122 return y;123}124 125 126@interface PointerWrapper127- (int *)getPtr;128@end129 130id getNil(void) {131 return 0;132}133 134void testNilReceiverHelper(int *x) {135 *x = 1; // expected-warning {{Dereference of null pointer}}136 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'x')}}137}138 139void testNilReceiver(id *x, id *y, id *z) {140 // FIXME: Should say "Assuming pointer value is null" instead.141 // For some reason we're displaying different notes for142 // tracked and untracked pointers.143 if (*y) {} // expected-note {{Assuming the condition is false}}144 // expected-note@-1 {{Taking false branch}}145 if (*x) { // expected-note {{Assuming pointer value is null}}146 // expected-note@-1 {{Taking false branch}}147 return;148 }149 // FIXME: Should say "Assuming pointer value is null" instead.150 if (*z) {} // expected-note {{Assuming the condition is false}}151 // expected-note@-1 {{Taking false branch}}152 testNilReceiverHelper([*x getPtr]);153 // expected-note@-1 {{'getPtr' not called because the receiver is nil}}154 // expected-note@-2 {{Passing null pointer value via 1st parameter 'x'}}155 // expected-note@-3 {{Calling 'testNilReceiverHelper'}}156}157 158id testCreateArrayLiteral(id myNil) {159 if (myNil) // expected-note {{Assuming 'myNil' is nil}}160 ; // expected-note@-1 {{Taking false branch}}161 return @[ @"a", myNil, @"c" ]; // expected-warning {{Array element cannot be nil}}162 //expected-note@-1 {{Array element cannot be nil}}163}164 165id testAutoreleaseTakesEffectInDispatch(void) {166 static dispatch_once_t token = 0;167 dispatch_once(&token, ^{});168 169 id x = [[[[NSObject alloc] init] autorelease] autorelease];170 // expected-note@-1 {{Method returns an instance of NSObject with a +1 retain count}}171 // expected-note@-2 {{Object autoreleased}}172 // expected-note@-3 {{Object autoreleased}}173 174 dispatch_once(&token, ^{}); // don't crash, don't warn here175 176 return x; // expected-warning{{Object autoreleased too many times}}177 // expected-note@-1 {{Object was autoreleased 2 times but the object has a +0 retain count}}178}179 180void testNullDereferenceInDispatch(void) {181 dispatch_once(0, ^{}); // no-warning, don't crash182}183 184