Skip to content

Commit d921073

Browse files
authored
Merge pull request #3521 from jfinkels/mktemp-tmpdir-absolute-path
mktemp: correct error message on absolute path
2 parents bda9f9f + 8a941db commit d921073

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/uu/mktemp/src/mktemp.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
152152
let dry_run = matches.is_present(OPT_DRY_RUN);
153153
let suppress_file_err = matches.is_present(OPT_QUIET);
154154

155-
let (prefix, rand, suffix) = parse_template(template, matches.value_of(OPT_SUFFIX))?;
156-
157-
if matches.is_present(OPT_TMPDIR) && PathBuf::from(prefix).is_absolute() {
155+
// If `--tmpdir` is given, the template cannot be an absolute
156+
// path. For example, `mktemp --tmpdir=a /XXX` is not allowed.
157+
if matches.is_present(OPT_TMPDIR) && PathBuf::from(template).is_absolute() {
158158
return Err(MkTempError::InvalidTemplate(template.into()).into());
159159
}
160160

161+
let (prefix, rand, suffix) = parse_template(template, matches.value_of(OPT_SUFFIX))?;
162+
161163
let res = if dry_run {
162164
dry_exec(tmpdir, prefix, rand, suffix)
163165
} else {

tests/by-util/test_mktemp.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,22 @@ fn test_mktemp_directory_tmpdir() {
414414
assert!(PathBuf::from(result.stdout_str().trim()).is_dir());
415415
}
416416

417+
/// Test that an absolute path is disallowed when --tmpdir is provided.
418+
#[test]
419+
fn test_tmpdir_absolute_path() {
420+
#[cfg(windows)]
421+
let path = r"C:\XXX";
422+
#[cfg(not(windows))]
423+
let path = "/XXX";
424+
new_ucmd!()
425+
.args(&["--tmpdir=a", path])
426+
.fails()
427+
.stderr_only(format!(
428+
"mktemp: invalid template, '{}'; with --tmpdir, it may not be absolute\n",
429+
path
430+
));
431+
}
432+
417433
/// Decide whether a string matches a given template.
418434
///
419435
/// In the template, the character `'X'` is treated as a wildcard,

0 commit comments

Comments
 (0)