[clang-tools-extra] [clang-tidy] Add recursion protection in ExceptionSpecAnalyzer (PR #66810)
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 24 08:23:46 PST 2024
https://github.com/PiotrZSL updated https://github.com/llvm/llvm-project/pull/66810
>From c808abe4dcba2a523552863bf7a32736e6bbbc37 Mon Sep 17 00:00:00 2001
From: Piotr Zegar <me at piotrzegar.pl>
Date: Tue, 19 Sep 2023 19:05:00 +0000
Subject: [PATCH] [clang-tidy] Add recursion protection in
ExceptionSpecAnalyzer
Normally endless recursion should not happen in ExceptionSpecAnalyzer,
but if AST would be malformed (missing include), this could cause crash.
---
.../clang-tidy/utils/ExceptionSpecAnalyzer.cpp | 13 +++++++------
.../performance/noexcept-move-constructor-crash.cpp | 8 ++++++++
2 files changed, 15 insertions(+), 6 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-crash.cpp
diff --git a/clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp b/clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp
index 4a9426ee7e8bbb..0637d0eff688cd 100644
--- a/clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp
+++ b/clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp
@@ -14,13 +14,14 @@ namespace clang::tidy::utils {
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyze(const FunctionDecl *FuncDecl) {
- // Check if the function has already been analyzed and reuse that result.
- const auto CacheEntry = FunctionCache.find(FuncDecl);
- if (CacheEntry == FunctionCache.end()) {
+ // Check if function exist in cache or add temporary value to cache to protect
+ // against endless recursion.
+ const auto [CacheEntry, NotFound] =
+ FunctionCache.try_emplace(FuncDecl, State::NotThrowing);
+ if (NotFound) {
ExceptionSpecAnalyzer::State State = analyzeImpl(FuncDecl);
-
- // Cache the result of the analysis.
- FunctionCache.try_emplace(FuncDecl, State);
+ // Update result with calculated value
+ FunctionCache[FuncDecl] = State;
return State;
}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-crash.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-crash.cpp
new file mode 100644
index 00000000000000..8cbf1ccbde1505
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor-crash.cpp
@@ -0,0 +1,8 @@
+// RUN: %check_clang_tidy -std=c++17 -expect-clang-tidy-error %s performance-noexcept-move-constructor %t
+
+template <typename value_type> class set {
+ set(set &&) = default;
+ set(initializer_list<value_type> __l) {};
+// CHECK-MESSAGES: :[[@LINE-1]]:7: error: member 'initializer_list' cannot have template arguments [clang-diagnostic-error]
+// CHECK-MESSAGES: :[[@LINE-2]]:36: error: expected ')' [clang-diagnostic-error]
+};
More information about the cfe-commits
mailing list