Closed
Description
This code panics on Windows:
fn main() {
std::path::Path::new("C:").strip_prefix("C:");
}
According to the documentation for strip_prefix
, it should either succeed or return Err(StripPrefixError)
. Instead, it panics due to an out-of-range index:
thread 'main' panicked at 'range start index 2 out of range for slice of length 0', library\std\src\path.rs:716:24
Meta
rustc --version --verbose
:
rustc 1.58.1 (db9d1b20b 2022-01-20)
binary: rustc
commit-hash: db9d1b20bba1968c1ec1fc49616d4742c1725b4b
commit-date: 2022-01-20
host: x86_64-pc-windows-msvc
release: 1.58.1
LLVM version: 13.0.0
The bug is also present in nightly.
Backtrace
thread 'main' panicked at 'range start index 2 out of range for slice of length 0', library\std\src\path.rs:716:24
stack backtrace:
0: std::panicking::begin_panic_handler
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\std\src\panicking.rs:498
1: core::panicking::panic_fmt
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\core\src\panicking.rs:107
2: core::slice::index::slice_start_index_len_fail
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\core\src\slice\index.rs:34
3: core::slice::index::impl$5::index
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\core\src\slice\index.rs:324
4: core::slice::index::impl$0::index
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\core\src\slice\index.rs:15
5: std::path::Components::include_cur_dir
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\std\src\path.rs:722
6: std::path::Components::len_before_body
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\std\src\path.rs:657
7: std::path::Components::trim_right
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\std\src\path.rs:774
8: std::path::Components::as_path
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\std\src\path.rs:693
9: std::path::impl$62::_strip_prefix::closure$0
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\std\src\path.rs:2242
10: core::option::Option::map
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\core\src\option.rs:846
11: std::path::Path::_strip_prefix
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\/library\std\src\path.rs:2241
12: std::path::Path::strip_prefix<str>
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\std\src\path.rs:2237
13: test_path_panic::main
at .\src\main.rs:2
14: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b\library\core\src\ops\function.rs:227
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
error: process didn't exit successfully: `target\debug\test-path-panic.exe` (exit code: 101)