Skip to content

Commit 0eb00ef

Browse files
authored
[alpha.webkit.RetainPtrCtorAdoptChecker] Don't treat assignment to an +1 out argument as a leak (#161633)
Make RetainPtrCtorAdoptChecker recognize an assignment to an +1 out argument so that it won't emit a memory leak warning.
1 parent b3d05e6 commit 0eb00ef

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,15 +355,37 @@ class RetainPtrCtorAdoptChecker
355355
void visitBinaryOperator(const BinaryOperator *BO) const {
356356
if (!BO->isAssignmentOp())
357357
return;
358-
if (!isa<ObjCIvarRefExpr>(BO->getLHS()))
359-
return;
358+
auto *LHS = BO->getLHS();
360359
auto *RHS = BO->getRHS()->IgnoreParenCasts();
361-
const Expr *Inner = nullptr;
362-
if (isAllocInit(RHS, &Inner)) {
363-
CreateOrCopyFnCall.insert(RHS);
364-
if (Inner)
365-
CreateOrCopyFnCall.insert(Inner);
360+
if (isa<ObjCIvarRefExpr>(LHS)) {
361+
const Expr *Inner = nullptr;
362+
if (isAllocInit(RHS, &Inner)) {
363+
CreateOrCopyFnCall.insert(RHS);
364+
if (Inner)
365+
CreateOrCopyFnCall.insert(Inner);
366+
}
367+
return;
366368
}
369+
auto *UO = dyn_cast<UnaryOperator>(LHS);
370+
if (!UO)
371+
return;
372+
auto OpCode = UO->getOpcode();
373+
if (OpCode != UO_Deref)
374+
return;
375+
auto *DerefTarget = UO->getSubExpr();
376+
if (!DerefTarget)
377+
return;
378+
DerefTarget = DerefTarget->IgnoreParenCasts();
379+
auto *DRE = dyn_cast<DeclRefExpr>(DerefTarget);
380+
if (!DRE)
381+
return;
382+
auto *Decl = DRE->getDecl();
383+
if (!Decl)
384+
return;
385+
if (!isa<ParmVarDecl>(Decl) || !isCreateOrCopy(RHS))
386+
return;
387+
if (Decl->hasAttr<CFReturnsRetainedAttr>())
388+
CreateOrCopyFnCall.insert(RHS);
367389
}
368390

369391
void visitReturnStmt(const ReturnStmt *RS, const Decl *DeclWithIssue) const {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ void adopt_retainptr() {
190190
auto bar = adoptNS([allocSomeObj() init]);
191191
}
192192

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+
193201
RetainPtr<CFArrayRef> return_arg(CFArrayRef arg) {
194202
return arg;
195203
}

0 commit comments

Comments
 (0)