From 462abbb0cd2b61ce8da9f27a6f064507606220cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= 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 023aaa7f0572b..0036e506b6365 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.