]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
du: treat negative --max-depth values as errors
authorCollin Funk <collin.funk1@gmail.com>
Sat, 4 Jul 2026 03:24:39 +0000 (20:24 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Sat, 4 Jul 2026 03:29:48 +0000 (20:29 -0700)
* NEWS: Mention the bug fix.
* src/du.c (main): Prefer ckd_add to check for overflow. Emit an error
if the value is negative.
* tests/du/max-depth.sh: Add a test case for the bug.

NEWS
src/du.c
tests/du/max-depth.sh

diff --git a/NEWS b/NEWS
index 2ae329509f739347df96291cb1b64123a99102cf..b1ec92562caa00f8f241b41a67f613cb1d1c80a7 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,10 @@ GNU coreutils NEWS                                    -*- outline -*-
   will correctly match the last delimiter specified.
   [bug introduced with multi-byte support in coreutils-9.11]
 
+  'du --max-depth=N' now exits with a nonzero exit status and an error message
+  if N is negative.  Previously it behaved as if N were zero.
+  [bug introduced in coreutils-9.4]
+
   'head' and 'tail' now quote names in file headers when needed.
   [This bug was present in "the beginning".]
 
index bff1b6672ed7feb426d08feb89bd737094e762a8..9b80633711251a8fd0d1e5fe13cbfa90b3177aa8 100644 (file)
--- a/src/du.c
+++ b/src/du.c
@@ -860,11 +860,8 @@ main (int argc, char **argv)
           {
             intmax_t tmp;
             if (xstrtoimax (optarg, NULL, 0, &tmp, "") == LONGINT_OK
-                && tmp <= IDX_MAX)
-              {
-                max_depth_specified = true;
-                max_depth = tmp;
-              }
+                && ! ckd_add (&max_depth, tmp, 0) && 0 <= max_depth)
+              max_depth_specified = true;
             else
               {
                 error (0, 0, _("invalid maximum depth %s"),
index 8684379eb83e7c386d30b6a073a4a00aff924a3d..420d88632d0afc3d34a174723054a6d7c06719d7 100755 (executable)
@@ -36,4 +36,15 @@ cut -f2- out > k && mv k out
 compare exp out || fail=1
 compare /dev/null err || fail=1
 
+# Repeat, but use -d -1 and check for an error.
+# coreutils 9.4 to 9.11 would mistakenly behave as if --max-depth=0 were
+# specified when given a negative value.
+cat <<\EOF >exp || framework_failure_
+du: invalid maximum depth '-1'
+Try 'du --help' for more information.
+EOF
+returns_ 1 du -d -1 a >out 2>err || fail=1
+compare /dev/null out || fail=1
+compare exp err || fail=1
+
 Exit $fail