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

Balázs Kéri via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 24 00:56:24 PDT 2024


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

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.

>From 462abbb0cd2b61ce8da9f27a6f064507606220cb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <balazs.keri at ericsson.com>
Date: Thu, 22 Feb 2024 15:34:52 +0100
Subject: [PATCH] [clang][ASTImporter] Fix possible crash "given incorrect
 InsertPos for specialization".

---
 clang/lib/AST/ASTImporter.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

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.



More information about the cfe-commits mailing list