From: Pádraig Brady
Date: Tue, 24 Jun 2025 14:47:48 +0000 (+0100) Subject: od: output standard diagnostics for invalid -w arguments X-Git-Tag: v9.8~288 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2b7f3621f8b994db9436318fbe818a34ee87859a;p=thirdparty%2Fcoreutils.git od: output standard diagnostics for invalid -w arguments * src/od.c (main): Don't pass LONGINT_OK to xstrtol_fatal(), as otherwise it will abort(). * tests/od/od.pl: Add test cases. * NEWS: Mention the bug fix. Addresses https://bugs.gnu.org/78879 --- diff --git a/NEWS b/NEWS index e4d9c9b608..116dd993ef 100644 --- a/NEWS +++ b/NEWS @@ -16,6 +16,10 @@ GNU coreutils NEWS -*- outline -*- write a NUL byte after a heap buffer, or output invalid addresses. [These bugs were present in "the beginning".] + 'od -w0' will now issue a diagnostic and exit gracefully. + Previously it would have aborted. + [bug introduced in coreutils-9.3] + sort with key character offsets of SIZE_MAX, could induce a read of 1 byte before an allocated heap buffer. For example: 'sort +0.18446744073709551615R input' on 64 bit systems. diff --git a/src/od.c b/src/od.c index 32e8498960..6b5c8675f1 100644 --- a/src/od.c +++ b/src/od.c @@ -1820,7 +1820,9 @@ main (int argc, char **argv) { intmax_t w_tmp; s_err = xstrtoimax (optarg, nullptr, 10, &w_tmp, ""); - if (s_err != LONGINT_OK || w_tmp <= 0) + if (s_err == LONGINT_OK && w_tmp <= 0) + s_err = LONGINT_INVALID; + if (s_err != LONGINT_OK) xstrtol_fatal (s_err, oi, c, long_options, optarg); if (ckd_add (&desired_width, w_tmp, 0)) error (EXIT_FAILURE, 0, _("%s is too large"), quote (optarg)); diff --git a/tests/od/od.pl b/tests/od/od.pl index affdc75aed..5bb271e607 100755 --- a/tests/od/od.pl +++ b/tests/od/od.pl @@ -23,6 +23,8 @@ use strict; # Turn off localization of executable's output. @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; +my $prog = 'od'; + # Use a file in /proc whose size is not likely to # change between the wc and od invocations. my $proc_file = '/proc/version'; @@ -64,11 +66,19 @@ my @Tests = ['wide-a', '-a -w65537 -An', {IN=>{g=>'x'}}, {OUT=>" x\n"}], ['wide-c', '-c -w65537 -An', {IN=>{g=>'x'}}, {OUT=>" x\n"}], ['wide-x', '-tx1 -w65537 -An', {IN=>{g=>'B'}}, {OUT=>" 42\n"}], + + # Ensure that invalid widths do not cause trouble. + # From coreutils-9.3 through coreutils-9.7, these would abort + ['invalid-w-1', '-w0 -An', {IN=>""}, {EXIT=>1}, + {ERR=>"$prog: invalid -w argument '0'\n"}], + ['invalid-w-2', '-w-1 -An', {IN=>""}, {EXIT=>1}, + {ERR=>"$prog: invalid -w argument '-1'\n"}], + ['invalid-w-3', '-ww -An', {IN=>""}, {EXIT=>1}, + {ERR=>"$prog: invalid -w argument 'w'\n"}], ); my $save_temps = $ENV{DEBUG}; my $verbose = $ENV{VERBOSE}; -my $prog = 'od'; my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); exit $fail;