Skip to content

Commit b3e1114

Browse files
authored
[DebugInfo] Pass string ownership to MarkupFilter (#75403)
Last `getline` call destroys `InputString`, and `finish` accesses dead `StringRef`. Detected with #72677. Fixes https://lab.llvm.org/buildbot/#/builders/sanitizer-x86_64-linux-fast
1 parent 11efcce commit b3e1114

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

llvm/include/llvm/DebugInfo/Symbolize/MarkupFilter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class MarkupFilter {
3939
///
4040
/// Invalid or unimplemented markup elements are removed. Some output may be
4141
/// deferred until future filter() or finish() call.
42-
void filter(StringRef Line);
42+
void filter(std::string &&InputLine);
4343

4444
/// Records that the input stream has ended and writes any deferred output.
4545
void finish();
@@ -142,7 +142,7 @@ class MarkupFilter {
142142
MarkupParser Parser;
143143

144144
// Current line being filtered.
145-
StringRef Line;
145+
std::string Line;
146146

147147
// A module info line currently being built. This incorporates as much mmap
148148
// information as possible before being emitted.

llvm/lib/DebugInfo/Symbolize/MarkupFilter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ MarkupFilter::MarkupFilter(raw_ostream &OS, LLVMSymbolizer &Symbolizer,
4141
ColorsEnabled(
4242
ColorsEnabled.value_or(WithColor::defaultAutoDetectFunction()(OS))) {}
4343

44-
void MarkupFilter::filter(StringRef Line) {
45-
this->Line = Line;
44+
void MarkupFilter::filter(std::string &&InputLine) {
45+
Line = std::move(InputLine);
4646
resetColor();
4747

4848
Parser.parseLine(Line);
@@ -695,7 +695,9 @@ void MarkupFilter::reportTypeError(StringRef Str, StringRef TypeName) const {
695695
// passed to beginLine().
696696
void MarkupFilter::reportLocation(StringRef::iterator Loc) const {
697697
errs() << Line;
698-
WithColor(errs().indent(Loc - Line.begin()), HighlightColor::String) << '^';
698+
WithColor(errs().indent(Loc - StringRef(Line).begin()),
699+
HighlightColor::String)
700+
<< '^';
699701
errs() << '\n';
700702
}
701703

@@ -741,7 +743,7 @@ uint64_t MarkupFilter::adjustAddr(uint64_t Addr, PCType Type) const {
741743
}
742744

743745
StringRef MarkupFilter::lineEnding() const {
744-
return Line.ends_with("\r\n") ? "\r\n" : "\n";
746+
return StringRef(Line).ends_with("\r\n") ? "\r\n" : "\n";
745747
}
746748

747749
bool MarkupFilter::MMap::contains(uint64_t Addr) const {

llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ static void filterMarkup(const opt::InputArgList &Args, LLVMSymbolizer &Symboliz
430430
std::string InputString;
431431
while (std::getline(std::cin, InputString)) {
432432
InputString += '\n';
433-
Filter.filter(InputString);
433+
Filter.filter(std::move(InputString));
434434
}
435435
Filter.finish();
436436
}

0 commit comments

Comments
 (0)