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

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 12 06:00:18 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir

Author: None (w-gc)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/158288.diff


1 Files Affected:

- (modified) llvm/lib/IR/Value.cpp (+9) 


``````````diff
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();

``````````

</details>


https://github.com/llvm/llvm-project/pull/158288


More information about the llvm-commits mailing list