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

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
This commit is contained in:
Jeffrey Finkelstein 2022-06-06 09:46:15 -04:00
parent 3a45f00fed
commit 6da070cdd3
2 changed files with 27 additions and 3 deletions

View file

@ -224,6 +224,11 @@ fn find_last_contiguous_block_of_xs(s: &str) -> Option<(usize, usize)> {
impl Params {
fn from(options: Options) -> Result<Self, MkTempError> {
// The template argument must end in 'X' if a suffix option is given.
if options.suffix.is_some() && !options.template.ends_with('X') {
return Err(MkTempError::MustEndInX(options.template));
}
// Get the start and end indices of the randomized part of the template.
//
// For example, if the template is "abcXXXXyz", then `i` is 3 and `j` is 7.
@ -285,9 +290,6 @@ impl Params {
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.
//

View file

@ -619,3 +619,25 @@ 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");
}
#[test]
fn test_suffix_empty_template() {
new_ucmd!()
.args(&["--suffix=aXXXb", ""])
.fails()
.stderr_is("mktemp: with --suffix, template '' must end in X\n");
new_ucmd!()
.args(&["-d", "--suffix=aXXXb", ""])
.fails()
.stderr_is("mktemp: with --suffix, template '' must end in X\n");
}