From b22a11789294f30dcf043187d68a27bdcd6a2eaf Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Mon, 6 Jun 2022 09:46:15 -0400 Subject: [PATCH] mktemp: error on empty --suffix in some situations Make `mktemp` exit with an error if the `--suffix` option is the empty string and the template argument does not end in an "X". Previously, the program succeeded. Before this commit, $ mktemp --suffix= aXXXb apBEb After this commit, $ mktemp --suffix= aXXXb mktemp: with --suffix, template 'aXXXb' must end in X --- src/uu/mktemp/src/mktemp.rs | 8 ++++---- tests/by-util/test_mktemp.rs | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/uu/mktemp/src/mktemp.rs b/src/uu/mktemp/src/mktemp.rs index 78e36874ea6..7d1c4a55d8f 100644 --- a/src/uu/mktemp/src/mktemp.rs +++ b/src/uu/mktemp/src/mktemp.rs @@ -279,15 +279,15 @@ impl Params { // // For example, if the suffix command-line argument is ".txt" and // the template is "XXXabc", then `suffix` is "abc.txt". - let suffix_from_option = options.suffix.unwrap_or_else(|| "".to_string()); let suffix_from_template = &options.template[j..]; + if !suffix_from_template.is_empty() && options.suffix.is_some() { + return Err(MkTempError::MustEndInX(options.template)); + } + let suffix_from_option = options.suffix.unwrap_or_else(|| "".to_string()); let suffix = format!("{}{}", suffix_from_template, suffix_from_option); if suffix.contains(MAIN_SEPARATOR) { return Err(MkTempError::SuffixContainsDirSeparator(suffix)); } - if !suffix_from_template.is_empty() && !suffix_from_option.is_empty() { - return Err(MkTempError::MustEndInX(options.template)); - } // The number of random characters in the template. // diff --git a/tests/by-util/test_mktemp.rs b/tests/by-util/test_mktemp.rs index df630e715a4..67c075827da 100644 --- a/tests/by-util/test_mktemp.rs +++ b/tests/by-util/test_mktemp.rs @@ -619,3 +619,12 @@ fn test_three_contiguous_wildcard_blocks() { assert_matches_template!(template, filename); assert!(at.file_exists(filename)); } + +/// Test that template must end in X even if `--suffix` is the empty string. +#[test] +fn test_suffix_must_end_in_x() { + new_ucmd!() + .args(&["--suffix=", "aXXXb"]) + .fails() + .stderr_is("mktemp: with --suffix, template 'aXXXb' must end in X\n"); +}