]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
od: output standard diagnostics for invalid -w arguments
authorPádraig Brady <P@draigBrady.com>
Tue, 24 Jun 2025 14:47:48 +0000 (15:47 +0100)
committerPádraig Brady <P@draigBrady.com>
Tue, 24 Jun 2025 15:37:01 +0000 (16:37 +0100)
* 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

NEWS
src/od.c
tests/od/od.pl

diff --git a/NEWS b/NEWS
index e4d9c9b608f94fbeefff759f432b9e7d40129b80..116dd993efc0f2847fb74ee3940b4525cdbb6369 100644 (file)
--- 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.
index 32e8498960964f30844e2dfdb22939702cdcff4a..6b5c8675f1390da9792983db5e41b900649e38df 100644 (file)
--- 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));
index affdc75aed95865b9a6c8d66ee64a94bbd4b24e3..5bb271e607febc803110e54582cc8f75c5cd70de 100755 (executable)
@@ -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;