[clang] Fix a crash in clang::isGetterOfRefCounted by checking nullptr in tryToFindPtrOrigin (PR #80768)
Ryosuke Niwa via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 5 16:16:25 PST 2024
https://github.com/rniwa updated https://github.com/llvm/llvm-project/pull/80768
>From f9f11843c2d09775de20d47dc71c5e482a1ff8b4 Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Mon, 5 Feb 2024 16:07:09 -0800
Subject: [PATCH] Fix a crash in clang::isGetterOfRefCounted by checking
nullptr in tryToFindPtrOrigin
---
.../Checkers/WebKit/ASTUtils.cpp | 15 ++++++-----
.../WebKit/member-function-pointer-crash.cpp | 26 +++++++++++++++++++
2 files changed, 35 insertions(+), 6 deletions(-)
create mode 100644 clang/test/Analysis/Checkers/WebKit/member-function-pointer-crash.cpp
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
index 64028b2770215..4526fac64735b 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
@@ -34,13 +34,16 @@ tryToFindPtrOrigin(const Expr *E, bool StopAtFirstRefCountedObj) {
}
if (auto *call = dyn_cast<CallExpr>(E)) {
if (auto *memberCall = dyn_cast<CXXMemberCallExpr>(call)) {
- std::optional<bool> IsGetterOfRefCt = isGetterOfRefCounted(memberCall->getMethodDecl());
- if (IsGetterOfRefCt && *IsGetterOfRefCt) {
- E = memberCall->getImplicitObjectArgument();
- if (StopAtFirstRefCountedObj) {
- return {E, true};
+ if (auto *decl = memberCall->getMethodDecl()) {
+ std::optional<bool> IsGetterOfRefCt =
+ isGetterOfRefCounted(memberCall->getMethodDecl());
+ if (IsGetterOfRefCt && *IsGetterOfRefCt) {
+ E = memberCall->getImplicitObjectArgument();
+ if (StopAtFirstRefCountedObj) {
+ return {E, true};
+ }
+ continue;
}
- continue;
}
}
diff --git a/clang/test/Analysis/Checkers/WebKit/member-function-pointer-crash.cpp b/clang/test/Analysis/Checkers/WebKit/member-function-pointer-crash.cpp
new file mode 100644
index 0000000000000..16d3b89b3ac4e
--- /dev/null
+++ b/clang/test/Analysis/Checkers/WebKit/member-function-pointer-crash.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedLocalVarsChecker -verify %s
+
+#include "mock-types.h"
+
+class RenderStyle;
+
+class FillLayer {
+public:
+ void ref() const;
+ void deref() const;
+};
+
+class FillLayersPropertyWrapper {
+public:
+ typedef const FillLayer& (RenderStyle::*LayersGetter)() const;
+
+private:
+ bool canInterpolate(const RenderStyle& from) const
+ {
+ auto* fromLayer = &(from.*m_layersGetter)();
+ // expected-warning at -1{{Local variable 'fromLayer' is uncounted and unsafe}}
+ return true;
+ }
+
+ LayersGetter m_layersGetter;
+};
More information about the cfe-commits
mailing list