Skip to content

Commit 6101248

Browse files
authored
[clang] Fix a use-after free in ASTContext::getSubstBuiltinTemplatePack (#160970)
ASTContext::getSubstBuiltinTemplatePack finds InsertPos and then calls itself recursively, which may lead to rehashing and invalidation of all pointers to buckets. The function then proceeds with using the potentially invalid InsertPos, leading to use-after-free. The issue goes back to #157662. I didn't manage to produce a reasonably-sized test case yet.
1 parent 779adf1 commit 6101248

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

clang/lib/AST/ASTContext.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5873,8 +5873,14 @@ ASTContext::getSubstBuiltinTemplatePack(const TemplateArgument &ArgPack) {
58735873

58745874
QualType Canon;
58755875
TemplateArgument CanonArgPack = getCanonicalTemplateArgument(ArgPack);
5876-
if (!CanonArgPack.structurallyEquals(ArgPack))
5876+
if (!CanonArgPack.structurallyEquals(ArgPack)) {
58775877
Canon = getSubstBuiltinTemplatePack(CanonArgPack);
5878+
// Refresh InsertPos, in case the recursive call above caused rehashing,
5879+
// which would invalidate the bucket pointer.
5880+
[[maybe_unused]] const auto *Nothing =
5881+
SubstBuiltinTemplatePackTypes.FindNodeOrInsertPos(ID, InsertPos);
5882+
assert(!Nothing);
5883+
}
58785884

58795885
auto *PackType = new (*this, alignof(SubstBuiltinTemplatePackType))
58805886
SubstBuiltinTemplatePackType(Canon, ArgPack);

0 commit comments

Comments
 (0)