Skip to content

Commit cb7c549

Browse files
committed
Avoid a potential use-after-free by storing a std::string (NFC)
rdar://111956360
1 parent f802bcb commit cb7c549

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

lldb/source/Plugins/TypeSystem/Swift/StoringDiagnosticConsumer.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
197197
formatted_text = !s.empty() ? std::move(s) : std::string(text);
198198
}
199199
RawDiagnostic diagnostic(
200-
formatted_text, info.Kind, bufferName, bufferID, line_col.first,
200+
formatted_text, info.Kind, bufferName.str(), bufferID, line_col.first,
201201
line_col.second,
202202
use_fixits ? info.FixIts : llvm::ArrayRef<swift::Diagnostic::FixIt>());
203203
if (info.ID == swift::diag::error_from_clang.ID ||
@@ -287,8 +287,7 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
287287

288288
// Need to remap the error/warning to a different line.
289289
StreamString match;
290-
match.Printf("%s:%u:", diagnostic.bufferName.str().c_str(),
291-
diagnostic.line);
290+
match.Printf("%s:%u:", diagnostic.bufferName.c_str(), diagnostic.line);
292291
const size_t match_len = match.GetString().size();
293292
size_t match_pos = diagnostic.description.find(match.GetString().str());
294293
if (match_pos == std::string::npos)
@@ -302,7 +301,7 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
302301
fixed_description.Printf(
303302
"%s",
304303
diagnostic.description.substr(start_pos, match_pos).c_str());
305-
fixed_description.Printf("%s:%u:", diagnostic.bufferName.str().c_str(),
304+
fixed_description.Printf("%s:%u:", diagnostic.bufferName.c_str(),
306305
diagnostic.line - first_line + 1);
307306
start_pos = match_pos + match_len;
308307
match_pos =
@@ -373,30 +372,29 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
373372
struct RawDiagnostic {
374373
RawDiagnostic() = default;
375374
RawDiagnostic(std::string in_desc, swift::DiagnosticKind in_kind,
376-
llvm::StringRef in_bufferName, unsigned in_bufferID,
375+
std::string in_bufferName, unsigned in_bufferID,
377376
uint32_t in_line, uint32_t in_column,
378377
llvm::ArrayRef<swift::Diagnostic::FixIt> in_fixits)
379-
: description(in_desc), kind(in_kind), bufferName(in_bufferName),
380-
bufferID(in_bufferID), line(in_line), column(in_column),
381-
fixits(in_fixits) {}
382-
RawDiagnostic(const RawDiagnostic &other)
383-
: description(other.description), kind(other.kind),
384-
bufferName(other.bufferName), bufferID(other.bufferID),
385-
line(other.line), column(other.column), fixits(other.fixits) {}
378+
: description(in_desc), bufferName(in_bufferName), fixits(in_fixits),
379+
kind(in_kind), bufferID(in_bufferID), line(in_line),
380+
column(in_column) {}
381+
RawDiagnostic(const RawDiagnostic &other) = default;
386382
bool operator==(const RawDiagnostic& other) {
387-
return kind == other.kind && bufferID == other.bufferID &&
388-
line == other.line && column == other.column &&
389-
description == other.description;
383+
return kind == other.kind && line == other.line &&
384+
bufferID == other.bufferID && column == other.column &&
385+
bufferName == other.bufferName && description == other.description;
390386
}
391387
bool operator!=(const RawDiagnostic &other) { return !(*this == other); }
392388

393389
std::string description;
390+
std::string bufferName;
391+
std::vector<swift::DiagnosticInfo::FixIt> fixits;
394392
swift::DiagnosticKind kind = swift::DiagnosticKind::Error;
395-
llvm::StringRef bufferName;
393+
/// Only stored for comparison in HandleDiagnostic. There is no
394+
/// guarantee that the SourceMgr is still alive in a stored diagnostic.
396395
unsigned bufferID;
397396
uint32_t line;
398397
uint32_t column;
399-
std::vector<swift::DiagnosticInfo::FixIt> fixits;
400398
};
401399

402400
SwiftASTContext &m_ast_context;

0 commit comments

Comments
 (0)