diff --git a/src/find/mod.rs b/src/find/mod.rs index f6b80751..47f1f8b9 100644 --- a/src/find/mod.rs +++ b/src/find/mod.rs @@ -211,6 +211,16 @@ fn process_dir( matcher: &dyn matchers::Matcher, quit: &mut bool, ) -> i32 { + // No depth can satisfy both bounds when -mindepth exceeds -maxdepth, so GNU + // find visits nothing at all. walkdir clamps min_depth down to max_depth + // instead (see `WalkDir::min_depth`), which would make us descend anyway, so + // handle the empty traversal ourselves. + if config.min_depth > config.max_depth { + let mut matcher_io = matchers::MatcherIO::new(deps); + matcher.finished(&mut matcher_io); + return matcher_io.exit_code(); + } + let mut walkdir = WalkDir::new(dir) .contents_first(config.depth_first) .max_depth(config.max_depth) diff --git a/tests/test_find.rs b/tests/test_find.rs index 2fbdff37..acf7bda8 100644 --- a/tests/test_find.rs +++ b/tests/test_find.rs @@ -1130,6 +1130,40 @@ fn find_ls_unmapped_owner_renders_numeric_id() { .stdout_contains(unmapped.to_string()); } +#[test] +fn find_mindepth_greater_than_maxdepth() { + ucmd() + .args(&["./test_data/depth", "-mindepth", "2", "-maxdepth", "1"]) + .succeeds() + .no_stderr() + .no_stdout(); +} + +#[test] +fn find_maxdepth_less_than_mindepth_reversed_order() { + ucmd() + .args(&["./test_data/depth", "-maxdepth", "1", "-mindepth", "2"]) + .succeeds() + .no_stderr() + .no_stdout(); +} + +#[test] +fn find_mindepth_greater_than_maxdepth_depth_first() { + ucmd() + .args(&[ + "./test_data/depth", + "-mindepth", + "2", + "-maxdepth", + "1", + "-depth", + ]) + .succeeds() + .no_stderr() + .no_stdout(); +} + #[test] #[cfg(unix)] fn find_slashes() {