* 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                   |  3 +++
 src/numfmt.c           | 14 ++++++++++++--
 tests/numfmt/numfmt.pl |  4 ++++
 3 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index 4f78f9ea8..58cf776a9 100644
--- 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]
diff --git a/src/numfmt.c b/src/numfmt.c
index d85a881b6..fe6350111 100644
--- a/src/numfmt.c
+++ b/src/numfmt.c
@@ -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.  */
diff --git a/tests/numfmt/numfmt.pl b/tests/numfmt/numfmt.pl
index 75de1a9f9..073db4ab0 100755
--- a/tests/numfmt/numfmt.pl
+++ b/tests/numfmt/numfmt.pl
@@ -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"}],
-- 
2.52.0


Reply via email to