diff options
author | Artem Dergachev <artem.dergachev@gmail.com> | 2017-10-05 08:43:32 +0000 |
---|---|---|
committer | Artem Dergachev <artem.dergachev@gmail.com> | 2017-10-05 08:43:32 +0000 |
commit | 7728f3542e45915878ff7f98d7dd14bca4fe933e (patch) | |
tree | 5355772ee91e6ae30d8073344496e2bac580fee5 /test/Analysis/objc-boxing.m | |
parent | e2787a6b9332ec828889a6aa1de4d7738524110a (diff) | |
download | clang-7728f3542e45915878ff7f98d7dd14bca4fe933e.tar.gz |
[analyzer] Fix leak false positives on stuff put in C++/ObjC initializer lists.
The analyzer now realizes that C++ std::initializer_list objects and
Objective-C boxed structure/array/dictionary expressions can potentially
maintain a reference to the objects that were put into them. This avoids
false memory leak posivites and a few other issues.
This is a conservative behavior; for now, we do not model what actually happens
to the objects after being passed into such initializer lists.
rdar://problem/32918288
Differential Revision: https://reviews.llvm.org/D35216
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@314975 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/Analysis/objc-boxing.m')
-rw-r--r-- | test/Analysis/objc-boxing.m | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/test/Analysis/objc-boxing.m b/test/Analysis/objc-boxing.m index 963374b3ef..66f24ddf77 100644 --- a/test/Analysis/objc-boxing.m +++ b/test/Analysis/objc-boxing.m @@ -5,6 +5,16 @@ void clang_analyzer_eval(int); typedef signed char BOOL; typedef long NSInteger; typedef unsigned long NSUInteger; + +@protocol NSObject +@end +@interface NSObject <NSObject> {} +@end +@protocol NSCopying +@end +@protocol NSCoding +@end + @interface NSString @end @interface NSString (NSStringExtensionMethods) + (id)stringWithUTF8String:(const char *)nullTerminatedCString; @@ -28,7 +38,15 @@ typedef unsigned long NSUInteger; + (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value ; @end +@interface NSValue : NSObject <NSCopying, NSCoding> +- (void)getValue:(void *)value; ++ (NSValue *)valueWithBytes:(const void *)value + objCType:(const char *)type; +@end +typedef typeof(sizeof(int)) size_t; +extern void *malloc(size_t); +extern void free(void *); extern char *strdup(const char *str); id constant_string() { @@ -39,6 +57,23 @@ id dynamic_string() { return @(strdup("boxed dynamic string")); // expected-warning{{Potential memory leak}} } +typedef struct __attribute__((objc_boxable)) { + const char *str; +} BoxableStruct; + +id leak_within_boxed_struct() { + BoxableStruct bs; + bs.str = strdup("dynamic string"); // The duped string shall be owned by val. + NSValue *val = @(bs); // no-warning + return val; +} + +id leak_of_boxed_struct() { + BoxableStruct *bs = malloc(sizeof(BoxableStruct)); // The pointer stored in bs isn't owned by val. + NSValue *val = @(*bs); // expected-warning{{Potential leak of memory pointed to by 'bs'}} + return val; +} + id const_char_pointer(int *x) { if (x) return @(3); |