Skip to content

[alpha.webkit.UncountedCallArgsChecker] Treat an explicit construction of Ref from a Ref return value safe. #130911

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
merged 2 commits into from
Mar 13, 2025

Conversation

rniwa
Copy link
Contributor

@rniwa rniwa commented Mar 12, 2025

Fix a bug that an explicit construction of Ref out of a Ref return value would not be treated as safe. It is definitely safe albit redundant.

…n of Ref from a Ref return value safe.

Fix a bug that an explicit construction of Ref out of a Ref return value would not be treated as safe.
It is definitely safe albit redundant.
@rniwa rniwa requested a review from t-rasmud March 12, 2025 07:13
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Mar 12, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 12, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Ryosuke Niwa (rniwa)

Changes

Fix a bug that an explicit construction of Ref out of a Ref return value would not be treated as safe. It is definitely safe albit redundant.


Full diff: https://github.com/llvm/llvm-project/pull/130911.diff

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp (+2)
  • (modified) clang/test/Analysis/Checkers/WebKit/call-args.cpp (+7)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
index 5e67cb29d08e4..1d9e8a468e899 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
@@ -70,6 +70,8 @@ bool tryToFindPtrOrigin(
           if (isCtorOfSafePtr(ConversionFunc))
             return callback(E, true);
         }
+        if (isa<CXXFunctionalCastExpr>(E) && isSafePtrType(cast->getType()))
+          return callback(E, true);
       }
       // FIXME: This can give false "origin" that would lead to false negatives
       // in checkers. See https://reviews.llvm.org/D37023 for reference.
diff --git a/clang/test/Analysis/Checkers/WebKit/call-args.cpp b/clang/test/Analysis/Checkers/WebKit/call-args.cpp
index e7afd9798da3e..0d53df6a2052f 100644
--- a/clang/test/Analysis/Checkers/WebKit/call-args.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/call-args.cpp
@@ -407,6 +407,13 @@ namespace call_with_explicit_temporary_obj {
   void baz() {
     bar<int>();
   }
+
+  class Foo {
+    Ref<RefCountable> ensure();
+    void foo() {
+      Ref { ensure() }->method();
+    }
+  };
 }
 
 namespace call_with_explicit_construct {

@@ -70,6 +70,8 @@ bool tryToFindPtrOrigin(
if (isCtorOfSafePtr(ConversionFunc))
return callback(E, true);
}
if (isa<CXXFunctionalCastExpr>(E) && isSafePtrType(cast->getType()))
return callback(E, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this case already handled for parameters and locals? Like are there instances of a constructing a Ref from another Ref parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We typically don't pass arguments as RefPtr / Ref except a few cases where we pass in as RefPtr&& / Ref&&. I guess we can add a test for it though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

Copy link
Contributor

@t-rasmud t-rasmud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@rniwa rniwa merged commit 27d8318 into llvm:main Mar 13, 2025
9 of 10 checks passed
@rniwa rniwa deleted the fix-explicit-ref-from-ref-return-value branch March 13, 2025 01:33
rniwa added a commit to rniwa/llvm-project that referenced this pull request Mar 13, 2025
…n of Ref from a Ref return value safe. (llvm#130911)

Fix a bug that an explicit construction of Ref out of a Ref return value
would not be treated as safe. It is definitely safe albit redundant.
frederik-h pushed a commit to frederik-h/llvm-project that referenced this pull request Mar 18, 2025
…n of Ref from a Ref return value safe. (llvm#130911)

Fix a bug that an explicit construction of Ref out of a Ref return value
would not be treated as safe. It is definitely safe albit redundant.
rniwa added a commit to rniwa/llvm-project that referenced this pull request Apr 22, 2025
…n of Ref from a Ref return value safe. (llvm#130911)

Fix a bug that an explicit construction of Ref out of a Ref return value
would not be treated as safe. It is definitely safe albit redundant.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants