Skip to content

Commit bd93229

Browse files
committed
SILOptimizer: fix a few instances of use-after-free
When building on Windows, the std::string being passed will on the stack, where the destructor will be invoked before the return, nil'ing out reference. This causes incorrect behaviour when building the diagnostic or FIXITs. Explicitly create the StringRef to prevent the std::string from being invalidated.
1 parent 8e38b67 commit bd93229

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -929,12 +929,13 @@ void LifetimeChecker::handleStoreUse(unsigned UseID) {
929929
// a diagnostic.
930930
if (!shouldEmitError(Use.Inst))
931931
continue;
932-
932+
933933
std::string PropertyName;
934934
auto *VD = TheMemory.getPathStringToElement(i, PropertyName);
935935
diagnose(Module, Use.Inst->getLoc(),
936-
diag::immutable_property_already_initialized, PropertyName);
937-
936+
diag::immutable_property_already_initialized,
937+
StringRef(PropertyName));
938+
938939
if (auto *Var = dyn_cast<VarDecl>(VD)) {
939940
if (Var->getParentInitializer())
940941
diagnose(Module, SILLocation(VD),
@@ -1089,7 +1090,7 @@ void LifetimeChecker::handleInOutUse(const DIMemoryUse &Use) {
10891090

10901091
std::string PropertyName;
10911092
auto VD = TheMemory.getPathStringToElement(i, PropertyName);
1092-
1093+
10931094
// Try to produce a specific error message about the inout use. If this is
10941095
// a call to a method or a mutating property access, indicate that.
10951096
// Otherwise, we produce a generic error.
@@ -1154,15 +1155,15 @@ void LifetimeChecker::handleInOutUse(const DIMemoryUse &Use) {
11541155
: diag::using_mutating_accessor_on_immutable_value,
11551156
accessor->getStorage()->getBaseName(),
11561157
isa<SubscriptDecl>(accessor->getStorage()),
1157-
PropertyName);
1158+
StringRef(PropertyName));
11581159
} else if (FD && FD->isOperator()) {
11591160
diagnose(Module, Use.Inst->getLoc(),
11601161
diag::mutating_method_called_on_immutable_value,
1161-
FD->getName(), /*operator*/ 1, PropertyName);
1162+
FD->getName(), /*operator*/ 1, StringRef(PropertyName));
11621163
} else if (FD && isSelfParameter) {
11631164
diagnose(Module, Use.Inst->getLoc(),
11641165
diag::mutating_method_called_on_immutable_value,
1165-
FD->getName(), /*method*/ 0, PropertyName);
1166+
FD->getName(), /*method*/ 0, StringRef(PropertyName));
11661167
} else if (isAssignment) {
11671168
diagnose(Module, Use.Inst->getLoc(),
11681169
diag::assignment_to_immutable_value, StringRef(PropertyName));

0 commit comments

Comments
 (0)