aboutsummaryrefslogtreecommitdiff
path: root/test/Analysis/inlining/inline-defensive-checks.m
blob: 0404ee6df81322bea513eac115460ec6d1696050 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-config suppress-inlined-defensive-checks=true -verify %s

typedef signed char BOOL;
typedef struct objc_class *Class;
typedef struct objc_object {
  Class isa;
} *id;
@protocol NSObject  - (BOOL)isEqual:(id)object; @end
@interface NSObject <NSObject> {}
+(id)alloc;
+(id)new;
-(id)init;
-(id)autorelease;
-(id)copy;
- (Class)class;
-(id)retain;
@end

// Check that inline defensive checks is triggered for null expressions
// within CompoundLiteralExpr.
typedef union {
  struct dispatch_object_s *_do;
  struct dispatch_source_s *_ds;
} dispatch_object_t __attribute__((__transparent_union__));
typedef struct dispatch_source_s *dispatch_source_t;

extern __attribute__((visibility("default"))) __attribute__((__nonnull__)) __attribute__((__nothrow__))
void
dispatch_resume(dispatch_object_t object);

@interface AppDelegate : NSObject {
@protected
	dispatch_source_t p;
}
@end
@implementation AppDelegate
- (void)updateDeleteTimer {
	if (p != ((void*)0))
		;
}
- (void)createAndStartDeleteTimer {
  [self updateDeleteTimer];
  dispatch_resume(p); // no warning
}
@end

// Test nil receiver suppression.
// We only suppress on nil receiver if the nil value is directly causing the bug.
@interface Foo {
@public
  int x;
}
- (Foo *)getFooPtr;
@end

Foo *retNil() {
  return 0;
}

Foo *retInputOrNil(Foo *p) {
  if (p)
    return p;
  return 0;
}

void idc(Foo *p) {
  if (p)
    ;
}

int testNilReceiver(Foo* fPtr) {
  if (fPtr)
    ;
  // On a path where fPtr is nil, mem should be nil.
  Foo *mem = [fPtr getFooPtr];
  return mem->x; // expected-warning {{Access to instance variable 'x' results in a dereference of a null pointer}}
}

int suppressNilReceiverRetNullCond(Foo* fPtr) {
  unsigned zero = 0;
  fPtr = retInputOrNil(fPtr);
  // On a path where fPtr is nzil, mem should be nil.
  Foo *mem = [fPtr getFooPtr];
  return mem->x;
}

int suppressNilReceiverRetNullCondCast(id fPtr) {
  unsigned zero = 0;
  fPtr = retInputOrNil(fPtr);
  // On a path where fPtr is nzil, mem should be nil.
  Foo *mem = ((id)([(Foo*)(fPtr) getFooPtr]));
  return mem->x;
}

int dontSuppressNilReceiverRetNullCond(Foo* fPtr) {
  unsigned zero = 0;
  fPtr = retInputOrNil(fPtr);
  // On a path where fPtr is nil, mem should be nil.
  // The warning is not suppressed because the receiver being nil is not
  // directly related to the value that triggers the warning.
  Foo *mem = [fPtr getFooPtr];
  if (!mem)
    return 5/zero; // expected-warning {{Division by zero}}
  return 0;
}

int dontSuppressNilReceiverRetNull(Foo* fPtr) {
  unsigned zero = 0;
  fPtr = retNil();
  // On a path where fPtr is nil, mem should be nil.
  // The warning is not suppressed because the receiver being nil is not
  // directly related to the value that triggers the warning.
  Foo *mem = [fPtr getFooPtr];
  if (!mem)
    return 5/zero; // expected-warning {{Division by zero}}
  return 0;
}

int dontSuppressNilReceiverIDC(Foo* fPtr) {
  unsigned zero = 0;
  idc(fPtr);
  // On a path where fPtr is nil, mem should be nil.
  // The warning is not suppressed because the receiver being nil is not
  // directly related to the value that triggers the warning.
  Foo *mem = [fPtr getFooPtr];
  if (!mem)
    return 5/zero; // expected-warning {{Division by zero}}
  return 0;
}