[clang] Fix an implicit cast to a base ref counted class generates a false positive. (PR #80934)
Ryosuke Niwa via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 9 15:27:59 PST 2024
https://github.com/rniwa updated https://github.com/llvm/llvm-project/pull/80934
>From 9154815c48578df9ee384a9707dd79ee64259cea Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Tue, 6 Feb 2024 20:10:33 -0800
Subject: [PATCH 1/2] [alpha.webkit.UncountedCallArgsChecker] Fix an implicit
cast to a base ref counted class generates a false positive.
The bug was caused by isRefCountable erroneously returning false for a class
with both ref() and deref() functions defined because we were not resetting
the base paths results between looking for "ref()" and "deref()"
---
.../Checkers/WebKit/PtrTypesSemantics.cpp | 1 +
...to-base-class-with-deref-in-superclass.cpp | 51 +++++++++++++++++++
2 files changed, 52 insertions(+)
create mode 100644 clang/test/Analysis/Checkers/WebKit/implicit-cast-to-base-class-with-deref-in-superclass.cpp
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
index d2b66341058000..0fd8afedc0b0f5 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
@@ -84,6 +84,7 @@ std::optional<bool> isRefCountable(const CXXRecordDecl* R)
if (AnyInconclusiveBase)
return std::nullopt;
+ Paths.clear();
const auto hasPublicDerefInBase =
[&AnyInconclusiveBase](const CXXBaseSpecifier *Base, CXXBasePath &) {
auto hasDerefInBase = clang::hasPublicMethodInBase(Base, "deref");
diff --git a/clang/test/Analysis/Checkers/WebKit/implicit-cast-to-base-class-with-deref-in-superclass.cpp b/clang/test/Analysis/Checkers/WebKit/implicit-cast-to-base-class-with-deref-in-superclass.cpp
new file mode 100644
index 00000000000000..49826c98a4610d
--- /dev/null
+++ b/clang/test/Analysis/Checkers/WebKit/implicit-cast-to-base-class-with-deref-in-superclass.cpp
@@ -0,0 +1,51 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
+// expected-no-diagnostics
+
+template<typename T>
+class Ref {
+public:
+ ~Ref()
+ {
+ if (auto* ptr = m_ptr)
+ ptr->deref();
+ m_ptr = nullptr;
+ }
+
+ Ref(T& object)
+ : m_ptr(&object)
+ {
+ object.ref();
+ }
+
+ operator T&() const { return *m_ptr; }
+ bool operator!() const { return !*m_ptr; }
+
+private:
+ T* m_ptr;
+};
+
+class Base {
+public:
+ virtual ~Base();
+ void ref() const;
+ void deref() const;
+};
+
+class Event : public Base {
+protected:
+ explicit Event();
+};
+
+class SubEvent : public Event {
+public:
+ static Ref<SubEvent> create();
+private:
+ SubEvent() = default;
+};
+
+void someFunction(Base&);
+
+static void test()
+{
+ someFunction(SubEvent::create());
+}
>From c4e5d9c747b021201c5cdb7d3073f6ec358418e5 Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Fri, 9 Feb 2024 15:27:18 -0800
Subject: [PATCH 2/2] Use mock-types.h instead of embedding the defintion of
Ref in the test.
---
...to-base-class-with-deref-in-superclass.cpp | 23 +------------------
1 file changed, 1 insertion(+), 22 deletions(-)
diff --git a/clang/test/Analysis/Checkers/WebKit/implicit-cast-to-base-class-with-deref-in-superclass.cpp b/clang/test/Analysis/Checkers/WebKit/implicit-cast-to-base-class-with-deref-in-superclass.cpp
index 49826c98a4610d..176238f31bd2e4 100644
--- a/clang/test/Analysis/Checkers/WebKit/implicit-cast-to-base-class-with-deref-in-superclass.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/implicit-cast-to-base-class-with-deref-in-superclass.cpp
@@ -1,28 +1,7 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
// expected-no-diagnostics
-template<typename T>
-class Ref {
-public:
- ~Ref()
- {
- if (auto* ptr = m_ptr)
- ptr->deref();
- m_ptr = nullptr;
- }
-
- Ref(T& object)
- : m_ptr(&object)
- {
- object.ref();
- }
-
- operator T&() const { return *m_ptr; }
- bool operator!() const { return !*m_ptr; }
-
-private:
- T* m_ptr;
-};
+#include "mock-types.h"
class Base {
public:
More information about the cfe-commits
mailing list