-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[IR] Fix string overlap issue in Value::setNameImpl
#158288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-ir Author: None (w-gc) ChangesFull diff: https://github.com/llvm/llvm-project/pull/158288.diff 1 Files Affected:
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();
|
Value::setNameImpl
LGTM. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add a unit test for this?
if (getSymTab(this, ST)) | ||
return; // Cannot set a name on this value (e.g. constant). | ||
|
||
// Copy NewName forcely if the memory overlaps. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Copy NewName forcely if the memory overlaps. | |
// Copy NewName if the memory overlaps. |
or "forcefully", but I think you can just omit it.
if (!NameRef.empty()) { | ||
StringRef OldNameRef = getName(); | ||
if ((OldNameRef.bytes_begin() < NameRef.bytes_end()) && | ||
(NameRef.bytes_begin() < OldNameRef.bytes_end())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is technically UB in the case where it doesn't overlap. One has to use std::less in this case.
No description provided.