1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.cocoa.SuperDealloc,debug.ExprInspection -analyzer-output=text -verify %s
void clang_analyzer_warnIfReached();
#define nil ((id)0)
typedef unsigned long NSUInteger;
@protocol NSObject
- (instancetype)retain;
- (oneway void)release;
@end
@interface NSObject <NSObject> { }
- (void)dealloc;
- (instancetype)init;
@end
typedef struct objc_selector *SEL;
//===------------------------------------------------------------------------===
// <rdar://problem/6953275>
// Check that 'self' is not referenced after calling '[super dealloc]'.
@interface SuperDeallocThenReleaseIvarClass : NSObject {
NSObject *_ivar;
}
@end
@implementation SuperDeallocThenReleaseIvarClass
- (instancetype)initWithIvar:(NSObject *)ivar {
self = [super init];
if (!self)
return nil;
_ivar = [ivar retain];
return self;
}
- (void)dealloc {
[super dealloc]; // expected-note {{[super dealloc] called here}}
[_ivar release]; // expected-warning {{Use of instance variable '_ivar' after 'self' has been deallocated}}
// expected-note@-1 {{Use of instance variable '_ivar' after 'self' has been deallocated}}
}
@end
@interface SuperDeallocThenAssignNilToIvarClass : NSObject {
NSObject *_delegate;
}
@end
@implementation SuperDeallocThenAssignNilToIvarClass
- (instancetype)initWithDelegate:(NSObject *)delegate {
self = [super init];
if (!self)
return nil;
_delegate = delegate;
return self;
}
- (void)dealloc {
[super dealloc]; // expected-note {{[super dealloc] called here}}
_delegate = nil; // expected-warning {{Use of instance variable '_delegate' after 'self' has been deallocated}}
// expected-note@-1 {{Use of instance variable '_delegate' after 'self' has been deallocated}}
}
@end
struct SomeStruct {
int f;
};
@interface SuperDeallocThenAssignIvarField : NSObject {
struct SomeStruct _s;
}
@end
@implementation SuperDeallocThenAssignIvarField
- (void)dealloc {
[super dealloc]; // expected-note {{[super dealloc] called here}}
_s.f = 7; // expected-warning {{Use of instance variable '_s' after 'self' has been deallocated}}
// expected-note@-1 {{Use of instance variable '_s' after 'self' has been deallocated}}
}
@end
@interface OtherClassWithIvar {
@public
int _otherIvar;
}
@end;
@interface SuperDeallocThenAssignIvarIvar : NSObject {
OtherClassWithIvar *_ivar;
}
@end
@implementation SuperDeallocThenAssignIvarIvar
- (void)dealloc {
[super dealloc]; // expected-note {{[super dealloc] called here}}
_ivar->_otherIvar = 7; // expected-warning {{Use of instance variable '_ivar' after 'self' has been deallocated}}
// expected-note@-1 {{Use of instance variable '_ivar' after 'self' has been deallocated}}
}
@end
@interface SuperDeallocThenAssignSelfIvar : NSObject {
NSObject *_ivar;
}
@end
@implementation SuperDeallocThenAssignSelfIvar
- (void)dealloc {
[super dealloc]; // expected-note {{[super dealloc] called here}}
self->_ivar = nil; // expected-warning {{Use of instance variable '_ivar' after 'self' has been deallocated}}
// expected-note@-1 {{Use of instance variable '_ivar' after 'self' has been deallocated}}
}
@end
@interface SuperDeallocThenReleasePropertyClass : NSObject { }
@property (retain) NSObject *ivar;
@end
@implementation SuperDeallocThenReleasePropertyClass
- (instancetype)initWithProperty:(NSObject *)ivar {
self = [super init];
if (!self)
return nil;
self.ivar = ivar;
return self;
}
- (void)dealloc {
[super dealloc]; // expected-note {{[super dealloc] called here}}
self.ivar = nil; // expected-warning {{Use of 'self' after it has been deallocated}}
// expected-note@-1 {{Use of 'self' after it has been deallocated}}
}
@end
@interface SuperDeallocThenAssignNilToPropertyClass : NSObject { }
@property (assign) NSObject *delegate;
@end
@implementation SuperDeallocThenAssignNilToPropertyClass
- (instancetype)initWithDelegate:(NSObject *)delegate {
self = [super init];
if (!self)
return nil;
self.delegate = delegate;
return self;
}
- (void)dealloc {
[super dealloc]; // expected-note {{[super dealloc] called here}}
self.delegate = nil; // expected-warning {{Use of 'self' after it has been deallocated}}
// expected-note@-1 {{Use of 'self' after it has been deallocated}}
}
@end
@interface SuperDeallocThenCallInstanceMethodClass : NSObject { }
- (void)_invalidate;
@end
@implementation SuperDeallocThenCallInstanceMethodClass
- (void)_invalidate {
}
- (void)dealloc {
[super dealloc]; // expected-note {{[super dealloc] called here}}
[self _invalidate]; // expected-warning {{Use of 'self' after it has been deallocated}}
// expected-note@-1 {{Use of 'self' after it has been deallocated}}
}
@end
@interface SuperDeallocThenCallNonObjectiveCMethodClass : NSObject { }
@end
static void _invalidate(NSObject *object) {
(void)object;
}
@implementation SuperDeallocThenCallNonObjectiveCMethodClass
- (void)dealloc {
[super dealloc]; // expected-note {{[super dealloc] called here}}
_invalidate(self); // expected-warning {{Use of 'self' after it has been deallocated}}
// expected-note@-1 {{Use of 'self' after it has been deallocated}}
}
@end
@interface SuperDeallocThenCallObjectiveClassMethodClass : NSObject { }
@end
@implementation SuperDeallocThenCallObjectiveClassMethodClass
+ (void) invalidate:(id)arg; {
}
- (void)dealloc {
[super dealloc]; // expected-note {{[super dealloc] called here}}
[SuperDeallocThenCallObjectiveClassMethodClass invalidate:self]; // expected-warning {{Use of 'self' after it has been deallocated}}
// expected-note@-1 {{Use of 'self' after it has been deallocated}}
}
@end
@interface TwoSuperDeallocCallsClass : NSObject {
NSObject *_ivar;
}
- (void)_invalidate;
@end
@implementation TwoSuperDeallocCallsClass
- (void)_invalidate {
}
- (void)dealloc {
if (_ivar) { // expected-note {{Assuming the condition is false}} expected-note {{Taking false branch}}
[_ivar release];
[super dealloc];
return;
}
[super dealloc]; // expected-note {{[super dealloc] called here}}
[self _invalidate]; // expected-warning {{Use of 'self' after it has been deallocated}}
// expected-note@-1 {{Use of 'self' after it has been deallocated}}
}
@end
//===------------------------------------------------------------------------===
// Warn about calling [super dealloc] twice due to missing return statement.
@interface MissingReturnCausesDoubleSuperDeallocClass : NSObject {
NSObject *_ivar;
}
@end
@implementation MissingReturnCausesDoubleSuperDeallocClass
- (void)dealloc {
if (_ivar) { // expected-note {{Assuming the condition is true}} expected-note {{Taking true branch}}
[_ivar release];
[super dealloc]; // expected-note {{[super dealloc] called here}}
// return;
}
[super dealloc]; // expected-warning{{[super dealloc] should not be called multiple times}}
// expected-note@-1{{[super dealloc] should not be called multiple times}}
}
@end
//===------------------------------------------------------------------------===
// Warn about calling [super dealloc] twice in two different methods.
@interface SuperDeallocInOtherMethodClass : NSObject {
NSObject *_ivar;
}
- (void)_cleanup;
@end
@implementation SuperDeallocInOtherMethodClass
- (void)_cleanup {
[_ivar release];
[super dealloc]; // expected-note {{[super dealloc] called here}}
}
- (void)dealloc {
[self _cleanup]; // expected-note {{Calling '_cleanup'}}
//expected-note@-1 {{Returning from '_cleanup'}}
[super dealloc]; // expected-warning {{[super dealloc] should not be called multiple times}}
// expected-note@-1 {{[super dealloc] should not be called multiple times}}
}
@end
//===------------------------------------------------------------------------===
// Do not warn about calling [super dealloc] recursively for different objects
// of the same type with custom retain counting.
//
// A class that contains an ivar of itself with custom retain counting (such
// as provided by _OBJC_SUPPORTED_INLINE_REFCNT_WITH_DEALLOC2MAIN) can generate
// a false positive that [super dealloc] is called twice if each object instance
// is not tracked separately by the checker. This test case is just a simple
// approximation to trigger the false positive.
@class ClassWithOwnIvarInstanceClass;
@interface ClassWithOwnIvarInstanceClass : NSObject {
ClassWithOwnIvarInstanceClass *_ivar;
NSUInteger _retainCount;
}
@end
@implementation ClassWithOwnIvarInstanceClass
- (instancetype)retain {
++_retainCount;
return self;
}
- (oneway void)release {
--_retainCount;
if (!_retainCount)
[self dealloc];
}
- (void)dealloc {
[_ivar release];
[super dealloc]; // no warning: different instances of same class
}
@end
//===------------------------------------------------------------------------===
// Do not warn about calling [super dealloc] twice if +dealloc is a class
// method.
@interface SuperDeallocClassMethodIgnoredClass : NSObject { }
+ (void)dealloc;
@end
@implementation SuperDeallocClassMethodIgnoredClass
+ (void)dealloc { }
@end
@interface SuperDeallocClassMethodIgnoredSubClass : NSObject { }
+ (void)dealloc;
@end
@implementation SuperDeallocClassMethodIgnoredSubClass
+ (void)dealloc {
[super dealloc];
[super dealloc]; // no warning: class method
}
@end
//===------------------------------------------------------------------------===
// Do not warn about calling [super dealloc] twice if when the analyzer has
// inlined the call to its super deallocator.
@interface SuperClassCallingSuperDealloc : NSObject {
NSObject *_ivar;
}
@end
@implementation SuperClassCallingSuperDealloc
- (void)dealloc; {
[_ivar release]; // no-warning
[super dealloc];
}
@end
@interface SubclassCallingSuperDealloc : SuperClassCallingSuperDealloc
@end
@implementation SubclassCallingSuperDealloc
- (void)dealloc; {
[super dealloc];
}
@end
//===------------------------------------------------------------------------===
// Treat calling [super dealloc] twice as as a sink.
@interface CallingSuperDeallocTwiceIsSink : NSObject
@end
@implementation CallingSuperDeallocTwiceIsSink
- (void)dealloc; {
[super dealloc]; // expected-note {{[super dealloc] called here}}
[super dealloc]; // expected-warning {{[super dealloc] should not be called multiple times}}
// expected-note@-1 {{[super dealloc] should not be called multiple times}}
clang_analyzer_warnIfReached(); // no-warning
}
@end
//===------------------------------------------------------------------------===
// Test path notes with intervening method call on self.
@interface InterveningMethodCallOnSelf : NSObject
@end
@implementation InterveningMethodCallOnSelf
- (void)anotherMethod {
}
- (void)dealloc; {
[super dealloc]; // expected-note {{[super dealloc] called here}}
[self anotherMethod]; // expected-warning {{Use of 'self' after it has been deallocated}}
// expected-note@-1 {{Use of 'self' after it has been deallocated}}
[super dealloc];
}
@end
|