From: lvgenggeng Date: Tue, 27 Feb 2024 01:41:27 +0000 (+0800) Subject: mktemp: fix template diagnostic with --suffix X-Git-Tag: v9.5~45 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e397ba1a31b99dbe4460e1aa8124786d487451c1;p=thirdparty%2Fcoreutils.git mktemp: fix template diagnostic with --suffix * src/mktemp.c (main): When --suffix is specified, TEMPLATE points to the meraged buffer DEST_NAME. As X's in the suffix are not significant to the generated random characters, the diagnostic for too few X's should only refer to the template portion. * tests/misc/mktemp.pl: Adjust accordingly. * NEWS: Mention the bug fix. --- diff --git a/NEWS b/NEWS index 8b1856cbd8..69e282e378 100644 --- a/NEWS +++ b/NEWS @@ -16,6 +16,10 @@ GNU coreutils NEWS -*- outline -*- numfmt options like --suffix no longer have an arbitrary 127-byte limit. [bug introduced with numfmt in coreutils-8.21] + mktemp with --suffix now better diagnoses templates with too few X's. + Previously it conflated the insignificant --suffix in the error. + [bug introduced in coreutils-8.1] + sort again handles thousands grouping characters in single-byte locales where the grouping character is greater than CHAR_MAX. For e.g. signed character platforms with a 0xA0 (aka  ) grouping character. diff --git a/src/mktemp.c b/src/mktemp.c index 5f8e5155df..e314ff6e3e 100644 --- a/src/mktemp.c +++ b/src/mktemp.c @@ -156,7 +156,7 @@ main (int argc, char **argv) int status = EXIT_SUCCESS; size_t x_count; size_t suffix_len; - char *dest_name; + char *dest_name = nullptr; initialize_main (&argc, &argv); set_program_name (argv[0]); @@ -255,7 +255,13 @@ main (int argc, char **argv) } x_count = count_consecutive_X_s (template, suffix - template); if (x_count < 3) - error (EXIT_FAILURE, 0, _("too few X's in template %s"), quote (template)); + { + /* when suffix was appended to template only diagnose the template. */ + if (template == dest_name) + template[suffix - template] = '\0'; + error (EXIT_FAILURE, 0, _("too few X's in template %s"), + quote (template)); + } if (use_dest_dir) { diff --git a/tests/misc/mktemp.pl b/tests/misc/mktemp.pl index 779fdc63d3..95127517b4 100755 --- a/tests/misc/mktemp.pl +++ b/tests/misc/mktemp.pl @@ -165,7 +165,7 @@ my @Tests = ['suffix10f', 'aXXb', {EXIT => 1}, {ERR=>"$prog: too few X's in template 'aXXb'\n"}], ['suffix10d', '-d --suffix=X aXX', {EXIT => 1}, - {ERR=>"$prog: too few X's in template 'aXXX'\n"}], + {ERR=>"$prog: too few X's in template 'aXX'\n"}], ['suffix11f', '--suffix=.txt', {OUT=>"./tmp.ZZZZZZZZZZ.txt\n"}, {ENV=>"TMPDIR=."},