137 lines · plain
1// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-config suppress-null-return-paths=false -verify %s2// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify -DSUPPRESSED=1 %s3// RUN: %clang_analyze_cc1 -analyzer-checker=core -fobjc-arc -verify -DSUPPRESSED=1 %s4// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-config avoid-suppressing-null-argument-paths=true -DSUPPRESSED=1 -DNULL_ARGS=1 -verify %s5 6#define ARC __has_feature(objc_arc)7 8#ifdef SUPPRESSED9// expected-no-diagnostics10#endif11 12@interface PointerWrapper13- (int *)getPtr;14- (id)getObject;15@end16 17id getNil(void) {18 return 0;19}20 21void testNilReceiverHelperA(int *x) {22 *x = 1;23#ifndef SUPPRESSED24 // expected-warning@-2 {{Dereference of null pointer}}25#endif26}27 28void testNilReceiverHelperB(int *x) {29 *x = 1;30#if !defined(SUPPRESSED)31 // expected-warning@-2 {{Dereference of null pointer}}32#endif33}34 35void testNilReceiver(int coin) {36 id x = getNil();37 if (coin)38 testNilReceiverHelperA([x getPtr]);39 else40 testNilReceiverHelperB([[x getObject] getPtr]);41}42 43// FALSE NEGATIVES (over-suppression)44 45__attribute__((objc_root_class))46@interface SomeClass {47 int ivar;48}49-(int *)methodReturningNull;50 51@property(readonly) int *propertyReturningNull;52 53@property(readonly) int *synthesizedProperty;54 55@property(readonly) SomeClass *propertyReturningNil;56 57@end58 59@interface SubOfSomeClass : SomeClass60@end61 62@implementation SubOfSomeClass63@end64 65@implementation SomeClass66-(int *)methodReturningNull {67 return 0;68}69 70-(int *)propertyReturningNull {71 return 0;72}73 74-(SomeClass *)propertyReturningNil {75 return 0;76}77 78+(int *)classPropertyReturningNull {79 return 0;80}81@end82 83void testMethodReturningNull(SomeClass *sc) {84 int *result = [sc methodReturningNull];85 *result = 1;86#ifndef SUPPRESSED87 // expected-warning@-2 {{Dereference of null pointer}}88#endif89}90 91void testPropertyReturningNull(SomeClass *sc) {92 int *result = sc.propertyReturningNull;93 *result = 1;94#ifndef SUPPRESSED95 // expected-warning@-2 {{Dereference of null pointer}}96#endif97}98 99@implementation SubOfSomeClass (ForTestOfSuperProperty)100-(void)testSuperPropertyReturningNull {101 int *result = super.propertyReturningNull;102 *result = 1;103#ifndef SUPPRESSED104 // expected-warning@-2 {{Dereference of null pointer}}105#endif106}107@end108 109void testClassPropertyReturningNull(void) {110 int *result = SomeClass.classPropertyReturningNull;111 *result = 1;112#ifndef SUPPRESSED113 // expected-warning@-2 {{Dereference of null pointer}}114#endif115}116 117@implementation SomeClass (ForTestOfPropertyReturningNil)118void testPropertyReturningNil(SomeClass *sc) {119 SomeClass *result = sc.propertyReturningNil;120 result->ivar = 1;121#ifndef SUPPRESSED122 // expected-warning@-2 {{Access to instance variable 'ivar' results in a dereference of a null pointer (loaded from variable 'result')}}123#endif124}125@end126 127void testSynthesizedPropertyReturningNull(SomeClass *sc) {128 if (sc.synthesizedProperty)129 return;130 131 int *result = sc.synthesizedProperty;132 *result = 1;133#ifndef SUPPRESSED134 // expected-warning@-2 {{Dereference of null pointer}}135#endif136}137