Skip to content

Path::strip_prefix may panic when a path consists only of a prefix component #93586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dlon opened this issue Feb 2, 2022 · 3 comments · Fixed by #93675
Closed

Path::strip_prefix may panic when a path consists only of a prefix component #93586

dlon opened this issue Feb 2, 2022 · 3 comments · Fixed by #93675
Assignees
Labels
A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` C-bug Category: This is a bug. O-windows Operating system: Windows T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@dlon
Copy link
Contributor

dlon commented Feb 2, 2022

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)

@dlon dlon added the C-bug Category: This is a bug. label Feb 2, 2022
@matthiaskrgr matthiaskrgr added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Feb 2, 2022
@ChrisDenton
Copy link
Member

I can confirm this. The problem originates in Components. A minimal example:

fn main() {
    let path = std::path::Path::new("C:");
    let mut iter = path.components();
    iter.next();
    iter.as_path(); // panics at 'range start index 2 out of range for slice of length 0'
}

@name1e5s
Copy link
Contributor

name1e5s commented Feb 5, 2022

It seems that the prefix_len() in include_cur_dir causes the issue, I'll try to fix.

@name1e5s
Copy link
Contributor

name1e5s commented Feb 5, 2022

@rustbot claim

@yaahc yaahc added A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` O-windows Operating system: Windows A-resolve Area: Name/path resolution done by `rustc_resolve` specifically and removed A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` A-resolve Area: Name/path resolution done by `rustc_resolve` specifically labels Feb 8, 2022
@bors bors closed this as completed in 83322c5 May 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` C-bug Category: This is a bug. O-windows Operating system: Windows T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants