@@ -246,7 +246,10 @@ unsafe impl<T> SliceIndex<[T]> for usize {
246
246
"slice::get_unchecked_mut requires that the index is within the slice" ,
247
247
) ;
248
248
// 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
+ }
250
253
}
251
254
252
255
#[ inline]
@@ -298,7 +301,10 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
298
301
// cannot be longer than `isize::MAX`. They also guarantee that
299
302
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
300
303
// 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
+ }
302
308
}
303
309
304
310
#[ inline]
@@ -308,7 +314,10 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
308
314
"slice::get_unchecked_mut requires that the index is within the slice" ,
309
315
) ;
310
316
// 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
+ }
312
321
}
313
322
314
323
#[ inline]
@@ -368,6 +377,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
368
377
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
369
378
// so the call to `add` is safe and the length calculation cannot overflow.
370
379
unsafe {
380
+ crate :: hint:: assert_unchecked ( self . end <= slice. len ( ) ) ;
371
381
let new_len = unchecked_sub ( self . end , self . start ) ;
372
382
ptr:: slice_from_raw_parts ( slice. as_ptr ( ) . add ( self . start ) , new_len)
373
383
}
@@ -381,6 +391,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
381
391
) ;
382
392
// SAFETY: see comments for `get_unchecked` above.
383
393
unsafe {
394
+ crate :: hint:: assert_unchecked ( self . end <= slice. len ( ) ) ;
384
395
let new_len = unchecked_sub ( self . end , self . start ) ;
385
396
ptr:: slice_from_raw_parts_mut ( slice. as_mut_ptr ( ) . add ( self . start ) , new_len)
386
397
}
0 commit comments