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"); +}