Skip to content

Commit 270ad33

Browse files
committed
Fix lookupDirect() use-after-scope bugs
In #7530, NominalTypeDecl::lookupDirect() started returning TinyPtrVector instead of ArrayRef so that it wouldn’t be returning a pointer into a mutable data structure. Unfortunately, some callees assigned its return value into an ArrayRef; C++ happily converted the TinyPtrVector to an ArrayRef and then treated the TinyPtrVector as out-of-scope, so the ArrayRef would now point to an out-of-scope object. Oops.
1 parent 5f21c12 commit 270ad33

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
@@ -7048,7 +7048,7 @@ void SwiftDeclConverter::importInheritedConstructors(
70487048

70497049
auto curObjCClass = cast<clang::ObjCInterfaceDecl>(classDecl->getClangDecl());
70507050

7051-
auto inheritConstructors = [&](ArrayRef<ValueDecl *> members,
7051+
auto inheritConstructors = [&](TinyPtrVector<ValueDecl *> members,
70527052
Optional<CtorInitializerKind> kind) {
70537053
const auto &languageVersion =
70547054
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
@@ -7090,7 +7090,7 @@ bool FailureDiagnosis::visitObjectLiteralExpr(ObjectLiteralExpr *E) {
70907090
return false;
70917091
DeclName constrName = TC.getObjectLiteralConstructorName(E);
70927092
assert(constrName);
7093-
ArrayRef<ValueDecl *> constrs = protocol->lookupDirect(constrName);
7093+
auto constrs = protocol->lookupDirect(constrName);
70947094
if (constrs.size() != 1 || !isa<ConstructorDecl>(constrs.front()))
70957095
return false;
70967096
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)