Skip to content

Commit e166ec9

Browse files
Merge pull request #62231 from eeckstein/fix-update-protocol-refs
IRGen: fix a memory use-after-free problem in `updateProtocolRefs`
2 parents 5cc0808 + f94a4ff commit e166ec9

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/IRGen/GenObjC.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,16 @@ static void updateProtocolRefs(IRGenModule &IGM,
505505
assert(clangImporter && "Must have a clang importer");
506506

507507
// Get the array containining the protocol refs.
508-
unsigned protocolRefsSize;
509-
llvm::ConstantArray *protocolRefs;
510-
std::tie(protocolRefsSize, protocolRefs) = getProtocolRefsList(protocol);
508+
unsigned protocolRefsSize = getProtocolRefsList(protocol).first;
511509
unsigned currentIdx = 0;
512510
auto inheritedObjCProtocols = getRuntimeProtocolList(objcProtocol->protocols());
513511
for (auto inheritedObjCProtocol : inheritedObjCProtocols) {
514512
assert(currentIdx < protocolRefsSize);
513+
514+
// Getting the `protocolRefs` constant must not be hoisted out of the loop
515+
// because this constant might be deleted by
516+
// `oldVar->replaceAllUsesWith(newOpd)` below.
517+
llvm::ConstantArray *protocolRefs = getProtocolRefsList(protocol).second;
515518
auto oldVar = protocolRefs->getOperand(currentIdx);
516519
// Map the objc protocol to swift protocol.
517520
auto optionalDecl = clangImporter->importDeclCached(inheritedObjCProtocol);

0 commit comments

Comments
 (0)