| 1 |
// RUN: %clang_analyze_cc1 -verify -Wno-objc-root-class %s \ |
| 2 |
// RUN: -analyzer-checker=core \ |
| 3 |
// RUN: -analyzer-checker=nullability \ |
| 4 |
// RUN: -analyzer-checker=osx.cocoa.NSError \ |
| 5 |
// RUN: -analyzer-checker=osx.coreFoundation.CFError |
| 6 |
|
| 7 |
typedef signed char BOOL; |
| 8 |
typedef int NSInteger; |
| 9 |
typedef struct _NSZone NSZone; |
| 10 |
@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; |
| 11 |
@protocol NSObject - (BOOL)isEqual:(id)object; @end |
| 12 |
@protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end |
| 13 |
@protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end |
| 14 |
@interface NSObject <NSObject> {} @end |
| 15 |
@class NSDictionary; |
| 16 |
@interface NSError : NSObject <NSCopying, NSCoding> {} |
| 17 |
+ (id)errorWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict; |
| 18 |
@end |
| 19 |
extern NSString * const NSXMLParserErrorDomain ; |
| 20 |
|
| 21 |
@interface A |
| 22 |
- (void)myMethodWhichMayFail:(NSError **)error; |
| 23 |
- (BOOL)myMethodWhichMayFail2:(NSError **)error; |
| 24 |
- (BOOL)myMethodWhichMayFail3:(NSError **_Nonnull)error; |
| 25 |
- (BOOL)myMethodWhichMayFail4:(NSError **)error __attribute__((nonnull)); |
| 26 |
@end |
| 27 |
|
| 28 |
@implementation A |
| 29 |
- (void)myMethodWhichMayFail:(NSError **)error { // expected-warning {{Method accepting NSError** should have a non-void return value to indicate whether or not an error occurred}} |
| 30 |
*error = [NSError errorWithDomain:@"domain" code:1 userInfo:0]; // expected-warning {{Potential null dereference. According to coding standards in 'Creating and Returning NSError Objects' the parameter may be null [osx.cocoa.NSError]}} |
| 31 |
} |
| 32 |
|
| 33 |
- (BOOL)myMethodWhichMayFail2:(NSError **)error { // no-warning |
| 34 |
if (error) *error = [NSError errorWithDomain:@"domain" code:1 userInfo:0]; // no-warning |
| 35 |
return 0; |
| 36 |
} |
| 37 |
|
| 38 |
- (BOOL)myMethodWhichMayFail3:(NSError **_Nonnull)error { // no-warning |
| 39 |
*error = [NSError errorWithDomain:@"domain" code:1 userInfo:0]; // no-warning |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
- (BOOL)myMethodWhichMayFail4:(NSError **)error { // no-warning |
| 44 |
*error = [NSError errorWithDomain:@"domain" code:1 userInfo:0]; // no-warning |
| 45 |
return 0; |
| 46 |
} |
| 47 |
@end |
| 48 |
|
| 49 |
struct __CFError {}; |
| 50 |
typedef struct __CFError* CFErrorRef; |
| 51 |
|
| 52 |
void foo(CFErrorRef* error) { // expected-warning {{Function accepting CFErrorRef* should have a non-void return value to indicate whether or not an error occurred}} |
| 53 |
*error = 0; // expected-warning {{Potential null dereference. According to coding standards documented in CoreFoundation/CFError.h the parameter may be null [osx.coreFoundation.CFError]}} |
| 54 |
} |
| 55 |
|
| 56 |
int f1(CFErrorRef* error) { |
| 57 |
if (error) *error = 0; // no-warning |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
int f2(CFErrorRef* error) { |
| 62 |
if (0 != error) *error = 0; // no-warning |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
int f3(CFErrorRef* error) { |
| 67 |
if (error != 0) *error = 0; // no-warning |
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
int __attribute__((nonnull)) f4(CFErrorRef *error) { |
| 72 |
*error = 0; // no-warning |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
int __attribute__((nonnull(1))) f5(int *x, CFErrorRef *error) { |
| 77 |
*error = 0; // expected-warning {{Potential null dereference. According to coding standards documented in CoreFoundation/CFError.h the parameter may be null [osx.coreFoundation.CFError]}} |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
int __attribute__((nonnull(2))) f6(int *x, CFErrorRef *error) { |
| 82 |
*error = 0; // no-warning |
| 83 |
return 0; |
| 84 |
} |