Skip to content

Commit 7200afa

Browse files
committed
Check for " and \ in a filename
And also fix typo.
1 parent 9270ca1 commit 7200afa

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,20 @@ pub(crate) fn make_bat_command_line(
307307
) -> io::Result<Vec<u16>> {
308308
// Set the start of the command line to `cmd.exe /c "`
309309
// It is necessary to surround the command in an extra pair of quotes,
310-
// hence The trailing quote here. It will be closed after all arguments
310+
// hence the trailing quote here. It will be closed after all arguments
311311
// have been added.
312312
let mut cmd: Vec<u16> = "cmd.exe /c \"".encode_utf16().collect();
313313

314314
// Push the script name surrounded by its quote pair.
315315
cmd.push(b'"' as u16);
316+
// Windows file names cannot contain a `"` character or end with `\\`.
317+
// If the script name does then return an error.
318+
if script.contains(&(b'"' as u16)) || script.last() == Some(&(b'\\' as u16)) {
319+
return Err(io::const_io_error!(
320+
io::ErrorKind::InvalidInput,
321+
"Windows file names may not contain `\"` or end with `\\`"
322+
));
323+
}
316324
cmd.extend_from_slice(script.strip_suffix(&[0]).unwrap_or(script));
317325
cmd.push(b'"' as u16);
318326

0 commit comments

Comments
 (0)