Debian Codegraph @

clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use.mm

raw
1 // UNSUPPORTED: target={{.*}}-zos{{.*}}, target={{.*}}-aix{{.*}}
2 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.RetainPtrCtorAdoptChecker -verify %s
3
4 #include "objc-mock-types.h"
5
6 CFTypeRef CFCopyArray(CFArrayRef);
7 void* CreateCopy();
8
9 void basic_correct() {
10 auto ns1 = adoptNS([SomeObj alloc]);
11 auto ns2 = adoptNS([[SomeObj alloc] init]);
12 RetainPtr<SomeObj> ns3 = [ns1.get() next];
13 auto ns4 = adoptNS([ns3 mutableCopy]);
14 auto ns5 = adoptNS([ns3 copyWithValue:3]);
15 auto ns6 = retainPtr([ns3 next]);
16 auto ns7 = retainPtr((SomeObj *)0);
17 auto ns8 = adoptNS(nil);
18 CFMutableArrayRef cf1 = adoptCF(CFArrayCreateMutable(kCFAllocatorDefault, 10));
19 auto cf2 = adoptCF(SecTaskCreateFromSelf(kCFAllocatorDefault));
20 auto cf3 = adoptCF(checked_cf_cast<CFArrayRef>(CFCopyArray(cf1)));
21 CreateCopy();
22 }
23
24 CFMutableArrayRef provide_cf();
25
26 void basic_wrong() {
27 RetainPtr<SomeObj> ns1 = [[SomeObj alloc] init];
28 // expected-warning@-1{{Incorrect use of RetainPtr constructor. The argument is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
29 auto ns2 = adoptNS([ns1.get() next]);
30 // expected-warning@-1{{Incorrect use of adoptNS. The argument is +0 and results in an use-after-free [alpha.webkit.RetainPtrCtorAdoptChecker]}}
31 RetainPtr<CFMutableArrayRef> cf1 = CFArrayCreateMutable(kCFAllocatorDefault, 10);
32 // expected-warning@-1{{Incorrect use of RetainPtr constructor. The argument is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
33 RetainPtr<CFMutableArrayRef> cf2 = adoptCF(provide_cf());
34 // expected-warning@-1{{Incorrect use of adoptCF. The argument is +0 and results in an use-after-free [alpha.webkit.RetainPtrCtorAdoptChecker]}}
35 RetainPtr<CFTypeRef> cf3 = SecTaskCreateFromSelf(kCFAllocatorDefault);
36 // expected-warning@-1{{Incorrect use of RetainPtr constructor. The argument is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
37 CFCopyArray(cf1);
38 // expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
39 }
40
41 void basic_correct_arc() {
42 auto *obj = [[SomeObj alloc] init];
43 // expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
44 [obj doWork];
45 }
46
47 @implementation SomeObj {
48 NSNumber *_number;
49 SomeObj *_next;
50 SomeObj *_other;
51 }
52
53 - (instancetype)_init {
54 self = [super init];
55 _number = nil;
56 _next = nil;
57 _other = nil;
58 return self;
59 }
60
61 - (SomeObj *)mutableCopy {
62 auto *copy = [[SomeObj alloc] init];
63 // expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
64 [copy setValue:_number];
65 [copy setNext:_next];
66 [copy setOther:_other];
67 return copy;
68 }
69
70 - (SomeObj *)copyWithValue:(int)value {
71 auto *copy = [[SomeObj alloc] init];
72 // expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
73 [copy setValue:_number];
74 [copy setNext:_next];
75 [copy setOther:_other];
76 return copy;
77 }
78
79 - (void)doWork {
80 _number = [[NSNumber alloc] initWithInt:5];
81 }
82
83 - (SomeObj *)other {
84 return _other;
85 }
86
87 - (void)setOther:(SomeObj *)obj {
88 _other = obj;
89 }
90
91 - (SomeObj *)next {
92 return _next;
93 }
94
95 - (void)setNext:(SomeObj *)obj {
96 _next = obj;
97 }
98
99 - (int)value {
100 return [_number intValue];
101 }
102
103 - (void)setValue:value {
104 _number = value;
105 }
106
107 - (id)copyWithZone:(NSZone *)zone {
108 auto copy = adoptNS([(SomeObj *)[SomeObj allocWithZone:zone] init]);
109 [copy setValue:_number];
110 [copy setNext:_next];
111 [copy setOther:_other];
112 return copy.leakRef();
113 }
114
115 @end;
116
117 RetainPtr<CVPixelBufferRef> cf_out_argument() {
118 auto surface = adoptCF(IOSurfaceCreate(nullptr));
119 CVPixelBufferRef rawBuffer = nullptr;
120 auto status = CVPixelBufferCreateWithIOSurface(kCFAllocatorDefault, surface.get(), nullptr, &rawBuffer);
121 return adoptCF(rawBuffer);
122 }
123
124 RetainPtr<SomeObj> return_nil() {
125 return nil;
126 }
127
128 RetainPtr<SomeObj> return_nullptr() {
129 return nullptr;
130 }
131
132 RetainPtr<SomeObj> return_retainptr() {
133 RetainPtr<SomeObj> foo;
134 return foo;
135 }
136
137 CFTypeRef CopyValueForSomething();
138
139 void cast_retainptr() {
140 RetainPtr<NSObject> foo;
141 RetainPtr<SomeObj> bar = static_cast<SomeObj*>(foo);
142
143 auto baz = adoptCF(CopyValueForSomething()).get();
144 RetainPtr<CFArrayRef> v = static_cast<CFArrayRef>(baz);
145 }
146
147 CFTypeRef CopyWrapper() {
148 return CopyValueForSomething();
149 }
150
151 CFTypeRef LeakWrapper() {
152 return CopyValueForSomething();
153 // expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
154 }
155
156 NSArray *makeArray() NS_RETURNS_RETAINED {
157 return (__bridge NSArray *)CFArrayCreateMutable(kCFAllocatorDefault, 10);
158 }
159
160 extern Class (*getNSArrayClass)();
161 NSArray *allocArrayInstance() NS_RETURNS_RETAINED {
162 return adoptNS([[getNSArrayClass() alloc] init]).leakRef();
163 }
164
165 extern int (*GetObj)(CF_RETURNS_RETAINED CFTypeRef* objOut);
166 RetainPtr<CFTypeRef> getObject() {
167 CFTypeRef obj = nullptr;
168 if (GetObj(&obj))
169 return nullptr;
170 return adoptCF(obj);
171 }
172
173 CFArrayRef CreateSingleArray(CFStringRef);
174 CFArrayRef CreateSingleArray(CFDictionaryRef);
175 CFArrayRef CreateSingleArray(CFArrayRef);
176 template <typename ElementType>
177 static RetainPtr<CFArrayRef> makeArrayWithSingleEntry(ElementType arg) {
178 return adoptCF(CreateSingleArray(arg));
179 }
180
181 void callMakeArayWithSingleEntry() {
182 auto dictionary = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, nullptr, nullptr, 0));
183 makeArrayWithSingleEntry(dictionary.get());
184 }
185
186 SomeObj* allocSomeObj() CF_RETURNS_RETAINED;
187
188 void adopt_retainptr() {
189 RetainPtr<NSObject> foo = adoptNS([[SomeObj alloc] init]);
190 auto bar = adoptNS([allocSomeObj() init]);
191 }
192
193 CFTypeRef make_cf_obj() CF_RETURNS_RETAINED {
194 return CFArrayCreateMutable(kCFAllocatorDefault, 1);
195 }
196
197 void get_cf_obj(CFTypeRef* CF_RETURNS_RETAINED result) {
198 *result = CFArrayCreateMutable(kCFAllocatorDefault, 1);
199 }
200
201 RetainPtr<CFArrayRef> return_arg(CFArrayRef arg) {
202 return arg;
203 }
204
205 class MemberInit {
206 public:
207 MemberInit(RetainPtr<CFMutableArrayRef>&& array, NSString *str, CFRunLoopRef runLoop)
208 : m_array(array)
209 , m_str(str)
210 , m_runLoop(runLoop)
211 { }
212
213 private:
214 RetainPtr<CFMutableArrayRef> m_array;
215 RetainPtr<NSString> m_str;
216 RetainPtr<CFRunLoopRef> m_runLoop;
217 };
218 void create_member_init() {
219 MemberInit init { adoptCF(CFArrayCreateMutable(kCFAllocatorDefault, 10)), @"hello", CFRunLoopGetCurrent() };
220 }
221
222 RetainPtr<CFStringRef> cfstr() {
223 return CFSTR("");
224 }
225
226 template <typename CF, typename NS>
227 static RetainPtr<NS> bridge_cast(RetainPtr<CF>&& ptr)
228 {
229 return adoptNS((__bridge NSArray *)(ptr.leakRef()));
230 }
231
232 RetainPtr<CFArrayRef> create_cf_array();
233 RetainPtr<id> return_bridge_cast() {
234 return bridge_cast<CFArrayRef, NSArray>(create_cf_array());
235 }
236
237 void mutable_copy_dictionary() {
238 RetainPtr<NSMutableDictionary> mutableDictionary = adoptNS(@{
239 @"Content-Type": @"text/html",
240 }.mutableCopy);
241 }
242
243 void mutable_copy_array() {
244 RetainPtr<NSMutableArray> mutableArray = adoptNS(@[
245 @"foo",
246 ].mutableCopy);
247 }
248
249 void string_copy(NSString *str) {
250 RetainPtr<NSString> copy = adoptNS(str.copy);
251 }
252
253 void alloc_init_spi() {
254 auto ptr = adoptNS([[SomeObj alloc] _init]);
255 }
256
257 void alloc_init_c_function() {
258 RetainPtr ptr = adoptNS([allocSomeObj() init]);
259 }
260
261 void alloc_init_autorelease() {
262 [[[SomeObj alloc] init] autorelease];
263 }
264
265 CFArrayRef make_array() CF_RETURNS_RETAINED;
266
267 RetainPtr<CFArrayRef> adopt_make_array() {
268 return adoptCF(make_array());
269 }
270
271 @interface SomeObject : NSObject
272 -(void)basic_correct;
273 -(void)basic_wrong;
274 -(NSString *)leak_string;
275 -(NSString *)make_string NS_RETURNS_RETAINED;
276 @property (nonatomic, readonly) SomeObj *obj;
277 @end
278
279 @implementation SomeObject
280 -(void)basic_correct {
281 auto ns1 = adoptNS([SomeObj alloc]);
282 auto ns2 = adoptNS([[SomeObj alloc] init]);
283 RetainPtr<SomeObj> ns3 = [ns1.get() next];
284 auto ns4 = adoptNS([ns3 mutableCopy]);
285 auto ns5 = adoptNS([ns3 copyWithValue:3]);
286 auto ns6 = retainPtr([ns3 next]);
287 CFMutableArrayRef cf1 = adoptCF(CFArrayCreateMutable(kCFAllocatorDefault, 10));
288 auto cf2 = adoptCF(SecTaskCreateFromSelf(kCFAllocatorDefault));
289 auto cf3 = adoptCF(checked_cf_cast<CFArrayRef>(CFCopyArray(cf1)));
290 }
291
292 -(void)basic_wrong {
293 RetainPtr<SomeObj> ns1 = [[SomeObj alloc] init];
294 // expected-warning@-1{{Incorrect use of RetainPtr constructor. The argument is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
295 auto ns2 = adoptNS([ns1.get() next]);
296 // expected-warning@-1{{Incorrect use of adoptNS. The argument is +0 and results in an use-after-free [alpha.webkit.RetainPtrCtorAdoptChecker]}}
297 RetainPtr<CFMutableArrayRef> cf1 = CFArrayCreateMutable(kCFAllocatorDefault, 10);
298 // expected-warning@-1{{Incorrect use of RetainPtr constructor. The argument is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
299 RetainPtr<CFMutableArrayRef> cf2 = adoptCF(provide_cf());
300 // expected-warning@-1{{Incorrect use of adoptCF. The argument is +0 and results in an use-after-free [alpha.webkit.RetainPtrCtorAdoptChecker]}}
301 RetainPtr<CFTypeRef> cf3 = SecTaskCreateFromSelf(kCFAllocatorDefault);
302 // expected-warning@-1{{Incorrect use of RetainPtr constructor. The argument is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
303 CFCopyArray(cf1);
304 // expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
305 }
306
307 -(NSString *)leak_string {
308 return [[NSString alloc] initWithUTF8String:"hello"];
309 // expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
310 }
311
312 -(NSString *)make_string {
313 return adoptNS([[NSString alloc] initWithUTF8String:"hello"]).leakRef();
314 }
315
316 -(void)local_leak_string {
317 if ([[NSString alloc] initWithUTF8String:"hello"]) {
318 // expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
319 }
320 }
321
322 -(void)make_some_obj {
323 auto some_obj = adoptNS([allocSomeObj() init]);
324 }
325
326 -(void)alloc_init_bad_order {
327 auto *obj = [NSObject alloc];
328 // expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
329 auto ptr = adoptNS([obj init]);
330 // expected-warning@-1{{Incorrect use of adoptNS. The argument is +0 and results in an use-after-free [alpha.webkit.RetainPtrCtorAdoptChecker]}}
331 }
332
333 -(void)alloc_init_good_order {
334 auto obj = adoptNS([NSObject alloc]);
335 (void)[obj init];
336 }
337
338 -(void)copy_assign_ivar {
339 _obj = [allocSomeObj() init];
340 }
341
342 -(void)do_more_work:(OtherObj *)otherObj {
343 [otherObj doMoreWork:[[OtherObj alloc] init]];
344 // expected-warning@-1{{The return value is +1 and results in a memory leak [alpha.webkit.RetainPtrCtorAdoptChecker]}}
345 }
346 @end