Skip to content

find: match nothing when -mindepth exceeds -maxdepth - #854

Open
Developer1010x wants to merge 1 commit into
uutils:mainfrom
Developer1010x:find-mindepth-gt-maxdepth
Open

find: match nothing when -mindepth exceeds -maxdepth#854
Developer1010x wants to merge 1 commit into
uutils:mainfrom
Developer1010x:find-mindepth-gt-maxdepth

Conversation

@Developer1010x

Copy link
Copy Markdown

Fixes #778.

Problem

$ mkdir -p /tmp/dt/a/b/c && touch /tmp/dt/a/b/c/f /tmp/dt/a/x

$ /usr/bin/find /tmp/dt -mindepth 3 -maxdepth 1     # GNU
$ find /tmp/dt -mindepth 3 -maxdepth 1              # uutils, before
/tmp/dt/a

An empty depth range should match nothing, but uutils printed entries at depth 1.

Cause

walkdir's min_depth() and max_depth() setters both clamp min_depth down to max_depth, so -mindepth 3 -maxdepth 1 was silently turned into -mindepth 1 -maxdepth 1. Swapping the call order does not help — either setter clamps.

Fix

Detect the impossible range before building the walker. In that case walk the starting point only (max_depth(0)), so errors for a bad starting point are still reported, and discard every entry the walk yields.

$ find /nope -mindepth 3 -maxdepth 1
Error: /nope: No such file or directory (os error 2)   # rc=1, matching GNU's diagnostic

Verification

Every combination below now agrees with GNU find on output and exit code, with and without -depth:

args GNU uutils
-mindepth 3 -maxdepth 1 (empty) (empty)
-mindepth 5 -maxdepth 0 (empty) (empty)
-mindepth 1 -maxdepth 3 4 entries same
-mindepth 2 -maxdepth 2 2 entries same
-maxdepth 1 2 entries same
-mindepth 2 4 entries same

Added find_mindepth_greater_than_maxdepth, covering both traversal orders. cargo test, cargo clippy --all-targets -- -D warnings and cargo fmt --check are all clean.

No GNU source was consulted; the fix is derived from walkdir's documented clamping behaviour.

walkdir's min_depth()/max_depth() setters clamp min_depth down to
max_depth, so an impossible range such as `-mindepth 3 -maxdepth 1`
silently became `-mindepth 1 -maxdepth 1` and printed entries at depth 1.
GNU find matches nothing for such a range.

Detect the impossible range up front, walk the starting point only so
errors for it are still reported, and discard every entry the walk
yields.

Fixes uutils#778
@codecov

codecov Bot commented Aug 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.32%. Comparing base (3de01a0) to head (4401d05).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #854      +/-   ##
==========================================
+ Coverage   92.31%   92.32%   +0.01%     
==========================================
  Files          35       35              
  Lines        7466     7482      +16     
  Branches      388      391       +3     
==========================================
+ Hits         6892     6908      +16     
  Misses        433      433              
  Partials      141      141              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed-hq

codspeed-hq Bot commented Aug 26, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing Developer1010x:find-mindepth-gt-maxdepth (4401d05) with main (3de01a0)

Open in CodSpeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(find): when -mindepth greater than -maxdepth it outputs files, even if must not

1 participant