From: Pádraig Brady Date: Wed, 22 Oct 2025 12:53:46 +0000 (+0100) Subject: numfmt: promptly diagnose write errors X-Git-Tag: v9.9~57 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5971f450b5abffd5a335a8937d6d588431137594;p=thirdparty%2Fcoreutils.git numfmt: promptly diagnose write errors * src/numfmt.c (process line): Inspect the stdio error state when outputting each line so that we don't have to check each output function but do eventually exit upon write error, while also remaining buffered. (main): Also check when outputting a header for the edge case of very long headers. * tests/misc/write-errors.sh: Enable the numfmt test case. * NEWS: Mention the improvement, and reorganize all numfmt improvements. --- diff --git a/NEWS b/NEWS index 7641e4f6ac..b49b73c547 100644 --- a/NEWS +++ b/NEWS @@ -45,9 +45,11 @@ GNU coreutils NEWS -*- outline -*- 'install' now uses posix_spawn() to invoke the strip program more efficiently. - numfmt now parses numbers with a non-breaking space character before a unit, - and parses numbers containing grouping characters from the current locale. - It also supports a multi-byte --delimiter character. + 'numfmt': + - parses numbers with a non-breaking space character before a unit + - parses numbers containing grouping characters from the current locale + - supports a multi-byte --delimiter character + - no longer processes input indefinitely in the presence of write errors wc -l now operates 10% faster on hosts that support AVX512 instructions. diff --git a/src/numfmt.c b/src/numfmt.c index 5a1e94570d..1ae831ba3f 100644 --- a/src/numfmt.c +++ b/src/numfmt.c @@ -1512,6 +1512,9 @@ process_line (char *line, bool newline) if (newline) putchar (line_delim); + if (ferror (stdout)) + write_error (); + return valid_number; } @@ -1710,7 +1713,10 @@ main (int argc, char **argv) while (header-- && getdelim (&line, &line_allocated, line_delim, stdin) > 0) - fputs (line, stdout); + { + if (fputs (line, stdout) == EOF) + write_error (); + } while ((len = getdelim (&line, &line_allocated, line_delim, stdin)) > 0) diff --git a/tests/misc/write-errors.sh b/tests/misc/write-errors.sh index b30f4f6bd3..782ccae5a7 100755 --- a/tests/misc/write-errors.sh +++ b/tests/misc/write-errors.sh @@ -41,7 +41,7 @@ fold --version; yes | tr -d '\\n' | fold head -z -n-1 /dev/zero join -a 1 -z /dev/zero /dev/null # TODO: nl --version; yes | nl -# TODO: numfmt --version; yes 1 | numfmt +numfmt --version; yes 1 | numfmt od -v /dev/zero paste /dev/zero # TODO: pr /dev/zero