hoy created this revision. Herald added subscribers: cfe-commits, wenlei. Herald added a project: clang. hoy requested review of this revision.
Template argument deduction may update the underlying specialization container and invalidate previous iterators of the container. This triggered an assert (in DeclTemplate.cpp) and corrupted the heap with writing through invalid iterators. This change recomputes invalided iterators in place before they are referenced. Test Plan: Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D87853 Files: clang/lib/Sema/SemaTemplate.cpp Index: clang/lib/Sema/SemaTemplate.cpp =================================================================== --- clang/lib/Sema/SemaTemplate.cpp +++ clang/lib/Sema/SemaTemplate.cpp @@ -4471,7 +4471,16 @@ // Note that we do not instantiate a definition until we see an odr-use // in DoMarkVarDeclReferenced(). // FIXME: LateAttrs et al.? - VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( + // Search for the variable template specialization declaration that + // corresponds to these arguments again to get an up-to-date InsertPos. + // The template argument deduction in between may update the underlying + // specialization container and invalidate previous iterators. + VarTemplateSpecializationDecl *Decl = + Template->findSpecialization(Converted, InsertPos); + // Assert we should not have a variable template specialization, otherwise we + // should have returned early. + assert(!Decl); + Decl = BuildVarTemplateInstantiation( Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); if (!Decl)
Index: clang/lib/Sema/SemaTemplate.cpp =================================================================== --- clang/lib/Sema/SemaTemplate.cpp +++ clang/lib/Sema/SemaTemplate.cpp @@ -4471,7 +4471,16 @@ // Note that we do not instantiate a definition until we see an odr-use // in DoMarkVarDeclReferenced(). // FIXME: LateAttrs et al.? - VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( + // Search for the variable template specialization declaration that + // corresponds to these arguments again to get an up-to-date InsertPos. + // The template argument deduction in between may update the underlying + // specialization container and invalidate previous iterators. + VarTemplateSpecializationDecl *Decl = + Template->findSpecialization(Converted, InsertPos); + // Assert we should not have a variable template specialization, otherwise we + // should have returned early. + assert(!Decl); + Decl = BuildVarTemplateInstantiation( Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); if (!Decl)
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits