[llvm] [llvm-cgdata] Fix Dangling StringRefs (PR #143210)

Nuri Amari via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 6 14:52:07 PDT 2025


https://github.com/NuriAmari created https://github.com/llvm/llvm-project/pull/143210

`OutputFilename` and `Filename` are assigned to StringRefs in `parseArgs`, but after `parseArgs` returns the `InputArgsList` that once owned the underlying strings is popped off the stack, and the StringRefs can be left pointing at garbage.

Switch to std::string to own the strings.

>From 94580db32b5c117756527941f4cfdfe03fa079a1 Mon Sep 17 00:00:00 2001
From: Nuri Amari <nuriamari at fb.com>
Date: Fri, 6 Jun 2025 14:44:02 -0700
Subject: [PATCH] [llvm-cgdata] Fix Dangling StringRefs

`OutputFilename` and `Filename` are assigned to StringRefs in
`parseArgs`, but after `parseArgs` returns the `InputArgsList` that once
owned the underlying strings is popped off the stack, and the StringRefs
can be left pointing at garbage.

Switch to std::string to own the strings.
---
 llvm/tools/llvm-cgdata/llvm-cgdata.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/tools/llvm-cgdata/llvm-cgdata.cpp b/llvm/tools/llvm-cgdata/llvm-cgdata.cpp
index 5c028311b06b5..98fa5c5657353 100644
--- a/llvm/tools/llvm-cgdata/llvm-cgdata.cpp
+++ b/llvm/tools/llvm-cgdata/llvm-cgdata.cpp
@@ -75,8 +75,8 @@ class CGDataOptTable : public opt::GenericOptTable {
 
 // Options
 static StringRef ToolName;
-static StringRef OutputFilename = "-";
-static StringRef Filename;
+static std::string OutputFilename = "-";
+static std::string Filename;
 static bool ShowCGDataVersion;
 static bool SkipTrim;
 static CGDataAction Action;



More information about the llvm-commits mailing list