Closed
Description
I would expect this code to panic at the drain()
because usize::max_value()
is an out of bounds index:
fn string_drain_overflow() {
let mut string = String::new();
string.drain(..=usize::max_value());
}
Instead, it succeeds and returns an empty iterator. Looking at the implementation for String::drain()
, I'm assuming an integer overflow happens at line 1542 Included(&n) => n + 1
and the range is taken to be 0..0
. Honestly, I expected the add operation itself to panic on overflow so I'm not entirely sure what the correct behaviour is here.
Meta
rustc 1.43.1 (8d69840ab 2020-05-04)
binary: rustc
commit-hash: 8d69840ab92ea7f4d323420088dd8c9775f180cd
commit-date: 2020-05-04
host: x86_64-unknown-linux-gnu
release: 1.43.1
LLVM version: 9.0
Update: the same bug exists in String::replace_range
.