Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.RetainCount -analyzer-store=region -verify -Wno-objc-root-class %s |
Argyrios Kyrtzidis | 14429b9 | 2011-01-25 00:04:03 +0000 | [diff] [blame] | 2 | |
| 3 | typedef signed char BOOL; |
| 4 | typedef unsigned int NSUInteger; |
| 5 | typedef struct _NSZone NSZone; |
| 6 | @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 7 | @protocol NSObject - (BOOL)isEqual:(id)object; @end |
| 8 | @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end |
| 9 | @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end |
| 10 | @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end |
| 11 | @interface NSObject <NSObject> {} |
| 12 | +(id)alloc; |
| 13 | -(id)init; |
| 14 | -(id)autorelease; |
| 15 | -(id)copy; |
| 16 | -(id)retain; |
| 17 | @end |
| 18 | @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding> |
| 19 | - (NSUInteger)length; |
| 20 | -(id)initWithFormat:(NSString *)f,...; |
| 21 | -(BOOL)isEqualToString:(NSString *)s; |
| 22 | + (id)string; |
| 23 | @end |
| 24 | @interface NSNumber : NSObject {} |
| 25 | +(id)alloc; |
| 26 | -(id)initWithInteger:(int)i; |
| 27 | @end |
| 28 | |
| 29 | // rdar://6946338 |
| 30 | |
| 31 | @interface Test1 : NSObject { |
| 32 | NSString *text; |
| 33 | } |
| 34 | -(id)myMethod; |
| 35 | @property (nonatomic, assign) NSString *text; |
| 36 | @end |
| 37 | |
| 38 | |
| 39 | @implementation Test1 |
| 40 | |
| 41 | @synthesize text; |
| 42 | |
| 43 | -(id)myMethod { |
| 44 | Test1 *cell = [[[Test1 alloc] init] autorelease]; |
| 45 | |
| 46 | NSString *string1 = [[NSString alloc] initWithFormat:@"test %f", 0.0]; // expected-warning {{Potential leak}} |
| 47 | cell.text = string1; |
| 48 | |
| 49 | return cell; |
| 50 | } |
| 51 | |
| 52 | @end |
| 53 | |
| 54 | |
| 55 | // rdar://8824416 |
| 56 | |
| 57 | @interface MyNumber : NSObject |
| 58 | { |
| 59 | NSNumber* _myNumber; |
| 60 | } |
| 61 | |
| 62 | - (id)initWithNumber:(NSNumber *)number; |
| 63 | |
| 64 | @property (nonatomic, readonly) NSNumber* myNumber; |
| 65 | @property (nonatomic, readonly) NSNumber* newMyNumber; |
| 66 | |
| 67 | @end |
| 68 | |
| 69 | @implementation MyNumber |
| 70 | @synthesize myNumber=_myNumber; |
| 71 | |
| 72 | - (id)initWithNumber:(NSNumber *)number |
| 73 | { |
| 74 | self = [super init]; |
| 75 | |
| 76 | if ( self ) |
| 77 | { |
| 78 | _myNumber = [number copy]; |
| 79 | } |
| 80 | |
| 81 | return self; |
| 82 | } |
| 83 | |
| 84 | - (NSNumber*)newMyNumber |
| 85 | { |
| 86 | if ( _myNumber ) |
| 87 | return [_myNumber retain]; |
| 88 | |
| 89 | return [[NSNumber alloc] initWithInteger:1]; |
| 90 | } |
| 91 | |
| 92 | - (id)valueForUndefinedKey:(NSString*)key |
| 93 | { |
| 94 | id value = 0; |
| 95 | |
| 96 | if ([key isEqualToString:@"MyIvarNumberAsPropertyOverReleased"]) |
| 97 | value = self.myNumber; // _myNumber will be over released, since the value returned from self.myNumber is not retained. |
| 98 | else if ([key isEqualToString:@"MyIvarNumberAsPropertyOk"]) |
| 99 | value = [self.myNumber retain]; // this line fixes the over release |
| 100 | else if ([key isEqualToString:@"MyIvarNumberAsNewMyNumber"]) |
| 101 | value = self.newMyNumber; // this one is ok, since value is returned retained |
| 102 | else |
| 103 | value = [[NSNumber alloc] initWithInteger:0]; |
| 104 | |
| 105 | return [value autorelease]; // expected-warning {{Object sent -autorelease too many times}} |
| 106 | } |
| 107 | |
| 108 | @end |
| 109 | |
| 110 | NSNumber* numberFromMyNumberProperty(MyNumber* aMyNumber) |
| 111 | { |
| 112 | NSNumber* result = aMyNumber.myNumber; |
| 113 | |
| 114 | return [result autorelease]; // expected-warning {{Object sent -autorelease too many times}} |
| 115 | } |
| 116 | |
| 117 | |
| 118 | // rdar://6611873 |
| 119 | |
| 120 | @interface Person : NSObject { |
| 121 | NSString *_name; |
| 122 | } |
| 123 | @property (retain) NSString * name; |
| 124 | @end |
| 125 | |
| 126 | @implementation Person |
| 127 | @synthesize name = _name; |
| 128 | @end |
| 129 | |
| 130 | void rdar6611873() { |
| 131 | Person *p = [[[Person alloc] init] autorelease]; |
| 132 | |
| 133 | p.name = [[NSString string] retain]; // expected-warning {{leak}} |
| 134 | p.name = [[NSString alloc] init]; // expected-warning {{leak}} |
| 135 | } |
Argyrios Kyrtzidis | 9319b56 | 2011-01-27 16:17:11 +0000 | [diff] [blame] | 136 | |
| 137 | @interface SubPerson : Person |
| 138 | -(NSString *)foo; |
| 139 | @end |
| 140 | |
| 141 | @implementation SubPerson |
| 142 | -(NSString *)foo { |
| 143 | return super.name; |
| 144 | } |
| 145 | @end |
Ted Kremenek | b673a41 | 2012-02-18 20:53:30 +0000 | [diff] [blame] | 146 | |
| 147 | // <rdar://problem/9241180> Static analyzer doesn't detect uninitialized variable issues for property accesses |
| 148 | @interface RDar9241180 |
| 149 | @property (readwrite,assign) id x; |
| 150 | -(id)testAnalyzer1:(int) y; |
| 151 | -(void)testAnalyzer2; |
| 152 | @end |
| 153 | |
| 154 | @implementation RDar9241180 |
| 155 | @synthesize x; |
| 156 | -(id)testAnalyzer1:(int)y { |
| 157 | RDar9241180 *o; |
| 158 | if (y && o.x) // expected-warning {{Property access on an uninitialized object pointer}} |
| 159 | return o; |
| 160 | return o; // expected-warning {{Undefined or garbage value returned to caller}} |
| 161 | } |
| 162 | -(void)testAnalyzer2 { |
| 163 | id y; |
| 164 | self.x = y; // expected-warning {{Argument for property setter is an uninitialized value}} |
| 165 | } |
| 166 | @end |
| 167 | |
| 168 | |