2018-03-30 09:29:07 +08:00
|
|
|
// UNSUPPORTED: system-windows
|
2018-03-30 06:28:04 +08:00
|
|
|
// 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
|
2018-03-30 04:55:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
typedef signed char BOOL;
|
2018-04-28 06:00:51 +08:00
|
|
|
#define YES ((BOOL)1)
|
2018-03-30 04:55:34 +08:00
|
|
|
@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;
|
2018-04-28 06:00:51 +08:00
|
|
|
typedef unsigned long NSUInteger;
|
|
|
|
|
2018-03-30 04:55:34 +08:00
|
|
|
@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)();
|
|
|
|
|
2018-04-28 06:00:51 +08:00
|
|
|
typedef enum {
|
|
|
|
NSEnumerationConcurrent = (1UL << 0),
|
|
|
|
NSEnumerationReverse = (1UL << 1)
|
|
|
|
} NSEnumerationOptions;
|
|
|
|
|
2018-03-30 04:55:34 +08:00
|
|
|
@interface NSArray
|
2018-04-28 06:00:51 +08:00
|
|
|
- (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;
|
2018-03-30 04:55:34 +08:00
|
|
|
@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;
|
2020-06-03 17:50:45 +08:00
|
|
|
- (BOOL)writeToErrorInAutoreleasePool:(NSError *__autoreleasing *)error;
|
|
|
|
- (BOOL)writeToStrongErrorInAutoreleasePool:(NSError *__strong *)error;
|
|
|
|
- (BOOL)writeToLocalErrorInAutoreleasePool:(NSError *__autoreleasing *)error;
|
|
|
|
- (BOOL)writeToErrorInAutoreleasePoolMultipleTimes:(NSError *__autoreleasing *)error;
|
2018-03-30 04:55:34 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation I
|
|
|
|
|
|
|
|
- (BOOL) writeToErrorInBlock:(NSError *__autoreleasing *)error {
|
|
|
|
dispatch_semaphore_t sem = dispatch_semaphore_create(0l);
|
|
|
|
dispatch_async(queue, ^{
|
|
|
|
if (error) {
|
2018-04-28 06:00:51 +08:00
|
|
|
*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}}
|
2018-03-30 04:55:34 +08:00
|
|
|
}
|
|
|
|
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) {
|
2018-04-28 06:00:51 +08:00
|
|
|
*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}}
|
2018-03-30 04:55:34 +08:00
|
|
|
}
|
|
|
|
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) {
|
2018-04-28 06:00:51 +08:00
|
|
|
*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}}
|
2018-03-30 04:55:34 +08:00
|
|
|
}
|
|
|
|
dispatch_semaphore_signal(sem);
|
|
|
|
});
|
|
|
|
dispatch_async(queue, ^{
|
|
|
|
if (error) {
|
2018-04-28 06:00:51 +08:00
|
|
|
*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}}
|
2018-03-30 04:55:34 +08:00
|
|
|
}
|
|
|
|
dispatch_semaphore_signal(sem);
|
|
|
|
});
|
|
|
|
*error = [NSError errorWithDomain:1]; // no-warning
|
|
|
|
|
|
|
|
dispatch_semaphore_wait(sem, 100);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-03 17:50:45 +08:00
|
|
|
- (BOOL)writeToErrorInAutoreleasePool:(NSError *__autoreleasing *)error {
|
|
|
|
@autoreleasepool {
|
|
|
|
if (error) {
|
|
|
|
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside locally-scoped autorelease pool; consider writing first to a strong local variable declared outside of the autorelease pool}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)writeToStrongErrorInAutoreleasePool:(NSError *__strong *)error {
|
|
|
|
@autoreleasepool {
|
|
|
|
if (error) {
|
|
|
|
*error = [NSError errorWithDomain:1]; // no-warning
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)writeToLocalErrorInAutoreleasePool:(NSError *__autoreleasing *)error {
|
|
|
|
NSError *localError;
|
|
|
|
@autoreleasepool {
|
|
|
|
localError = [NSError errorWithDomain:1]; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
*error = localError; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)writeToErrorInAutoreleasePoolMultipleTimes:(NSError *__autoreleasing *)error {
|
|
|
|
@autoreleasepool {
|
|
|
|
if (error) {
|
|
|
|
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside locally-scoped autorelease pool; consider writing first to a strong local variable declared outside of the autorelease pool}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
*error = [NSError errorWithDomain:1]; // no-warning
|
|
|
|
}
|
|
|
|
@autoreleasepool {
|
|
|
|
if (error) {
|
|
|
|
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside locally-scoped autorelease pool; consider writing first to a strong local variable declared outside of the autorelease pool}}
|
|
|
|
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside locally-scoped autorelease pool; consider writing first to a strong local variable declared outside of the autorelease pool}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-30 04:55:34 +08:00
|
|
|
- (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) {
|
2018-04-28 06:00:51 +08:00
|
|
|
*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}}
|
2018-03-30 04:55:34 +08:00
|
|
|
}
|
|
|
|
dispatch_semaphore_signal(sem);
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch_semaphore_wait(sem, 100);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-03 17:50:45 +08:00
|
|
|
BOOL writeIntoErrorInAutoreleasePoolFromCFunc(NSError *__autoreleasing *error) {
|
|
|
|
@autoreleasepool {
|
|
|
|
if (error) {
|
|
|
|
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter inside locally-scoped autorelease pool; consider writing first to a strong local variable declared outside of the autorelease pool}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-30 04:55:34 +08:00
|
|
|
BOOL writeToErrorNoWarning(NSError *__autoreleasing* error) {
|
|
|
|
*error = [NSError errorWithDomain:1]; // no-warning
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-28 06:00:51 +08:00
|
|
|
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;
|
2018-03-30 04:55:34 +08:00
|
|
|
}];
|
|
|
|
return 0;
|
|
|
|
}
|
2018-05-12 04:39:19 +08:00
|
|
|
|
2018-05-15 05:39:54 +08:00
|
|
|
void writeIntoError(NSError **error) {
|
|
|
|
*error = [NSError errorWithDomain:1];
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void readError(NSError *error);
|
|
|
|
|
2018-05-12 04:39:19 +08:00
|
|
|
void writeToErrorWithIteratorNonnull(NSError *__autoreleasing* _Nonnull error, NSDictionary *a) {
|
|
|
|
[a enumerateKeysAndObjectsUsingBlock:^{
|
|
|
|
*error = [NSError errorWithDomain:1]; // expected-warning{{Write to autoreleasing out parameter}}
|
|
|
|
}];
|
|
|
|
}
|
2018-05-15 05:39:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
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}}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2018-05-17 06:47:05 +08:00
|
|
|
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'}}
|
|
|
|
}];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-30 04:55:34 +08:00
|
|
|
#endif
|
2018-05-12 04:39:19 +08:00
|
|
|
|