Off by one error in str::index_mut (specifically) for RangeToInclusive (specifically) · Issue #50002 · rust-lang/rust (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

@ExpHP

Description

@ExpHP

Caught while fleshing out a much beefier test suite for SliceIndex, which I plan to submit as a PR. (this Issue is here just in case that PR never happens).

fn index_mut(self, slice: &mut str) -> &mut Self::Output {
assert!(self.end != usize::max_value(),
"attempted to index str up to maximum usize");
if slice.is_char_boundary(self.end) {
unsafe { self.get_unchecked_mut(slice) }
} else {
super::slice_error_fail(slice, 0, self.end + 1)
}
}

The unicode character boundary check here should be looking at self.end + 1. Better yet, the method could just delegate to RangeTo instead. (note: it can't delegate to get_mut here without NLL, which seems to be a leading factor as to how this function ended up this way)