File tree 2 files changed +12
-4
lines changed
2 files changed +12
-4
lines changed Original file line number Diff line number Diff line change @@ -1372,13 +1372,14 @@ impl<A: Array> SmallVec<A> {
1372
1372
}
1373
1373
let mut ptr = ptr. as_ptr ( ) ;
1374
1374
let len = * len_ptr;
1375
+ if index > len {
1376
+ panic ! ( "index exceeds length" ) ;
1377
+ }
1378
+ // SAFETY: add is UB if index > len, but we panicked first
1375
1379
ptr = ptr. add ( index) ;
1376
1380
if index < len {
1381
+ // Shift element to the right of `index`.
1377
1382
ptr:: copy ( ptr, ptr. add ( 1 ) , len - index) ;
1378
- } else if index == len {
1379
- // No elements need shifting.
1380
- } else {
1381
- panic ! ( "index exceeds length" ) ;
1382
1383
}
1383
1384
* len_ptr = len + 1 ;
1384
1385
ptr:: write ( ptr, element) ;
Original file line number Diff line number Diff line change @@ -1049,3 +1049,10 @@ fn max_swap_remove() {
1049
1049
let mut sv: SmallVec < [ i32 ; 2 ] > = smallvec ! [ 0 ] ;
1050
1050
sv. swap_remove ( usize:: MAX ) ;
1051
1051
}
1052
+
1053
+ #[ test]
1054
+ #[ should_panic]
1055
+ fn test_insert_out_of_bounds ( ) {
1056
+ let mut v: SmallVec < [ i32 ; 4 ] > = SmallVec :: new ( ) ;
1057
+ v. insert ( 10 , 6 ) ;
1058
+ }
You can’t perform that action at this time.
0 commit comments