1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #3543 from jfinkels/mktemp-suffix-path-separator

mktemp: fix error msg when suffix has path sep.
This commit is contained in:
Sylvestre Ledru 2022-05-20 11:56:47 +02:00 committed by GitHub
commit c517096e19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View file

@ -17,7 +17,7 @@ use std::env;
use std::error::Error; use std::error::Error;
use std::fmt::Display; use std::fmt::Display;
use std::iter; use std::iter;
use std::path::{is_separator, Path, PathBuf}; use std::path::{is_separator, Path, PathBuf, MAIN_SEPARATOR};
use rand::Rng; use rand::Rng;
use tempfile::Builder; use tempfile::Builder;
@ -129,6 +129,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}; };
let filename = path.file_name(); let filename = path.file_name();
let template = filename.unwrap().to_str().unwrap(); let template = filename.unwrap().to_str().unwrap();
// If the command line was `mktemp aXXX/b`, then we will
// find that `tmp`, which is the result of getting the
// parent when treating the argument as a path, contains
// at least three consecutive Xs. This means that there
// was a path separator in the suffix, which is not
// allowed.
if tmp.display().to_string().contains("XXX") {
return Err(MkTempError::SuffixContainsDirSeparator(format!(
"{}{}",
MAIN_SEPARATOR, template
))
.into());
}
(template, tmp) (template, tmp)
} }
} else { } else {

View file

@ -496,3 +496,18 @@ fn test_template_path_separator() {
"a/bXXX".quote() "a/bXXX".quote()
)); ));
} }
/// Test that a suffix with a path separator is invalid.
#[test]
fn test_suffix_path_separator() {
#[cfg(not(windows))]
new_ucmd!()
.arg("aXXX/b")
.fails()
.stderr_only("mktemp: invalid suffix '/b', contains directory separator\n");
#[cfg(windows)]
new_ucmd!()
.arg(r"aXXX\b")
.fails()
.stderr_only("mktemp: invalid suffix '\\b', contains directory separator\n");
}