Skip to content

Commit e379ab9

Browse files
authored
Merge pull request #20557 from brentdax/this-is-why-swift-is-a-memory-safe-language
Fix lookupDirect() use-after-scope bugs
2 parents 4ad727e + 270ad33 commit e379ab9

File tree

4 files changed

+6
-8
lines changed

4 files changed

+6
-8
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7040,7 +7040,7 @@ void SwiftDeclConverter::importInheritedConstructors(
70407040

70417041
auto curObjCClass = cast<clang::ObjCInterfaceDecl>(classDecl->getClangDecl());
70427042

7043-
auto inheritConstructors = [&](ArrayRef<ValueDecl *> members,
7043+
auto inheritConstructors = [&](TinyPtrVector<ValueDecl *> members,
70447044
Optional<CtorInitializerKind> kind) {
70457045
const auto &languageVersion =
70467046
Impl.SwiftContext.LangOpts.EffectiveLanguageVersion;

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,15 @@ SILInstruction *CastOptimizer::optimizeBridgedSwiftToObjCCast(
354354

355355
auto *NTD = Source.getNominalOrBoundGenericNominal();
356356
assert(NTD);
357-
SmallVector<ValueDecl *, 4> FoundMembers;
358-
ArrayRef<ValueDecl *> Members;
359-
Members = NTD->lookupDirect(M.getASTContext().Id_bridgeToObjectiveC);
357+
auto Members = NTD->lookupDirect(M.getASTContext().Id_bridgeToObjectiveC);
360358
if (Members.empty()) {
359+
SmallVector<ValueDecl *, 4> FoundMembers;
361360
if (NTD->getDeclContext()->lookupQualified(
362361
NTD, M.getASTContext().Id_bridgeToObjectiveC,
363362
NLOptions::NL_ProtocolMembers, FoundMembers)) {
364-
Members = FoundMembers;
365363
// Returned members are starting with the most specialized ones.
366364
// Thus, the first element is what we are looking for.
367-
Members = Members.take_front(1);
365+
Members.push_back(FoundMembers.front());
368366
}
369367
}
370368

lib/Sema/CSDiag.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7084,7 +7084,7 @@ bool FailureDiagnosis::visitObjectLiteralExpr(ObjectLiteralExpr *E) {
70847084
return false;
70857085
DeclName constrName = TC.getObjectLiteralConstructorName(E);
70867086
assert(constrName);
7087-
ArrayRef<ValueDecl *> constrs = protocol->lookupDirect(constrName);
7087+
auto constrs = protocol->lookupDirect(constrName);
70887088
if (constrs.size() != 1 || !isa<ConstructorDecl>(constrs.front()))
70897089
return false;
70907090
auto *constr = cast<ConstructorDecl>(constrs.front());

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ namespace {
13061306
// use the right labels before forming the call to the initializer.
13071307
DeclName constrName = tc.getObjectLiteralConstructorName(expr);
13081308
assert(constrName);
1309-
ArrayRef<ValueDecl *> constrs = protocol->lookupDirect(constrName);
1309+
auto constrs = protocol->lookupDirect(constrName);
13101310
if (constrs.size() != 1 || !isa<ConstructorDecl>(constrs.front())) {
13111311
tc.diagnose(protocol, diag::object_literal_broken_proto);
13121312
return nullptr;

0 commit comments

Comments
 (0)