-
Notifications
You must be signed in to change notification settings - Fork 13.3k
fix str mutating through a ptr derived from &self #58200
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1757,9 +1757,9 @@ mod traits { | |
} | ||
#[inline] | ||
unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output { | ||
let ptr = slice.as_ptr().add(self.start); | ||
let ptr = slice.as_mut_ptr().add(self.start); | ||
let len = self.end - self.start; | ||
super::from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr as *mut u8, len)) | ||
super::from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, len)) | ||
} | ||
#[inline] | ||
fn index(self, slice: &str) -> &Self::Output { | ||
|
@@ -1821,8 +1821,8 @@ mod traits { | |
} | ||
#[inline] | ||
unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output { | ||
let ptr = slice.as_ptr(); | ||
super::from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr as *mut u8, self.end)) | ||
let ptr = slice.as_mut_ptr(); | ||
super::from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, self.end)) | ||
} | ||
#[inline] | ||
fn index(self, slice: &str) -> &Self::Output { | ||
|
@@ -1883,9 +1883,9 @@ mod traits { | |
} | ||
#[inline] | ||
unsafe fn get_unchecked_mut(self, slice: &mut str) -> &mut Self::Output { | ||
let ptr = slice.as_ptr().add(self.start); | ||
let ptr = slice.as_mut_ptr().add(self.start); | ||
let len = slice.len() - self.start; | ||
super::from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr as *mut u8, len)) | ||
super::from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, len)) | ||
} | ||
#[inline] | ||
fn index(self, slice: &str) -> &Self::Output { | ||
|
@@ -2213,6 +2213,22 @@ impl str { | |
self as *const str as *const u8 | ||
} | ||
|
||
/// Converts a mutable string slice to a raw pointer. | ||
/// | ||
/// As string slices are a slice of bytes, the raw pointer points to a | ||
/// [`u8`]. This pointer will be pointing to the first byte of the string | ||
/// slice. | ||
/// | ||
/// It is your responsibility to make sure that the string slice only gets | ||
/// modified in a way that it remains valid UTF-8. | ||
/// | ||
/// [`u8`]: primitive.u8.html | ||
#[unstable(feature = "str_as_mut_ptr", issue = "0")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. r+ with a tracking issue There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opened a tracking issue in #58215, please check if that looks all right. |
||
#[inline] | ||
pub fn as_mut_ptr(&mut self) -> *mut u8 { | ||
self as *mut str as *mut u8 | ||
} | ||
|
||
/// Returns a subslice of `str`. | ||
/// | ||
/// This is the non-panicking alternative to indexing the `str`. Returns | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Notice that this raw ptr cast here is the "canary" that gave away that the old code was wrong -- it was actually casting
*const u8
to*mut u8
, which should not have been necessary.