85 lines · plain
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.CFError6 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; @end12@protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end13@protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end14@interface NSObject <NSObject> {} @end15@class NSDictionary;16@interface NSError : NSObject <NSCopying, NSCoding> {}17+ (id)errorWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict;18@end19extern NSString * const NSXMLParserErrorDomain ;20 21@interface A22- (void)myMethodWhichMayFail:(NSError **)error;23- (BOOL)myMethodWhichMayFail2:(NSError **)error;24- (BOOL)myMethodWhichMayFail3:(NSError **_Nonnull)error;25- (BOOL)myMethodWhichMayFail4:(NSError **)error __attribute__((nonnull));26@end27 28@implementation A29- (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-warning34 if (error) *error = [NSError errorWithDomain:@"domain" code:1 userInfo:0]; // no-warning35 return 0;36}37 38- (BOOL)myMethodWhichMayFail3:(NSError **_Nonnull)error { // no-warning39 *error = [NSError errorWithDomain:@"domain" code:1 userInfo:0]; // no-warning40 return 0;41}42 43- (BOOL)myMethodWhichMayFail4:(NSError **)error { // no-warning44 *error = [NSError errorWithDomain:@"domain" code:1 userInfo:0]; // no-warning45 return 0;46}47@end48 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. According to coding standards documented in CoreFoundation/CFError.h the parameter may be null [osx.coreFoundation.CFError]}}54}55 56int f1(CFErrorRef* error) {57 if (error) *error = 0; // no-warning58 return 0;59}60 61int f2(CFErrorRef* error) {62 if (0 != error) *error = 0; // no-warning63 return 0;64}65 66int f3(CFErrorRef* error) {67 if (error != 0) *error = 0; // no-warning68 return 0;69}70 71int __attribute__((nonnull)) f4(CFErrorRef *error) {72 *error = 0; // no-warning73 return 0;74}75 76int __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 81int __attribute__((nonnull(2))) f6(int *x, CFErrorRef *error) {82 *error = 0; // no-warning83 return 0;84}85