1 // RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-config suppress-null-return-paths=false -verify %s 2 // RUN: %clang_cc1 -analyze -analyzer-checker=core -verify -DSUPPRESSED=1 %s 3 // RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-config avoid-suppressing-null-argument-paths=true -DSUPPRESSED=1 -DNULL_ARGS=1 -verify %s 4 5 #ifdef SUPPRESSED 6 // expected-no-diagnostics 7 #endif 8 9 @interface PointerWrapper 10 - (int *)getPtr; 11 - (id)getObject; 12 @end 13 14 id getNil() { 15 return 0; 16 } 17 18 void testNilReceiverHelperA(int *x) { 19 *x = 1; 20 #ifndef SUPPRESSED 21 // expected-warning@-2 {{Dereference of null pointer}} 22 #endif 23 } 24 25 void testNilReceiverHelperB(int *x) { 26 *x = 1; 27 #ifndef SUPPRESSED 28 // expected-warning@-2 {{Dereference of null pointer}} 29 #endif 30 } 31 32 void testNilReceiver(int coin) { 33 id x = getNil(); 34 if (coin) 35 testNilReceiverHelperA([x getPtr]); 36 else 37 testNilReceiverHelperB([[x getObject] getPtr]); 38 } 39 40 // FALSE NEGATIVES (over-suppression) 41 42 __attribute__((objc_root_class)) 43 @interface SomeClass 44 -(int *)methodReturningNull; 45 46 @property(readonly) int *propertyReturningNull; 47 48 @property(readonly) int *synthesizedProperty; 49 50 @end 51 52 @interface SubOfSomeClass : SomeClass 53 @end 54 55 @implementation SubOfSomeClass 56 @end 57 58 @implementation SomeClass 59 -(int *)methodReturningNull { 60 return 0; 61 } 62 63 -(int *)propertyReturningNull { 64 return 0; 65 } 66 67 +(int *)classPropertyReturningNull { 68 return 0; 69 } 70 @end 71 72 void testMethodReturningNull(SomeClass *sc) { 73 int *result = [sc methodReturningNull]; 74 *result = 1; 75 #ifndef SUPPRESSED 76 // expected-warning@-2 {{Dereference of null pointer}} 77 #endif 78 } 79 80 void testPropertyReturningNull(SomeClass *sc) { 81 int *result = sc.propertyReturningNull; 82 *result = 1; 83 #ifndef SUPPRESSED 84 // expected-warning@-2 {{Dereference of null pointer}} 85 #endif 86 } 87 88 @implementation SubOfSomeClass (ForTestOfSuperProperty) 89 -(void)testSuperPropertyReturningNull { 90 int *result = super.propertyReturningNull; 91 *result = 1; 92 #ifndef SUPPRESSED 93 // expected-warning@-2 {{Dereference of null pointer}} 94 #endif 95 } 96 @end 97 98 void testClassPropertyReturningNull() { 99 int *result = SomeClass.classPropertyReturningNull; 100 *result = 1; 101 #ifndef SUPPRESSED 102 // expected-warning@-2 {{Dereference of null pointer}} 103 #endif 104 } 105 106 void testSynthesizedPropertyReturningNull(SomeClass *sc) { 107 if (sc.synthesizedProperty) 108 return; 109 110 int *result = sc.synthesizedProperty; 111 *result = 1; 112 #ifndef SUPPRESSED 113 // expected-warning@-2 {{Dereference of null pointer}} 114 #endif 115 } 116