1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-15 00:12:00 +02:00

numfmt: fix dropped custom suffix when failing to parse

* 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
This commit is contained in:
Pádraig Brady
2025-12-22 13:14:57 +00:00
parent 1313c24056
commit 955dfdafa2
3 changed files with 19 additions and 2 deletions

3
NEWS
View File

@@ -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]

View 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. */

View File

@@ -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"}],