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 7typedef signed char BOOL; 8typedef int NSInteger; 9typedef 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 19extern 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}} 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 49struct __CFError {}; 50typedef struct __CFError* CFErrorRef; 51 52void 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}} 54} 55 56int f1(CFErrorRef* error) { 57 if (error) *error = 0; // no-warning 58 return 0; 59} 60 61int f2(CFErrorRef* error) { 62 if (0 != error) *error = 0; // no-warning 63 return 0; 64} 65 66int f3(CFErrorRef* error) { 67 if (error != 0) *error = 0; // no-warning 68 return 0; 69} 70 71int __attribute__((nonnull)) f4(CFErrorRef *error) { 72 *error = 0; // no-warning 73 return 0; 74} 75 76int __attribute__((nonnull(1))) f5(int *x, CFErrorRef *error) { 77 *error = 0; // expected-warning {{Potential null dereference}} 78 return 0; 79} 80 81int __attribute__((nonnull(2))) f6(int *x, CFErrorRef *error) { 82 *error = 0; // no-warning 83 return 0; 84} 85