[clang] 1d75b18 - [analyzer][NFC] Fix dangling StringRef in barely used code

Donát Nagy via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 30 08:17:52 PDT 2023


Author: Donát Nagy
Date: 2023-06-30T17:15:43+02:00
New Revision: 1d75b18843fbca52655e240a120b5fdeeef17c0e

URL: https://github.com/llvm/llvm-project/commit/1d75b18843fbca52655e240a120b5fdeeef17c0e
DIFF: https://github.com/llvm/llvm-project/commit/1d75b18843fbca52655e240a120b5fdeeef17c0e.diff

LOG: [analyzer][NFC] Fix dangling StringRef in barely used code

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

Added: 
    

Modified: 
    clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
index 9bd5a802d5d64e..9923c41e6ad2d1 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
@@ -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);
   }
 


        


More information about the cfe-commits mailing list