-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Fix soundness issue for replace_range
and range
#81169
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1553,18 +1553,25 @@ impl String { | |
// Replace_range does not have the memory safety issues of a vector Splice. | ||
// of the vector version. The data is just plain bytes. | ||
|
||
match range.start_bound() { | ||
// WARNING: Inlining this variable would be unsound (#81138) | ||
let start = range.start_bound(); | ||
match start { | ||
Included(&n) => assert!(self.is_char_boundary(n)), | ||
Excluded(&n) => assert!(self.is_char_boundary(n + 1)), | ||
Unbounded => {} | ||
}; | ||
match range.end_bound() { | ||
// WARNING: Inlining this variable would be unsound (#81138) | ||
let end = range.end_bound(); | ||
match end { | ||
Included(&n) => assert!(self.is_char_boundary(n + 1)), | ||
Excluded(&n) => assert!(self.is_char_boundary(n)), | ||
Unbounded => {} | ||
}; | ||
|
||
unsafe { self.as_mut_vec() }.splice(range, replace_with.bytes()); | ||
// Using `range` again would be unsound (#81138) | ||
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. Ah, here it is. Hmm. 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. @bugadani Do you think another comment would be helpful? I added one where I thought it would make the most sense, since adding one above 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 think more comments wouldn't hurt 🙂 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. That's true 😛
dylni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// We assume the bounds reported by `range` remain the same, but | ||
// an adversarial implementation could change between calls | ||
unsafe { self.as_mut_vec() }.splice((start, end), replace_with.bytes()); | ||
} | ||
|
||
/// Converts this `String` into a [`Box`]`<`[`str`]`>`. | ||
|
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 think comments explaining this change might be beneficial here as well. With or without a big
// WARNING: do not inline this variable
:)