Skip to content

Commit b6c6ab3

Browse files
author
George Karpenkov
committed
[analyzer] [RetainCountChecker] Fix object type for CF/Obj-C bridged casts
Having an incorrect type for a cast causes the checker to incorrectly dismiss the operation under ARC, leading to a false positive use-after-release on the test. rdar://47709885 Differential Revision: https://reviews.llvm.org/D57557 llvm-svn: 352824
1 parent 6fa43f8 commit b6c6ab3

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountChecker.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,16 @@ void RetainCountChecker::checkPostStmt(const CastExpr *CE,
185185
if (!BE)
186186
return;
187187

188-
ArgEffect AE = ArgEffect(IncRef, ObjKind::ObjC);
188+
QualType QT = CE->getType();
189+
ObjKind K;
190+
if (coreFoundation::isCFObjectRef(QT)) {
191+
K = ObjKind::CF;
192+
} else {
193+
assert(cocoa::isCocoaObjectRef(QT));
194+
K = ObjKind::ObjC;
195+
}
196+
197+
ArgEffect AE = ArgEffect(IncRef, K);
189198

190199
switch (BE->getBridgeKind()) {
191200
case OBC_Bridge:

clang/test/Analysis/objc-arc.m

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void rdar9424882() {
123123
typedef const void *CFTypeRef;
124124
typedef const struct __CFString *CFStringRef;
125125

126-
@interface NSString
126+
@interface NSString : NSObject
127127
- (id) self;
128128
@end
129129

@@ -231,3 +231,16 @@ id rdar14061675() {
231231
return result;
232232
}
233233

234+
typedef const void * CFTypeRef;
235+
typedef const struct __CFString * CFStringRef;
236+
typedef const struct __CFAllocator * CFAllocatorRef;
237+
extern const CFAllocatorRef kCFAllocatorDefault;
238+
239+
extern CFTypeRef CFRetain(CFTypeRef cf);
240+
extern void CFRelease(CFTypeRef cf);
241+
242+
void check_bridge_retained_cast() {
243+
NSString *nsStr = [[NSString alloc] init];
244+
CFStringRef cfStr = (__bridge_retained CFStringRef)nsStr;
245+
CFRelease(cfStr); // no-warning
246+
}

0 commit comments

Comments
 (0)