More robust extension checking · patricklam/verify-rust-std@fee6300 (original) (raw)

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