Skip to content

Commit bcd7ff1

Browse files
ChrisDentonpietroalbini
authored andcommitted
More robust extension checking
1 parent f54dd91 commit bcd7ff1

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

library/std/src/sys/pal/windows/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use self::rand::hashmap_random_keys;
1414
#[macro_use]
1515
pub mod compat;
1616

17-
mod api;
17+
pub mod api;
1818

1919
pub mod alloc;
2020
pub mod args;

library/std/src/sys/pal/windows/process.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,24 @@ impl Command {
279279
None
280280
};
281281
let program = resolve_exe(&self.program, || env::var_os("PATH"), child_paths)?;
282-
// Case insensitive "ends_with" of UTF-16 encoded ".bat" or ".cmd"
283-
let is_batch_file = matches!(
284-
program.len().checked_sub(5).and_then(|i| program.get(i..)),
285-
Some([46, 98 | 66, 97 | 65, 116 | 84, 0] | [46, 99 | 67, 109 | 77, 100 | 68, 0])
286-
);
282+
let has_bat_extension = |program: &[u16]| {
283+
matches!(
284+
// Case insensitive "ends_with" of UTF-16 encoded ".bat" or ".cmd"
285+
program.len().checked_sub(4).and_then(|i| program.get(i..)),
286+
Some([46, 98 | 66, 97 | 65, 116 | 84] | [46, 99 | 67, 109 | 77, 100 | 68])
287+
)
288+
};
289+
let is_batch_file = if path::is_verbatim(&program) {
290+
has_bat_extension(&program[..program.len() - 1])
291+
} else {
292+
super::fill_utf16_buf(
293+
|buffer, size| unsafe {
294+
// resolve the path so we can test the final file name.
295+
c::GetFullPathNameW(program.as_ptr(), size, buffer, ptr::null_mut())
296+
},
297+
|program| has_bat_extension(program),
298+
)?
299+
};
287300
let (program, mut cmd_str) = if is_batch_file {
288301
(
289302
command_prompt()?,

library/std/src/sys/path/windows.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::ffi::{OsStr, OsString};
22
use crate::io;
33
use crate::path::{Path, PathBuf, Prefix};
44
use crate::ptr;
5+
use crate::sys::api::utf16;
56
use crate::sys::pal::{c, fill_utf16_buf, os2path, to_u16s};
67

78
#[cfg(test)]
@@ -20,6 +21,10 @@ pub fn is_verbatim_sep(b: u8) -> bool {
2021
b == b'\\'
2122
}
2223

24+
pub fn is_verbatim(path: &[u16]) -> bool {
25+
path.starts_with(utf16!(r"\\?\")) || path.starts_with(utf16!(r"\??\"))
26+
}
27+
2328
/// Returns true if `path` looks like a lone filename.
2429
pub(crate) fn is_file_name(path: &OsStr) -> bool {
2530
!path.as_encoded_bytes().iter().copied().any(is_sep_byte)

tests/ui/std/windows-bat-args.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ fn parent() {
3232
let bat2 = String::from(bat.to_str().unwrap());
3333
bat.set_file_name("windows-bat-args3.bat");
3434
let bat3 = String::from(bat.to_str().unwrap());
35-
let bat = [bat1.as_str(), bat2.as_str(), bat3.as_str()];
35+
bat.set_file_name("windows-bat-args1.bat .. ");
36+
let bat4 = String::from(bat.to_str().unwrap());
37+
let bat = [bat1.as_str(), bat2.as_str(), bat3.as_str(), bat4.as_str()];
3638

3739
check_args(&bat, &["a", "b"]).unwrap();
3840
check_args(&bat, &["c is for cat", "d is for dog"]).unwrap();

0 commit comments

Comments
 (0)