Fix panic message when RangeFrom
index is out of bounds
Before, the `Range` method was called with `end = slice.len()`. Unfortunately, because `Range::index` first checks the order of the indices (start has to be smaller than end), an out of bounds index leads to `core::slice::slice_index_order_fail` being called. This prints the message 'slice index starts at 27 but ends at 10', which is worse than 'index 27 out of range for slice of length 10'. This is not only useful to normal users reading panic messages, but also for people inspecting assembly and being confused by `slice_index_order_fail` calls.
This commit is contained in:
parent
47ea6d90b0
commit
ce338046c8
@ -3241,12 +3241,18 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeFrom<usize> {
|
||||
|
||||
#[inline]
|
||||
fn index(self, slice: &[T]) -> &[T] {
|
||||
(self.start..slice.len()).index(slice)
|
||||
if self.start > slice.len() {
|
||||
slice_index_len_fail(self.start, slice.len());
|
||||
}
|
||||
unsafe { &*self.get_unchecked(slice) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn index_mut(self, slice: &mut [T]) -> &mut [T] {
|
||||
(self.start..slice.len()).index_mut(slice)
|
||||
if self.start > slice.len() {
|
||||
slice_index_len_fail(self.start, slice.len());
|
||||
}
|
||||
unsafe { &mut *self.get_unchecked_mut(slice) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1088,7 +1088,7 @@ mod slice_index {
|
||||
|
||||
good: data[6..] == [];
|
||||
bad: data[7..];
|
||||
message: "but ends at"; // perhaps not ideal
|
||||
message: "out of range";
|
||||
}
|
||||
|
||||
in mod rangeto_len {
|
||||
|
Loading…
Reference in New Issue
Block a user