-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[alpha.webkit.RetainPtrCtorAdoptChecker] Don't treat assignment to an +1 out argument as a leak #161633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… +1 out argument as a leak Make RetainPtrCtorAdoptChecker recognize an assignment to an +1 out argument so that it won't emit a memory leak warning.
Member
|
@llvm/pr-subscribers-clang-static-analyzer-1 @llvm/pr-subscribers-clang Author: Ryosuke Niwa (rniwa) ChangesMake RetainPtrCtorAdoptChecker recognize an assignment to an +1 out argument so that it won't emit a memory leak warning. Full diff: https://github.com/llvm/llvm-project/pull/161633.diff 2 Files Affected:
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
index e1f9a77f5a5ca..7440352d8482d 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
@@ -355,14 +355,30 @@ class RetainPtrCtorAdoptChecker
void visitBinaryOperator(const BinaryOperator *BO) const {
if (!BO->isAssignmentOp())
return;
- if (!isa<ObjCIvarRefExpr>(BO->getLHS()))
- return;
+ auto *LHS = BO->getLHS();
auto *RHS = BO->getRHS()->IgnoreParenCasts();
- const Expr *Inner = nullptr;
- if (isAllocInit(RHS, &Inner)) {
- CreateOrCopyFnCall.insert(RHS);
- if (Inner)
- CreateOrCopyFnCall.insert(Inner);
+ if (isa<ObjCIvarRefExpr>(LHS)) {
+ const Expr *Inner = nullptr;
+ if (isAllocInit(RHS, &Inner)) {
+ CreateOrCopyFnCall.insert(RHS);
+ if (Inner)
+ CreateOrCopyFnCall.insert(Inner);
+ }
+ return;
+ } else if (auto *UO = dyn_cast<UnaryOperator>(LHS)) {
+ auto OpCode = UO->getOpcode();
+ if (OpCode == UO_Deref) {
+ if (auto *DerefTarget = UO->getSubExpr()) {
+ DerefTarget = DerefTarget->IgnoreParenCasts();
+ auto *DRE = dyn_cast<DeclRefExpr>(DerefTarget);
+ if (auto *Decl = DRE->getDecl()) {
+ if (!isa<ParmVarDecl>(Decl) || !isCreateOrCopy(RHS))
+ return;
+ if (Decl->hasAttr<CFReturnsRetainedAttr>())
+ CreateOrCopyFnCall.insert(RHS);
+ }
+ }
+ }
}
}
diff --git a/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use.mm b/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use.mm
index 769901778cdf0..3b0f19404097b 100644
--- a/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use.mm
+++ b/clang/test/Analysis/Checkers/WebKit/retain-ptr-ctor-adopt-use.mm
@@ -182,6 +182,14 @@ void adopt_retainptr() {
auto bar = adoptNS([allocSomeObj() init]);
}
+CFTypeRef make_cf_obj() CF_RETURNS_RETAINED {
+ return CFArrayCreateMutable(kCFAllocatorDefault, 1);
+}
+
+void get_cf_obj(CFTypeRef* CF_RETURNS_RETAINED result) {
+ *result = CFArrayCreateMutable(kCFAllocatorDefault, 1);
+}
+
RetainPtr<CFArrayRef> return_arg(CFArrayRef arg) {
return arg;
}
|
steakhal
reviewed
Nov 18, 2025
clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
Outdated
Show resolved
Hide resolved
clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
Outdated
Show resolved
Hide resolved
clang/lib/StaticAnalyzer/Checkers/WebKit/RetainPtrCtorAdoptChecker.cpp
Outdated
Show resolved
Hide resolved
steakhal
approved these changes
Dec 9, 2025
Contributor
Author
|
Thanks for the review. |
rniwa
added a commit
to rniwa/llvm-project
that referenced
this pull request
Dec 12, 2025
… +1 out argument as a leak (llvm#161633) Make RetainPtrCtorAdoptChecker recognize an assignment to an +1 out argument so that it won't emit a memory leak warning. (cherry picked from commit 0eb00ef)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Make RetainPtrCtorAdoptChecker recognize an assignment to an +1 out argument so that it won't emit a memory leak warning.