[llvm] [IR] Fix string overlap issue in `Value::setNameImpl` (PR #158288)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 14 23:11:18 PDT 2025


https://github.com/w-gc updated https://github.com/llvm/llvm-project/pull/158288

>From f3ea12956a98061c2452238aefcad9e075a4de8e Mon Sep 17 00:00:00 2001
From: w-gc <25614556+w-gc at users.noreply.github.com>
Date: Fri, 12 Sep 2025 20:58:12 +0800
Subject: [PATCH 1/2] [IR] Fix string overlap issue in Value::setNameImpl

---
 llvm/lib/IR/Value.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp
index 4e8f359481b81..a7aa7fb04d99e 100644
--- a/llvm/lib/IR/Value.cpp
+++ b/llvm/lib/IR/Value.cpp
@@ -374,6 +374,15 @@ void Value::setNameImpl(const Twine &NewName) {
   // NOTE: Could optimize for the case the name is shrinking to not deallocate
   // then reallocated.
   if (hasName()) {
+    if (!NameRef.empty()) {
+      StringRef OldNameRef = getName();
+      if ((OldNameRef.bytes_begin() < NameRef.bytes_end()) &&
+          (NameRef.bytes_begin() < OldNameRef.bytes_end())) {
+        NewName.toVector(NameData);
+        NameRef = StringRef(NameData.data(), NameData.size());
+      }
+    }
+
     // Remove old name.
     ST->removeValueName(getValueName());
     destroyValueName();

>From 36bfc120658ec15afeaaba92f24c7fafcdd61b95 Mon Sep 17 00:00:00 2001
From: w-gc <25614556+w-gc at users.noreply.github.com>
Date: Mon, 15 Sep 2025 14:10:51 +0800
Subject: [PATCH 2/2] [IR] Fix string overlap issue in Value::setNameImpl

---
 llvm/lib/IR/Value.cpp | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp
index a7aa7fb04d99e..7ae5a58c24707 100644
--- a/llvm/lib/IR/Value.cpp
+++ b/llvm/lib/IR/Value.cpp
@@ -356,6 +356,16 @@ void Value::setNameImpl(const Twine &NewName) {
   if (getSymTab(this, ST))
     return;  // Cannot set a name on this value (e.g. constant).
 
+  // Copy NewName forcely if the memory overlaps.
+  if (!NameRef.empty()) {
+    StringRef OldNameRef = getName();
+    if ((OldNameRef.bytes_begin() < NameRef.bytes_end()) &&
+        (NameRef.bytes_begin() < OldNameRef.bytes_end())) {
+      NewName.toVector(NameData);
+      NameRef = StringRef(NameData.data(), NameData.size());
+    }
+  }
+
   if (!ST) { // No symbol table to update?  Just do the change.
     // NOTE: Could optimize for the case the name is shrinking to not deallocate
     // then reallocated.
@@ -374,15 +384,6 @@ void Value::setNameImpl(const Twine &NewName) {
   // NOTE: Could optimize for the case the name is shrinking to not deallocate
   // then reallocated.
   if (hasName()) {
-    if (!NameRef.empty()) {
-      StringRef OldNameRef = getName();
-      if ((OldNameRef.bytes_begin() < NameRef.bytes_end()) &&
-          (NameRef.bytes_begin() < OldNameRef.bytes_end())) {
-        NewName.toVector(NameData);
-        NameRef = StringRef(NameData.data(), NameData.size());
-      }
-    }
-
     // Remove old name.
     ST->removeValueName(getValueName());
     destroyValueName();



More information about the llvm-commits mailing list