Skip to content

Off by one error in str::index_mut (specifically) for RangeToInclusive (specifically) #50002

Closed
@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).

rust/src/libcore/str/mod.rs

Lines 2107 to 2115 in 1ef1563

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions