autoreleasewritechecker_test.m
10.4 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// UNSUPPORTED: system-windows
// RUN: %clang_analyze_cc1 -DARC -fobjc-arc -analyzer-checker=core,osx.cocoa.AutoreleaseWrite %s -triple x86_64-darwin -fblocks -verify
// RUN: %clang_analyze_cc1 -DNOARC -analyzer-checker=core,osx.cocoa.AutoreleaseWrite %s -fblocks -triple x86_64-darwin -verify
typedef signed char BOOL;
#define YES ((BOOL)1)
@protocol NSObject - (BOOL)isEqual:(id)object; @end
@interface NSObject <NSObject> {}
+(id)alloc;
-(id)init;
-(id)autorelease;
-(id)copy;
-(id)retain;
@end
typedef int NSZone;
typedef int NSCoder;
typedef unsigned long NSUInteger;
@protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end
@protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end
@interface NSError : NSObject <NSCopying, NSCoding> {}
+ (id)errorWithDomain:(int)domain;
@end
typedef int dispatch_semaphore_t;
typedef void (^block_t)();
typedef enum {
NSEnumerationConcurrent = (1UL << 0),
NSEnumerationReverse = (1UL << 1)
} NSEnumerationOptions;
@interface NSArray
- (void)enumerateObjectsUsingBlock:(block_t)block;
@end
@interface NSSet
- (void)objectsPassingTest:(block_t)block;
@end
@interface NSDictionary
- (void)enumerateKeysAndObjectsUsingBlock:(block_t)block;
@end
@interface NSIndexSet
- (void)indexesPassingTest:(block_t)block;
- (NSUInteger)indexWithOptions:(NSEnumerationOptions)opts
passingTest:(BOOL (^)(NSUInteger idx, BOOL *stop))predicate;
@end
typedef int group_t;
typedef struct dispatch_queue_s *dispatch_queue_t;
typedef void (^dispatch_block_t)(void);
extern dispatch_queue_t queue;
void dispatch_group_async(dispatch_queue_t queue,
group_t group,
dispatch_block_t block);
void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
dispatch_semaphore_t dispatch_semaphore_create(int);
void dispatch_semaphore_wait(dispatch_semaphore_t, int);
void dispatch_semaphore_signal(dispatch_semaphore_t);
// No warnings without ARC.
#ifdef NOARC
// expected-no-diagnostics
BOOL writeToErrorWithIterator(NSError ** error, NSArray *a) {
[a enumerateObjectsUsingBlock:^{
*error = [NSError errorWithDomain:1]; // no-warning
}];
return 0;
}
#endif
#ifdef ARC
@interface I : NSObject
- (BOOL) writeToStrongErrorInBlock:(NSError *__strong *)error;
- (BOOL) writeToErrorInBlock:(NSError *__autoreleasing *)error;
- (BOOL) writeToLocalErrorInBlock:(NSError **)error;
- (BOOL) writeToErrorInBlockMultipleTimes:(NSError *__autoreleasing *)error;
- (BOOL) writeToError:(NSError *__autoreleasing *)error;
- (BOOL) writeToErrorWithDispatchGroup:(NSError *__autoreleasing *)error;
@end
@implementation I
- (BOOL) writeToErrorInBlock:(NSError *__autoreleasing *)error {
dispatch_semaphore_t sem = dispatch_semaphore_create(0l);
dispatch_async(queue, ^{
if (error) {
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside autorelease pool that may exit before method returns; consider writing first to a strong local variable declared outside of the block}}
}
dispatch_semaphore_signal(sem);
});
dispatch_semaphore_wait(sem, 100);
return 0;
}
- (BOOL) writeToErrorWithDispatchGroup:(NSError *__autoreleasing *)error {
dispatch_semaphore_t sem = dispatch_semaphore_create(0l);
dispatch_group_async(queue, 0, ^{
if (error) {
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside autorelease pool that may exit before method returns; consider writing first to a strong local variable declared outside of the block}}
}
dispatch_semaphore_signal(sem);
});
dispatch_semaphore_wait(sem, 100);
return 0;
}
- (BOOL) writeToLocalErrorInBlock:(NSError *__autoreleasing *)error {
dispatch_semaphore_t sem = dispatch_semaphore_create(0l);
dispatch_async(queue, ^{
NSError* error2;
NSError*__strong* error3 = &error2;
if (error) {
*error3 = [NSError errorWithDomain:1]; // no-warning
}
dispatch_semaphore_signal(sem);
});
dispatch_semaphore_wait(sem, 100);
return 0;
}
- (BOOL) writeToStrongErrorInBlock:(NSError *__strong *)error {
dispatch_semaphore_t sem = dispatch_semaphore_create(0l);
dispatch_async(queue, ^{
if (error) {
*error = [NSError errorWithDomain:2]; // no-warning
}
dispatch_semaphore_signal(sem);
});
dispatch_semaphore_wait(sem, 100);
return 0;
}
- (BOOL) writeToErrorInBlockMultipleTimes:(NSError *__autoreleasing *)error {
dispatch_semaphore_t sem = dispatch_semaphore_create(0l);
dispatch_async(queue, ^{
if (error) {
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside autorelease pool that may exit before method returns; consider writing first to a strong local variable declared outside of the block}}
}
dispatch_semaphore_signal(sem);
});
dispatch_async(queue, ^{
if (error) {
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside autorelease pool that may exit before method returns; consider writing first to a strong local variable declared outside of the block}}
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside autorelease pool that may exit before method returns; consider writing first to a strong local variable declared outside of the block}}
}
dispatch_semaphore_signal(sem);
});
*error = [NSError errorWithDomain:1]; // no-warning
dispatch_semaphore_wait(sem, 100);
return 0;
}
- (BOOL) writeToError:(NSError *__autoreleasing *)error {
*error = [NSError errorWithDomain:1]; // no-warning
return 0;
}
@end
BOOL writeToErrorInBlockFromCFunc(NSError *__autoreleasing* error) {
dispatch_semaphore_t sem = dispatch_semaphore_create(0l);
dispatch_async(queue, ^{
if (error) {
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside autorelease pool that may exit before function returns; consider writing first to a strong local variable declared outside of the block}}
}
dispatch_semaphore_signal(sem);
});
dispatch_semaphore_wait(sem, 100);
return 0;
}
BOOL writeToErrorNoWarning(NSError *__autoreleasing* error) {
*error = [NSError errorWithDomain:1]; // no-warning
return 0;
}
BOOL writeToErrorWithIterator(NSError *__autoreleasing* error, NSArray *a, NSSet *s, NSDictionary *d, NSIndexSet *i) { [a enumerateObjectsUsingBlock:^{
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside autorelease pool that may exit before function returns; consider writing first to a strong local variable declared outside of the block}}
}];
[d enumerateKeysAndObjectsUsingBlock:^{
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside autorelease pool that may exit before function returns; consider writing first to a strong local variable declared outside of the block}}
}];
[s objectsPassingTest:^{
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside autorelease pool that may exit before function returns; consider writing first to a strong local variable declared outside of the block}}
}];
[i indexesPassingTest:^{
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside autorelease pool that may exit before function returns; consider writing first to a strong local variable declared outside of the block}}
}];
[i indexWithOptions: NSEnumerationReverse passingTest:^(NSUInteger idx, BOOL *stop) {
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside autorelease pool that may exit before function returns; consider writing first to a strong local variable declared outside of the block}}
return YES;
}];
return 0;
}
void writeIntoError(NSError **error) {
*error = [NSError errorWithDomain:1];
}
extern void readError(NSError *error);
void writeToErrorWithIteratorNonnull(NSError *__autoreleasing* _Nonnull error, NSDictionary *a) {
[a enumerateKeysAndObjectsUsingBlock:^{
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter}}
}];
}
void escapeErrorFromIterator(NSError *__autoreleasing* _Nonnull error, NSDictionary *a) {
[a enumerateKeysAndObjectsUsingBlock:^{
writeIntoError(error); // expected-warning{{Capture of autoreleasing out parameter}}
}];
}
void noWarningOnRead(NSError *__autoreleasing* error, NSDictionary *a) {
[a enumerateKeysAndObjectsUsingBlock:^{
NSError* local = *error; // no-warning
}];
}
void noWarningOnEscapeRead(NSError *__autoreleasing* error, NSDictionary *a) {
[a enumerateKeysAndObjectsUsingBlock:^{
readError(*error); // no-warning
}];
}
@interface ErrorCapture
- (void) captureErrorOut:(NSError**) error;
- (void) captureError:(NSError*) error;
@end
void escapeErrorFromIteratorMethod(NSError *__autoreleasing* _Nonnull error,
NSDictionary *a,
ErrorCapture *capturer) {
[a enumerateKeysAndObjectsUsingBlock:^{
[capturer captureErrorOut:error]; // expected-warning{{Capture of autoreleasing out parameter}}
}];
}
void noWarningOnEscapeReadMethod(NSError *__autoreleasing* error,
NSDictionary *a,
ErrorCapture *capturer) {
[a enumerateKeysAndObjectsUsingBlock:^{
[capturer captureError:*error]; // no-warning
}];
}
void multipleErrors(NSError *__autoreleasing* error, NSDictionary *a) {
[a enumerateKeysAndObjectsUsingBlock:^{
writeIntoError(error); // expected-warning{{Capture of autoreleasing out parameter}}
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter}}
writeIntoError(error); // expected-warning{{Capture of autoreleasing out parameter}}
}];
}
typedef void (^errBlock)(NSError *__autoreleasing *error);
extern void expectError(errBlock);
void captureAutoreleasingVarFromBlock(NSDictionary *dict) {
expectError(^(NSError *__autoreleasing *err) {
[dict enumerateKeysAndObjectsUsingBlock:^{
writeIntoError(err); // expected-warning{{Capture of autoreleasing out parameter 'err'}}
}];
});
}
#endif