Skip to content

Commit 2966d99

Browse files
committed
Fix a use-after-delete in ec59bf9 caught by ASan.
1 parent c65acf0 commit 2966d99

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/ASTWalker.h"
2020
#include "swift/AST/NameLookup.h"
2121
#include "swift/AST/Pattern.h"
22+
#include "swift/Basic/Defer.h"
2223
#include "swift/Basic/SourceManager.h"
2324
#include "swift/Basic/StringExtras.h"
2425
#include "swift/Parse/Lexer.h"
@@ -827,6 +828,11 @@ bool swift::diagnoseArgumentLabelError(TypeChecker &TC, const Expr *expr,
827828

828829
auto tuple = dyn_cast<TupleExpr>(expr);
829830
if (!tuple) {
831+
llvm::SmallString<16> str;
832+
// If the diagnostic is local, flush it before returning.
833+
// This makes sure it's emitted before 'str' is destroyed.
834+
defer { diagOpt.reset(); };
835+
830836
if (newNames[0].empty()) {
831837
// This is probably a conversion from a value of labeled tuple type to
832838
// a scalar.
@@ -837,7 +843,6 @@ bool swift::diagnoseArgumentLabelError(TypeChecker &TC, const Expr *expr,
837843
if (scalarFieldIdx >= 0) {
838844
auto &field = tupleTy->getElement(scalarFieldIdx);
839845
if (field.hasName()) {
840-
llvm::SmallString<16> str;
841846
str = ".";
842847
str += field.getName().str();
843848
if (!existingDiag) {
@@ -864,7 +869,6 @@ bool swift::diagnoseArgumentLabelError(TypeChecker &TC, const Expr *expr,
864869
if (auto parenExpr = dyn_cast<ParenExpr>(expr))
865870
expr = parenExpr->getSubExpr();
866871

867-
llvm::SmallString<16> str;
868872
str += newNames[0].str();
869873
str += ": ";
870874
if (!existingDiag) {
@@ -987,6 +991,9 @@ bool swift::diagnoseArgumentLabelError(TypeChecker &TC, const Expr *expr,
987991
diag.fixItReplace(tuple->getElementNameLocs()[i], newStr);
988992
}
989993

994+
// If the diagnostic is local, flush it before returning.
995+
// This makes sure it's emitted before the message text buffers are destroyed.
996+
diagOpt.reset();
990997
return true;
991998
}
992999

lib/Sema/MiscDiagnostics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ void fixItAccessibility(InFlightDiagnostic &diag, ValueDecl *VD,
5353
///
5454
/// If \p existingDiag is null, the fix-its will be attached to an appropriate
5555
/// error diagnostic.
56+
///
57+
/// \returns true if the issue was diagnosed
5658
bool diagnoseArgumentLabelError(TypeChecker &TC, const Expr *expr,
5759
ArrayRef<Identifier> newNames,
5860
bool isSubscript,

test/attr/attr_availability.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ func unavailableUnnamed(_ a: Int) {} // expected-note {{here}}
284284
func unavailableUnnamedSame(_ a: Int) {} // expected-note {{here}}
285285
@available(*, unavailable, renamed: "shinyLabeledArguments(_:)")
286286
func unavailableNewlyUnnamed(a: Int) {} // expected-note {{here}}
287+
@available(*, unavailable, renamed: "shinyLabeledArguments(veryLongNameToOverflowASmallStringABCDEFGHIJKLMNOPQRSTUVWXYZ:)")
288+
func unavailableVeryLongArgNames(a: Int) {} // expected-note {{here}}
287289

288290
@available(*, unavailable, renamed: "shinyLabeledArguments(a:b:)")
289291
func unavailableMultiSame(a: Int, b: Int) {} // expected-note {{here}}
@@ -310,6 +312,7 @@ func testArgNames() {
310312
unavailableUnnamed(0) // expected-error {{'unavailableUnnamed' has been renamed to 'shinyLabeledArguments(example:)'}} {{3-21=shinyLabeledArguments}} {{22-22=example: }}
311313
unavailableUnnamedSame(0) // expected-error {{'unavailableUnnamedSame' has been renamed to 'shinyLabeledArguments(_:)'}} {{3-25=shinyLabeledArguments}}
312314
unavailableNewlyUnnamed(a: 0) // expected-error {{'unavailableNewlyUnnamed(a:)' has been renamed to 'shinyLabeledArguments(_:)'}} {{3-26=shinyLabeledArguments}} {{27-30=}}
315+
unavailableVeryLongArgNames(a: 0) // expected-error {{'unavailableVeryLongArgNames(a:)' has been renamed to 'shinyLabeledArguments(veryLongNameToOverflowASmallStringABCDEFGHIJKLMNOPQRSTUVWXYZ:)'}} {{3-30=shinyLabeledArguments}} {{31-32=veryLongNameToOverflowASmallStringABCDEFGHIJKLMNOPQRSTUVWXYZ}}
313316
unavailableMultiSame(a: 0, b: 1) // expected-error {{'unavailableMultiSame(a:b:)' has been renamed to 'shinyLabeledArguments(a:b:)'}} {{3-23=shinyLabeledArguments}}
314317
unavailableMultiUnnamed(0, 1) // expected-error {{'unavailableMultiUnnamed' has been renamed to 'shinyLabeledArguments(example:another:)'}} {{3-26=shinyLabeledArguments}} {{27-27=example: }} {{30-30=another: }}
315318
unavailableMultiUnnamedSame(0, 1) // expected-error {{'unavailableMultiUnnamedSame' has been renamed to 'shinyLabeledArguments(_:_:)'}} {{3-30=shinyLabeledArguments}}

0 commit comments

Comments
 (0)