1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-09-27 20:59:02 +00:00

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.
This commit is contained in:
Denis Cornehl 2022-03-08 20:58:58 +01:00 committed by GitHub
parent e71fefb912
commit 076a9e6b8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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]