File tree Expand file tree Collapse file tree 2 files changed +16
-2
lines changed
Expand file tree Collapse file tree 2 files changed +16
-2
lines changed Original file line number Diff line number Diff line change @@ -823,13 +823,14 @@ impl<T> Slab<T> {
823823 }
824824
825825 let entries_ptr = self . entries . as_mut_ptr ( ) ;
826- let entries_cap = self . entries . capacity ( ) ;
826+ let entries_len = self . entries . len ( ) ;
827827
828828 let mut res = MaybeUninit :: < [ & mut T ; N ] > :: uninit ( ) ;
829829 let res_ptr = res. as_mut_ptr ( ) as * mut & mut T ;
830830
831831 for ( i, & key) in keys. iter ( ) . enumerate ( ) {
832- if key >= entries_cap {
832+ // `key` won't be greater than `entries_len`.
833+ if key >= entries_len {
833834 return Err ( GetDisjointMutError :: IndexOutOfBounds ) ;
834835 }
835836 // SAFETY: we made sure above that this key is in bounds.
Original file line number Diff line number Diff line change @@ -767,3 +767,16 @@ fn get_disjoint_mut() {
767767 assert_eq ! ( slab[ 0 ] , 4 ) ;
768768 assert_eq ! ( slab[ 4 ] , 0 ) ;
769769}
770+
771+ #[ test]
772+ fn get_disjoint_mut_out_of_bounds_index_error ( ) {
773+ let mut slab: Slab < i32 > = Slab :: with_capacity ( 10 ) ;
774+ slab. insert ( 1 ) ;
775+ slab. insert ( 2 ) ;
776+
777+ // Index 0 and 1 are valid, but index 5 is out of bounds (beyond len)
778+ assert_eq ! (
779+ slab. get_disjoint_mut( [ 0 , 1 , 5 ] ) ,
780+ Err ( GetDisjointMutError :: IndexOutOfBounds )
781+ ) ;
782+ }
You can’t perform that action at this time.
0 commit comments