Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2c76737

Browse files
committedFeb 13, 2024
Expand assumes to the other unchecked slice ops
1 parent eaff1af commit 2c76737

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed
 

‎library/core/src/slice/index.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ unsafe impl<T> SliceIndex<[T]> for usize {
246246
"slice::get_unchecked_mut requires that the index is within the slice",
247247
);
248248
// SAFETY: see comments for `get_unchecked` above.
249-
unsafe { slice.as_mut_ptr().add(self) }
249+
unsafe {
250+
crate::hint::assert_unchecked(self < slice.len());
251+
slice.as_mut_ptr().add(self)
252+
}
250253
}
251254

252255
#[inline]
@@ -298,7 +301,10 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
298301
// cannot be longer than `isize::MAX`. They also guarantee that
299302
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
300303
// so the call to `add` is safe.
301-
unsafe { ptr::slice_from_raw_parts(slice.as_ptr().add(self.start()), self.len()) }
304+
unsafe {
305+
crate::hint::assert_unchecked(self.end() <= slice.len());
306+
ptr::slice_from_raw_parts(slice.as_ptr().add(self.start()), self.len())
307+
}
302308
}
303309

304310
#[inline]
@@ -308,7 +314,10 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
308314
"slice::get_unchecked_mut requires that the index is within the slice",
309315
);
310316
// SAFETY: see comments for `get_unchecked` above.
311-
unsafe { ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start()), self.len()) }
317+
unsafe {
318+
crate::hint::assert_unchecked(self.end() <= slice.len());
319+
ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start()), self.len())
320+
}
312321
}
313322

314323
#[inline]
@@ -368,6 +377,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
368377
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
369378
// so the call to `add` is safe and the length calculation cannot overflow.
370379
unsafe {
380+
crate::hint::assert_unchecked(self.end <= slice.len());
371381
let new_len = unchecked_sub(self.end, self.start);
372382
ptr::slice_from_raw_parts(slice.as_ptr().add(self.start), new_len)
373383
}
@@ -381,6 +391,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
381391
);
382392
// SAFETY: see comments for `get_unchecked` above.
383393
unsafe {
394+
crate::hint::assert_unchecked(self.end <= slice.len());
384395
let new_len = unchecked_sub(self.end, self.start);
385396
ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start), new_len)
386397
}

‎tests/codegen/issues/issue-116878.rs

+24
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,27 @@ pub unsafe fn unchecked_slice_no_bounds_check(s: &[u8]) -> u8 {
1010
// CHECK-NOT: panic_bounds_check
1111
a + s[0]
1212
}
13+
14+
// CHECK-LABEL: @unchecked_slice_no_bounds_check_mut
15+
#[no_mangle]
16+
pub unsafe fn unchecked_slice_no_bounds_check_mut(s: &mut [u8]) -> u8 {
17+
let a = *s.get_unchecked_mut(2);
18+
// CHECK-NOT: panic_bounds_check
19+
a + s[1]
20+
}
21+
22+
// CHECK-LABEL: @unchecked_slice_no_bounds_check_range
23+
#[no_mangle]
24+
pub unsafe fn unchecked_slice_no_bounds_check_range(s: &[u8]) -> u8 {
25+
let _a = &s.get_unchecked(..1);
26+
// CHECK-NOT: panic_bounds_check
27+
s[0]
28+
}
29+
30+
// CHECK-LABEL: @unchecked_slice_no_bounds_check_range_mut
31+
#[no_mangle]
32+
pub unsafe fn unchecked_slice_no_bounds_check_range_mut(s: &mut [u8]) -> u8 {
33+
let _a = &mut s.get_unchecked_mut(..2);
34+
// CHECK-NOT: panic_bounds_check
35+
s[1]
36+
}

0 commit comments

Comments
 (0)
Failed to load comments.