Skip to content

Commit

Permalink
[analyzer][NFC] Fix dangling StringRef in barely used code
Browse files Browse the repository at this point in the history
CheckerContext::getNoteTag has a shorthand version that takes a
plain 'StringRef Note' instead of a lambda that calculates the note.

The old implementation of this method was incorrect because it created a
lambda that captured the StringRef, which was dereferenced much later,
when the NoteTags were visited.

In the current codebase this does not cause errors because this method
is called only once, and there the `Note` argument is a string literal
that remains valid. However, I tried to use this method in a checker
that I was prototyping, and there it printed random memory junk (instead
of the message that I composed in a local variable).

Differential Revision: https://reviews.llvm.org/D153889
  • Loading branch information
NagyDonat committed Jun 30, 2023
1 parent 0e93c4a commit 1d75b18
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ class CheckerContext {
/// bug path significantly shorter.
const NoteTag *getNoteTag(StringRef Note, bool IsPrunable = false) {
return getNoteTag(
[Note](BugReporterContext &,
PathSensitiveBugReport &) { return std::string(Note); },
[Note = std::string(Note)](BugReporterContext &,
PathSensitiveBugReport &) { return Note; },
IsPrunable);
}

Expand Down

0 comments on commit 1d75b18

Please sign in to comment.