From 076a9e6b8e715fc200812f6a73a17a9764d45aba Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Tue, 8 Mar 2022 20:58:58 +0100 Subject: [PATCH] fix(rust): overrides should only check full segments (#3668) Fixes a bug in the Rust module where overrides would match against both full path components as well as partial ones (e.g. "/etc/passwd" would be considered a prefix of "/etc/passwd_new") on Windows. Solution is to convert back to a PathBuf after lossy conversion so that Path::starts_with is used instead of str::starts_with. --- src/modules/rust.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/modules/rust.rs b/src/modules/rust.rs index 557c4afb4..c3b251610 100644 --- a/src/modules/rust.rs +++ b/src/modules/rust.rs @@ -1,5 +1,7 @@ use std::fs; use std::path::Path; +#[cfg(windows)] +use std::path::PathBuf; use std::process::Output; use serde::Deserialize; @@ -123,11 +125,15 @@ fn extract_toolchain_from_rustup_override_list(stdout: &str, cwd: &Path) -> Opti if stdout == "no overrides\n" { return None; } - // use display version of path, also allows stripping \\?\ - let cwd = cwd.to_string_lossy(); - // rustup strips \\?\ prefix + #[cfg(windows)] - let cwd = cwd.strip_prefix(r"\\?\").unwrap_or(&cwd); + let cwd = { + // use display version of path, also allows stripping \\?\ + let cwd = cwd.to_string_lossy(); + // rustup strips \\?\ prefix, + // so we do the same and convert back to a `Path` + PathBuf::from(cwd.strip_prefix(r"\\?\").unwrap_or(&cwd)) + }; stdout .lines() @@ -295,6 +301,8 @@ mod tests { static OVERRIDES_CWD_B: &str = "/home/user/src/b/tests"; static OVERRIDES_CWD_C: &str = "/home/user/src/c/examples"; static OVERRIDES_CWD_D: &str = "/home/user/src/b/d c/spaces"; + static OVERRIDES_CWD_E: &str = "/home/user/src/b_and_more"; + static OVERRIDES_CWD_F: &str = "/home/user/src/b"; assert_eq!( extract_toolchain_from_rustup_override_list(OVERRIDES_INPUT, OVERRIDES_CWD_A.as_ref()), Some("beta-x86_64-unknown-linux-gnu".to_owned()), @@ -311,6 +319,14 @@ mod tests { extract_toolchain_from_rustup_override_list(OVERRIDES_INPUT, OVERRIDES_CWD_D.as_ref()), Some("stable-x86_64-pc-windows-msvc".to_owned()), ); + assert_eq!( + extract_toolchain_from_rustup_override_list(OVERRIDES_INPUT, OVERRIDES_CWD_E.as_ref()), + None, + ); + assert_eq!( + extract_toolchain_from_rustup_override_list(OVERRIDES_INPUT, OVERRIDES_CWD_F.as_ref()), + Some("nightly-x86_64-unknown-linux-gnu".to_owned()), + ); } #[test]