]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
numfmt: fix dropped custom suffix when failing to parse
authorPádraig Brady <P@draigBrady.com>
Mon, 22 Dec 2025 13:14:57 +0000 (13:14 +0000)
committerPádraig Brady <P@draigBrady.com>
Mon, 22 Dec 2025 13:17:37 +0000 (13:17 +0000)
* src/numfmt.c (process_suffixed_number): Restore custom suffix
upon failure to parse number.
* tests/numfmt/numfmt.pl: Add test cases.
* NEWS: Mention the bug fix.
Fixes https://bugs.debian.org/1094581

NEWS
src/numfmt.c
tests/numfmt/numfmt.pl

diff --git a/NEWS b/NEWS
index 4f78f9ea8b522f3d1dfd7a3e31150944f009781c..58cf776a9ecafdbc3ab65dcf6b2234b8407f0039 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,9 @@ GNU coreutils NEWS                                    -*- outline -*-
   This also applies to the sha*sum and b2sum utilities.
   [This bug was present in "the beginning".]
 
+  'numfmt' no longer drops custom suffixes from numbers it cannot fully parse.
+  [bug introduced with numfmt in coreutils-8.21]
+
   'timeout' will now propagate all terminating signals to the monitored command.
   Previously 'timeout' could have exited and left the monitored command running.
   [bug introduced with timeout in coreutils-7.0]
index d85a881b694035b9000bc56ca2a153009eaaf6e4..fe6350111e209517850014f4b5dfa0873a9841c8 100644 (file)
@@ -1320,14 +1320,17 @@ print_padded_number (intmax_t padding)
 
 /* Converts the TEXT number string to the requested representation,
    and handles automatic suffix addition.  */
-static int
+static bool
 process_suffixed_number (char *text, long double *result,
                          size_t *precision, long int field)
 {
+  char saved_suffix = '\0';
+
   if (suffix)
     {
       if (mbs_endswith (text, suffix))
         {
+          saved_suffix = *(text + strlen (text) - strlen (suffix));
           *(text + strlen (text) - strlen (suffix)) = '\0';
           devmsg ("trimming suffix %s\n", quote (suffix));
         }
@@ -1361,7 +1364,14 @@ process_suffixed_number (char *text, long double *result,
 
   *result = val;
 
-  return (e == SSE_OK || e == SSE_OK_PRECISION_LOSS);
+  if (e == SSE_OK || e == SSE_OK_PRECISION_LOSS)
+    return true;
+  else
+    {
+      if (saved_suffix)
+        *(text + strlen (text)) = saved_suffix;
+      return false;
+    }
 }
 
 /* Return true if the current charset is UTF-8.  */
index 75de1a9f97ecac96b0718263aa7e6e08cfb2a59e..073db4ab0ad9000cd1884d6b63ee4ef92aba5b6d 100755 (executable)
@@ -157,6 +157,10 @@ my @Tests =
      ['suf-18', '--suffix=Foo --to=si   7000FooF',
               {ERR => "$prog: invalid suffix in input: '7000FooF'\n"},
               {EXIT => '2'}],
+     ['suf-18.1', '--invalid=ignore --suffix=QWE 12q3QWE', {OUT=>"12q3QWE"}],
+     ['suf-18.2', '--invalid=abort --suffix=QWE 12q3QWE',
+              {ERR => "$prog: invalid suffix in input: '12q3'\n"},
+              {EXIT => '2'}],
      # space(s) between number and suffix.  Note only field 1 is used
      # by default so specify the NUL delimiter to consider the whole "line".
      ['suf-19', "-d '' --from=si '4.0 K'",         {OUT => "4000"}],