@@ -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 }
0 commit comments