@@ -1512,6 +1512,13 @@ fn test_copy_within() {
1512
1512
let mut bytes = * b"Hello, World!" ;
1513
1513
bytes. copy_within ( .., 0 ) ;
1514
1514
assert_eq ! ( & bytes, b"Hello, World!" ) ;
1515
+
1516
+ // Ensure that copying at the end of slice won't cause UB.
1517
+ let mut bytes = * b"Hello, World!" ;
1518
+ bytes. copy_within ( 13 ..13 , 5 ) ;
1519
+ assert_eq ! ( & bytes, b"Hello, World!" ) ;
1520
+ bytes. copy_within ( 5 ..5 , 13 ) ;
1521
+ assert_eq ! ( & bytes, b"Hello, World!" ) ;
1515
1522
}
1516
1523
1517
1524
#[ test]
@@ -1536,6 +1543,13 @@ fn test_copy_within_panics_src_inverted() {
1536
1543
// 2 is greater than 1, so this range is invalid.
1537
1544
bytes. copy_within ( 2 ..1 , 0 ) ;
1538
1545
}
1546
+ #[ test]
1547
+ #[ should_panic( expected = "attempted to index slice up to maximum usize" ) ]
1548
+ fn test_copy_within_panics_src_out_of_bounds ( ) {
1549
+ let mut bytes = * b"Hello, World!" ;
1550
+ // an inclusive range ending at usize::max_value() would make src_end overflow
1551
+ bytes. copy_within ( usize:: max_value ( ) ..=usize:: max_value ( ) , 0 ) ;
1552
+ }
1539
1553
1540
1554
#[ test]
1541
1555
fn test_is_sorted ( ) {
0 commit comments