]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
mktemp: fix template diagnostic with --suffix
authorlvgenggeng <lvgenggeng@uniontech.com>
Tue, 27 Feb 2024 01:41:27 +0000 (09:41 +0800)
committerPádraig Brady <P@draigBrady.com>
Tue, 27 Feb 2024 14:59:33 +0000 (14:59 +0000)
* 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.

NEWS
src/mktemp.c
tests/misc/mktemp.pl

diff --git a/NEWS b/NEWS
index 8b1856cbd814e1854305c7cb3d60139110ab1354..69e282e3784cec3fd62ce6087894e2042cf44629 100644 (file)
--- 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 &nbsp) grouping character.
index 5f8e5155df0207e3d4a25f7d3eef1f22a7c11c80..e314ff6e3e8ce30da03c0717de30d7af4053ae22 100644 (file)
@@ -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)
     {
index 779fdc63d3cc23db470f9f07a1681b54e1e7fad7..95127517b460d7274b32fe7a07bb9c07098ccbc2 100755 (executable)
@@ -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=."},