[clang-tools-extra] a663e78 - [clang-tidy] Add recursion protection in ExceptionSpecAnalyzer (#66810)

via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 13 08:51:31 PST 2025


Author: Piotr Zegar
Date: 2025-02-13T17:51:28+01:00
New Revision: a663e78a6eb6bbd20c0c74b3e6a78e24ee423544

URL: https://github.com/llvm/llvm-project/commit/a663e78a6eb6bbd20c0c74b3e6a78e24ee423544
DIFF: https://github.com/llvm/llvm-project/commit/a663e78a6eb6bbd20c0c74b3e6a78e24ee423544.diff

LOG: [clang-tidy] Add recursion protection in ExceptionSpecAnalyzer (#66810)

Normally endless recursion should not happen in ExceptionSpecAnalyzer,
but if AST would be malformed (missing include), this could cause crash.

I run into this issue when due to missing include constructor argument
were parsed as FieldDecl.
As checking for recursion cost nothing, why not to do this in check just
in case.

Fixes #111436

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp
    clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp b/clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp
index 4a9426ee7e8bb..0637d0eff688c 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.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor.cpp
index 347a1e3220061..4df0927b62af5 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor.cpp
@@ -1,4 +1,6 @@
 // RUN: %check_clang_tidy %s performance-noexcept-move-constructor %t -- -- -fexceptions
+// RUN: %check_clang_tidy -std=c++17 -check-suffixes=,ERR %s performance-noexcept-move-constructor %t \
+// RUN:                   -- --fix-errors -- -fexceptions -DENABLE_ERROR
 
 namespace std
 {
@@ -397,3 +399,18 @@ namespace gh68101
       Container(Container&&) noexcept(std::is_nothrow_move_constructible<T>::value);
   };
 } // namespace gh68101
+
+namespace gh111436
+{
+
+template <typename value_type> class set {
+  set(set &&) = default;
+
+#ifdef ENABLE_ERROR
+  set(initializer_list<value_type> __l) {};
+  // CHECK-MESSAGES-ERR: :[[@LINE-1]]:7: error: member 'initializer_list' cannot have template arguments [clang-diagnostic-error]
+  // CHECK-MESSAGES-ERR: :[[@LINE-2]]:36: error: expected ')' [clang-diagnostic-error]
+#endif
+};
+
+} // namespace gh111436


        


More information about the cfe-commits mailing list