From cd35794d5ea6688e77dd17e96556f5de0b410e8a Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Sat, 4 Mar 2023 15:11:24 -0700 Subject: [PATCH] Comment for why char boundaries aren't checked --- library/core/src/str/traits.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/library/core/src/str/traits.rs b/library/core/src/str/traits.rs index a7a2b03774a8..b6d605c52d1a 100644 --- a/library/core/src/str/traits.rs +++ b/library/core/src/str/traits.rs @@ -204,6 +204,12 @@ unsafe impl const SliceIndex for ops::Range { assert_unsafe_precondition!( "str::get_unchecked requires that the range is within the string slice", (this: ops::Range, slice: *const [u8]) => + // We'd like to check that the bounds are on char boundaries, + // but there's not really a way to do so without reading + // behind the pointer, which has aliasing implications. + // It's also not possible to move this check up to + // `str::get_unchecked` without adding a special function + // to `SliceIndex` just for this. this.end >= this.start && this.end <= slice.len() ); slice.as_ptr().add(self.start)