Skip to content

Commit dd6a569

Browse files
committed
RequirementMachine: Fix use-after-free detected by ASAN
We're adding new rules to the Rules list while iterating over it, which is not good.
1 parent b2ca238 commit dd6a569

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/AST/RequirementMachine/RewriteSystemCompletion.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ void RewriteSystem::processMergedAssociatedTypes() {
312312
// Add the rule X.[P1:T] => X.[P1&P2:T].
313313
addRule(rhs, mergedTerm);
314314

315+
// Collect new rules here so that we're not adding rules while iterating
316+
// over the rules list.
317+
SmallVector<std::pair<MutableTerm, MutableTerm>, 2> inducedRules;
318+
315319
// Look for conformance requirements on [P1:T] and [P2:T].
316320
for (const auto &otherRule : Rules) {
317321
const auto &otherLHS = otherRule.getLHS();
@@ -343,10 +347,14 @@ void RewriteSystem::processMergedAssociatedTypes() {
343347
MutableTerm newRHS;
344348
newRHS.add(mergedAtom);
345349

346-
addRule(newLHS, newRHS);
350+
inducedRules.emplace_back(newLHS, newRHS);
347351
}
348352
}
349353
}
354+
355+
// Now add the new rules.
356+
for (const auto &pair : inducedRules)
357+
addRule(pair.first, pair.second);
350358
}
351359

352360
MergedAssociatedTypes.clear();

0 commit comments

Comments
 (0)