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

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 19 13:07:04 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

<details>
<summary>Changes</summary>

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.

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


1 Files Affected:

- (modified) clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp (+7-6) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp b/clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp
index 4dd4a95f880aca0..5ffa4a92f574e0b 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;
   }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/66810


More information about the cfe-commits mailing list