| 1 |
// RUN: %clang_analyze_cc1 -fblocks \ |
| 2 |
// RUN: -analyzer-checker=core \ |
| 3 |
// RUN: -analyzer-checker=osx.cocoa.MissingSuperCall \ |
| 4 |
// RUN: -analyzer-checker=osx.cocoa.NSError \ |
| 5 |
// RUN: -analyzer-checker=osx.ObjCProperty \ |
| 6 |
// RUN: -analyzer-checker=osx.cocoa.RetainCount \ |
| 7 |
// RUN: -analyzer-checker=unix.Malloc \ |
| 8 |
// RUN: -analyzer-checker=alpha.core.CastToStruct \ |
| 9 |
// RUN: -Wno-unused-value -Wno-objc-root-class -verify %s |
| 10 |
|
| 11 |
#define SUPPRESS __attribute__((suppress)) |
| 12 |
#define SUPPRESS_SPECIFIC(...) __attribute__((suppress(__VA_ARGS__))) |
| 13 |
|
| 14 |
@protocol NSObject |
| 15 |
- (id)retain; |
| 16 |
- (oneway void)release; |
| 17 |
@end |
| 18 |
@interface NSObject <NSObject> { |
| 19 |
} |
| 20 |
- (id)init; |
| 21 |
+ (id)alloc; |
| 22 |
@end |
| 23 |
typedef int NSInteger; |
| 24 |
typedef char BOOL; |
| 25 |
typedef struct _NSZone NSZone; |
| 26 |
@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 27 |
@protocol NSCopying |
| 28 |
- (id)copyWithZone:(NSZone *)zone; |
| 29 |
@end |
| 30 |
@protocol NSCoding |
| 31 |
- (void)encodeWithCoder:(NSCoder *)aCoder; |
| 32 |
@end |
| 33 |
@class NSDictionary; |
| 34 |
@interface NSError : NSObject <NSCopying, NSCoding> { |
| 35 |
} |
| 36 |
+ (id)errorWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict; |
| 37 |
@end |
| 38 |
|
| 39 |
@interface NSMutableString : NSObject |
| 40 |
@end |
| 41 |
|
| 42 |
typedef __typeof__(sizeof(int)) size_t; |
| 43 |
void *malloc(size_t); |
| 44 |
void free(void *); |
| 45 |
|
| 46 |
void dereference_1() { |
| 47 |
int *x = 0; |
| 48 |
*x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} |
| 49 |
} |
| 50 |
|
| 51 |
void dereference_suppression_1() { |
| 52 |
int *x = 0; |
| 53 |
SUPPRESS { *x; } // no-warning |
| 54 |
} |
| 55 |
|
| 56 |
void dereference_2() { |
| 57 |
int *x = 0; |
| 58 |
if (*x) { // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
void dereference_suppression_2() { |
| 63 |
int *x = 0; |
| 64 |
SUPPRESS if (*x) { // no-warning |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
void dereference_suppression_2a() { |
| 69 |
int *x = 0; |
| 70 |
// FIXME: Implement suppressing individual checkers. |
| 71 |
SUPPRESS_SPECIFIC("core.NullDereference") if (*x) { // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
void dereference_suppression_2b() { |
| 76 |
int *x = 0; |
| 77 |
// This is not a MallocChecker issue so it shouldn't be suppressed. (Though the attribute |
| 78 |
// doesn't really understand any of those arguments yet.) |
| 79 |
SUPPRESS_SPECIFIC("unix.Malloc") if (*x) { // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
void dereference_3(int cond) { |
| 84 |
int *x = 0; |
| 85 |
if (cond) { |
| 86 |
(*x)++; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
void dereference_suppression_3(int cond) { |
| 91 |
int *x = 0; |
| 92 |
SUPPRESS if (cond) { |
| 93 |
(*x)++; // no-warning |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
void dereference_4() { |
| 98 |
int *x = 0; |
| 99 |
int y = *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} |
| 100 |
} |
| 101 |
|
| 102 |
void dereference_suppression_4() { |
| 103 |
int *x = 0; |
| 104 |
SUPPRESS int y = *x; // no-warning |
| 105 |
} |
| 106 |
|
| 107 |
void dereference_5() { |
| 108 |
int *x = 0; |
| 109 |
int y = *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} |
| 110 |
int z = *x; // no-warning (duplicate) |
| 111 |
} |
| 112 |
|
| 113 |
void dereference_suppression_5_1() { |
| 114 |
int *x = 0; |
| 115 |
SUPPRESS int y = *x; // no-warning |
| 116 |
int z = *x; // no-warning (duplicate) |
| 117 |
} |
| 118 |
|
| 119 |
void dereference_suppression_5_2() { |
| 120 |
int *x = 0; |
| 121 |
int y = *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} |
| 122 |
SUPPRESS int z = *x; // no-warning |
| 123 |
} |
| 124 |
|
| 125 |
void do_deref(int *y) { |
| 126 |
*y = 1; // expected-warning{{Dereference of null pointer (loaded from variable 'y')}} |
| 127 |
} |
| 128 |
|
| 129 |
void dereference_interprocedural() { |
| 130 |
int *x = 0; |
| 131 |
do_deref(x); |
| 132 |
} |
| 133 |
|
| 134 |
void do_deref_suppressed(int *y) { |
| 135 |
SUPPRESS *y = 1; // no-warning |
| 136 |
} |
| 137 |
|
| 138 |
void dereference_interprocedural_suppressed() { |
| 139 |
int *x = 0; |
| 140 |
do_deref_suppressed(x); |
| 141 |
} |
| 142 |
|
| 143 |
int malloc_leak_1() { |
| 144 |
int *x = (int *)malloc(sizeof(int)); |
| 145 |
*x = 42; |
| 146 |
return *x; // expected-warning{{Potential leak of memory pointed to by 'x'}} |
| 147 |
} |
| 148 |
|
| 149 |
int malloc_leak_suppression_1_1() { |
| 150 |
SUPPRESS int *x = (int *)malloc(sizeof(int)); |
| 151 |
*x = 42; |
| 152 |
return *x; |
| 153 |
} |
| 154 |
|
| 155 |
int malloc_leak_suppression_1_2() { |
| 156 |
int *x = (int *)malloc(sizeof(int)); |
| 157 |
*x = 42; |
| 158 |
SUPPRESS return *x; |
| 159 |
} |
| 160 |
|
| 161 |
void malloc_leak_2() { |
| 162 |
int *x = (int *)malloc(sizeof(int)); |
| 163 |
*x = 42; |
| 164 |
} // expected-warning{{Potential leak of memory pointed to by 'x'}} |
| 165 |
|
| 166 |
void malloc_leak_suppression_2_1() { |
| 167 |
SUPPRESS int *x = (int *)malloc(sizeof(int)); |
| 168 |
*x = 42; |
| 169 |
} |
| 170 |
|
| 171 |
void malloc_leak_suppression_2_2() SUPPRESS { |
| 172 |
int *x = (int *)malloc(sizeof(int)); |
| 173 |
*x = 42; |
| 174 |
} // no-warning |
| 175 |
|
| 176 |
SUPPRESS void malloc_leak_suppression_2_3() { |
| 177 |
int *x = (int *)malloc(sizeof(int)); |
| 178 |
*x = 42; |
| 179 |
} // no-warning |
| 180 |
|
| 181 |
void malloc_leak_suppression_2_4(int cond) { |
| 182 |
int *x = (int *)malloc(sizeof(int)); |
| 183 |
*x = 42; |
| 184 |
SUPPRESS; |
| 185 |
// FIXME: The warning should be suppressed but dead symbol elimination |
| 186 |
// happens too late. |
| 187 |
} // expected-warning{{Potential leak of memory pointed to by 'x'}} |
| 188 |
|
| 189 |
void retain_release_leak_1() { |
| 190 |
[[NSMutableString alloc] init]; // expected-warning{{Potential leak of an object of type 'NSMutableString *'}} |
| 191 |
} |
| 192 |
|
| 193 |
void retain_release_leak_suppression_1() { |
| 194 |
SUPPRESS { [[NSMutableString alloc] init]; } |
| 195 |
} |
| 196 |
|
| 197 |
void retain_release_leak_2(int cond) { |
| 198 |
id obj = [[NSMutableString alloc] init]; // expected-warning{{Potential leak of an object stored into 'obj'}} |
| 199 |
if (cond) { |
| 200 |
[obj release]; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
void retain_release_leak__suppression_2(int cond) { |
| 205 |
SUPPRESS id obj = [[NSMutableString alloc] init]; |
| 206 |
if (cond) { |
| 207 |
[obj release]; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
@interface UIResponder : NSObject { |
| 212 |
} |
| 213 |
- (char)resignFirstResponder; |
| 214 |
@end |
| 215 |
|
| 216 |
@interface Test : UIResponder { |
| 217 |
} |
| 218 |
@property(copy) NSMutableString *mutableStr; |
| 219 |
// expected-warning@-1 {{Property of mutable type 'NSMutableString' has 'copy' attribute; an immutable object will be stored instead}} |
| 220 |
@end |
| 221 |
@implementation Test |
| 222 |
|
| 223 |
- (BOOL)resignFirstResponder { |
| 224 |
return 0; |
| 225 |
} // expected-warning {{The 'resignFirstResponder' instance method in UIResponder subclass 'Test' is missing a [super resignFirstResponder] call}} |
| 226 |
|
| 227 |
- (void)methodWhichMayFail:(NSError **)error { |
| 228 |
// expected-warning@-1 {{Method accepting NSError** should have a non-void return value to indicate whether or not an error occurred}} |
| 229 |
} |
| 230 |
@end |
| 231 |
|
| 232 |
@interface TestSuppress : UIResponder { |
| 233 |
} |
| 234 |
@property(copy) SUPPRESS NSMutableString *mutableStr; // no-warning |
| 235 |
@end |
| 236 |
@implementation TestSuppress |
| 237 |
|
| 238 |
- (BOOL)resignFirstResponder SUPPRESS { // no-warning |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
- (void)methodWhichMayFail:(NSError **)error SUPPRESS { // no-warning |
| 243 |
} |
| 244 |
@end |
| 245 |
|
| 246 |
struct AB { |
| 247 |
int A, B; |
| 248 |
}; |
| 249 |
|
| 250 |
struct ABC { |
| 251 |
int A, B, C; |
| 252 |
}; |
| 253 |
|
| 254 |
void ast_checker_1() { |
| 255 |
struct AB Ab; |
| 256 |
struct ABC *Abc; |
| 257 |
Abc = (struct ABC *)&Ab; // expected-warning {{Casting data to a larger structure type and accessing a field can lead to memory access errors or data corruption}} |
| 258 |
} |
| 259 |
|
| 260 |
void ast_checker_suppress_1() { |
| 261 |
struct AB Ab; |
| 262 |
struct ABC *Abc; |
| 263 |
SUPPRESS { Abc = (struct ABC *)&Ab; } |
| 264 |
} |
| 265 |
|
| 266 |
SUPPRESS int suppressed_function() { |
| 267 |
int *x = 0; |
| 268 |
return *x; // no-warning |
| 269 |
} |
| 270 |
|
| 271 |
SUPPRESS int suppressed_function_forward(); |
| 272 |
int suppressed_function_forward() { |
| 273 |
int *x = 0; |
| 274 |
return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} |
| 275 |
} |
| 276 |
|
| 277 |
int suppressed_function_backward(); |
| 278 |
SUPPRESS int suppressed_function_backward() { |
| 279 |
int *x = 0; |
| 280 |
return *x; // no-warning |
| 281 |
} |
| 282 |
|
| 283 |
SUPPRESS |
| 284 |
@interface SuppressedInterface |
| 285 |
-(int)suppressedMethod; |
| 286 |
-(int)regularMethod SUPPRESS; |
| 287 |
@end |
| 288 |
|
| 289 |
@implementation SuppressedInterface |
| 290 |
-(int)suppressedMethod SUPPRESS { |
| 291 |
int *x = 0; |
| 292 |
return *x; // no-warning |
| 293 |
} |
| 294 |
|
| 295 |
// This one is NOT suppressed by the attribute on the forward declaration, |
| 296 |
// and it's also NOT suppressed by the attribute on the entire interface. |
| 297 |
-(int)regularMethod { |
| 298 |
int *x = 0; |
| 299 |
return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} |
| 300 |
} |
| 301 |
@end |