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

DonĂ¡t Nagy via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 27 09:23:44 PDT 2023


donat.nagy created this revision.
donat.nagy added reviewers: Szelethus, gamesh411, steakhal.
Herald added subscribers: manas, ASDenysPetrov, dkrupp, mikhail.ramalho, a.sidorin, szepet, baloghadamsoftware, xazax.hun.
Herald added a reviewer: NoQ.
Herald added a project: All.
donat.nagy requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

`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).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153889

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


Index: clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
===================================================================
--- clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
+++ clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
@@ -316,8 +316,8 @@
   ///        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);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153889.535017.patch
Type: text/x-patch
Size: 716 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230627/a755188c/attachment.bin>


More information about the cfe-commits mailing list