Skip to content

find: reject -files0-from /dev/stdin with -ok/-okdir (fixes #957) - #1054

Open
pethapixels-create wants to merge 4 commits into
uutils:mainfrom
pethapixels-create:fix/issue-957
Open

pethapixels-create wants to merge 4 commits into
uutils:mainfrom
pethapixels-create:fix/issue-957

Conversation

@pethapixels-create

Copy link
Copy Markdown
Contributor

Summary

Fixes #957: find did not reject combining -files0-from /dev/stdin with the interactive -ok/-okdir actions.

GNU find rejects this combination because both -files0-from /dev/stdin and -ok/-okdir consume standard input, which is ambiguous. uutils previously accepted it silently.

Root cause

In src/find/mod.rs, the -files0-from validation only rejected the literal - (standard input) when combined with -ok/-okdir:

if name == "-" && config.interactive_exec {
    return Err(From::from(
        "option -files0-from reading from standard input cannot be combined \
         with -ok, -okdir",
    ));
}

This guard never fired for -files0-from /dev/stdin (or /dev/fd/0, /proc/self/fd/0, or a list file that is also redirected onto stdin), because the source string was not the literal -. So the ambiguous "both actions read stdin" case slipped through.

Fix

Added a refers_to_stdin(name) helper (in src/find/mod.rs) that compares the file named by -files0-from against the actual open file descriptor 0 by device/inode identity (via /proc/self/fd/N on Unix, with a /dev/stdin fallback; returns false on non-Unix). The existing name == "-" check is extended so that, when -ok/-okdir is present, find also rejects any source that refers to standard input — including /dev/stdin, /dev/fd/0, /proc/self/fd/0, and a redirected list file — before doing any work:

if config.interactive_exec {
    if name == "-" {
        return Err(From::from(
            "option -files0-from reading from standard input cannot be combined \
             with -ok, -okdir",
        ));
    }
    if refers_to_stdin(name) {
        return Err(From::from(format!(
            "option -files0-from: standard input must not refer to the same \
             file when combined with -ok, -okdir: '{name}'",
        )));
    }
}

The new error message matches GNU's exact diagnostic (including the : '<name>' suffix) and exits with status 1. The change is small and idiomatic: it reuses the existing config.interactive_exec flag and adds one focused helper.

Behavior parity with GNU find

Verified against GNU findutils 4.10.0 (/usr/bin/find, LC_ALL=C):

Command GNU find 4.10.0 uutils (before) uutils (after)
-files0-from /dev/stdin -ok echo {} \; error, exit 1 silently ran error, exit 1 ✅
-files0-from /dev/stdin -okdir echo {} \; error, exit 1 silently ran error, exit 1 ✅
-files0-from <list> -ok echo {} \; < list (same file on stdin) error, exit 1 silently ran error, exit 1 ✅
-files0-from <regular-file> -ok echo {} \; (no stdin redirect) allowed allowed allowed ✅ (unchanged)

GNU's exact diagnostic (note the : '<name>' suffix, which the implementation reproduces):

find: option -files0-from: standard input must not refer to the same file when combined with -ok, -okdir: ‘/dev/stdin’

Regression tests

Added two tests to tests/test_find.rs:

  • files0_from_dev_stdin_rejected_with_ok — for both -ok and -okdir, and for each of /dev/stdin, /dev/fd/0, /proc/self/fd/0, asserts find -files0-from <alias> <action> echo {} \; fails with exit code 1, no stdout, and the GNU-matching error message (including the : '<alias>' suffix).
  • files0_from_same_file_as_stdin_rejected_with_ok — passes a regular file as both the -files0-from source and stdin (< list) with -ok; asserts it is rejected with exit code 1 and the GNU-matching message.

Test evidence

$ cargo test --test test_find files0
running 15 tests
test files0_from_dev_stdin_rejected_with_ok ... ok
test files0_from_same_file_as_stdin_rejected_with_ok ... ok
test files0_stdin_with_ok_is_rejected ... ok
... (all 15 files0 tests pass)

test result: ok. 15 passed; 0 failed; 0 ignored

Full suite (clean pass, no failures):

$ cargo test --test test_find
test result: ok. 172 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 2.79s

@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.45%. Comparing base (62dbd79) to head (1fc7bd0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1054      +/-   ##
==========================================
+ Coverage   92.43%   92.45%   +0.01%     
==========================================
  Files          38       38              
  Lines        8887     8908      +21     
  Branches      440      444       +4     
==========================================
+ Hits         8215     8236      +21     
  Misses        532      532              
  Partials      140      140              

☔ 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

codspeed Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 15 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 20 untouched benchmarks


Comparing pethapixels-create:fix/issue-957 (1fc7bd0) with main (62dbd79)

Open in CodSpeed

@sylvestre

Copy link
Copy Markdown
Contributor

i don't think we want PR_BODY.md in the tree ;) and sorry but it needs to be rebased, and rustfmt is sad :)

Signed-off-by: pethapixels-create <349370243+pethapixels-create@users.noreply.github.com>
Signed-off-by: pethapixels-create <349370243+pethapixels-create@users.noreply.github.com>
…erage)

Adds files0_from_missing_file_with_ok_reports_open_error, exercising the
metadata-error (false-return) branch of refers_to_stdin: a nonexistent
-files0-from name is not standard input, so the interactive-exec guard
does not fire and find falls through to the normal 'cannot open' error.
Raises patch coverage flagged by codecov (was 73.91%).

Signed-off-by: pethapixels-create <349370243+pethapixels-create@users.noreply.github.com>
Add a focused unit test exercising all three branches of refers_to_stdin
(matching /dev/stdin, non-matching regular file, missing-file error) to
lift codecov patch coverage on the new -files0-from + -ok/-okdir guard.

Simplify refers_to_stdin to a single let-else: /dev/stdin is a symlink to
/proc/self/fd/0 and metadata() follows symlinks, so statting it already
resolves to fd 0's actual file. This removes the redundant procfs lookup
and its environment-impossible fallback branch, making the helper fully
coverable while preserving identical behaviour.

Signed-off-by: pethapixels-create <349370243+pethapixels-create@users.noreply.github.com>
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.

find - -files0-from /dev/stdin is not rejected together with -ok/-okdir

2 participants