[clang] [clang][ASTImporter] Fix possible crash "given incorrect InsertPos for specialization". (PR #89887)

via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 24 00:56:55 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Balázs Kéri (balazske)

<details>
<summary>Changes</summary>

In some situations a new `VarTemplateSpecializationDecl` (for the same template) can be added during import of another one. The "insert position" that is used to insert the current object into the list of specializations is stored at start of the import and is used later. If the list changes before the insertion the position is not valid any more.

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


1 Files Affected:

- (modified) clang/lib/AST/ASTImporter.cpp (+5-2) 


``````````diff
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index 023aaa7f0572b4..0036e506b63653 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -6504,6 +6504,11 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
       return D2;
   }
 
+  // Update InsertPos, because preceding import calls may have invalidated
+  // it by adding new specializations.
+  if (!VarTemplate->findSpecialization(TemplateArgs, InsertPos))
+    VarTemplate->AddSpecialization(D2, InsertPos);
+
   QualType T;
   if (Error Err = importInto(T, D->getType()))
     return std::move(Err);
@@ -6540,8 +6545,6 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
   if (FoundSpecialization)
     D2->setPreviousDecl(FoundSpecialization->getMostRecentDecl());
 
-  VarTemplate->AddSpecialization(D2, InsertPos);
-
   addDeclToContexts(D, D2);
 
   // Import the rest of the chain. I.e. import all subsequent declarations.

``````````

</details>


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


More information about the cfe-commits mailing list