Skip to content

Commit 36bfc12

Browse files
authored
[IR] Fix string overlap issue in Value::setNameImpl
1 parent cd30e8e commit 36bfc12

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

llvm/lib/IR/Value.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,16 @@ void Value::setNameImpl(const Twine &NewName) {
356356
if (getSymTab(this, ST))
357357
return; // Cannot set a name on this value (e.g. constant).
358358

359+
// Copy NewName forcely if the memory overlaps.
360+
if (!NameRef.empty()) {
361+
StringRef OldNameRef = getName();
362+
if ((OldNameRef.bytes_begin() < NameRef.bytes_end()) &&
363+
(NameRef.bytes_begin() < OldNameRef.bytes_end())) {
364+
NewName.toVector(NameData);
365+
NameRef = StringRef(NameData.data(), NameData.size());
366+
}
367+
}
368+
359369
if (!ST) { // No symbol table to update? Just do the change.
360370
// NOTE: Could optimize for the case the name is shrinking to not deallocate
361371
// then reallocated.
@@ -374,15 +384,6 @@ void Value::setNameImpl(const Twine &NewName) {
374384
// NOTE: Could optimize for the case the name is shrinking to not deallocate
375385
// then reallocated.
376386
if (hasName()) {
377-
if (!NameRef.empty()) {
378-
      StringRef OldNameRef = getName();
379-
      if ((OldNameRef.bytes_begin() < NameRef.bytes_end()) &&
380-
          (NameRef.bytes_begin() < OldNameRef.bytes_end())) {
381-
        NewName.toVector(NameData);
382-
        NameRef = StringRef(NameData.data(), NameData.size());
383-
      }
384-
    }
385-
386387
// Remove old name.
387388
ST->removeValueName(getValueName());
388389
destroyValueName();

0 commit comments

Comments
 (0)