Skip to content

find: reject -files0-from stdin aliases with -ok/-okdir - #997

Open
M3M0KA wants to merge 7 commits into
uutils:mainfrom
M3M0KA:fix/files0-stdin-alias
Open

M3M0KA wants to merge 7 commits into
uutils:mainfrom
M3M0KA:fix/files0-stdin-alias

Conversation

@M3M0KA

@M3M0KA M3M0KA commented Nov 12, 2026

Copy link
Copy Markdown

Summary

Fixes #957.

Previously, -files0-from could be combined with -ok or -okdir when the input file referred to stdin through another path, such as /dev/stdin.

This change compares the device and inode IDs of the opened file and stdin on Unix to detect when they refer to the same underlying file.

Changes

  • Reject conflicting -files0-from and -ok/-okdir combinations.
  • Add regression tests for /dev/stdin and a regular file also supplied as stdin.
  • Cover both -ok and -okdir.

@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.28571% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.43%. Comparing base (4c49ccd) to head (d6a1028).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
src/find/mod.rs 94.28% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #997      +/-   ##
==========================================
- Coverage   92.43%   92.43%   -0.01%     
==========================================
  Files          38       38              
  Lines        8887     8911      +24     
  Branches      440      443       +3     
==========================================
+ Hits         8215     8237      +22     
- Misses        532      533       +1     
- Partials      140      141       +1     

☔ 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.

Comment thread src/find/mod.rs Outdated
.map_err(|e| format!("cannot open '{}' for reading: {}", name, e))?;

#[cfg(unix)]
if interactive_exec && is_file_stdin(&file)? {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interactive_exec is only used under #[cfg(unix)], so this will warn as an unused variable on windows, no?
could the check move to parse_args, next to the - one?

Comment thread src/find/mod.rs Outdated
#[cfg(unix)]
if interactive_exec && is_file_stdin(&file)? {
return Err(format!("when -ok or -okdir is in use, the option -files0-from cannot also read from standard input, \
but the file ({}) used for -files0-from appears to refer to the same file as the standard input", name).into());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GNU prints the name quoted: ('/dev/stdin'), here it is (/dev/stdin).
please also split this long line, rustfmt does not reflow it inside format!

Comment thread src/find/mod.rs
fn is_file_stdin(file: &std::fs::File) -> std::io::Result<bool> {
let file_metadata = file.metadata()?;
let stdin_metadata = nix::sys::stat::fstat(io::stdin())?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with stdin closed (<&-), this fails with "Bad file descriptor" instead of running.
maybe treat an fstat error as "not stdin"?

Comment thread tests/test_find.rs Outdated
.args(&["-files0-from", "/dev/stdin", action, "echo", "{}", ";"])
.fails_with_code(1)
.no_stdout()
.stderr_contains("standard input");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"standard input" also matches the - message. Please check for the new message here

Comment thread tests/test_find.rs Outdated
.set_stdin(File::open(&list).unwrap())
.fails_with_code(1)
.no_stdout()
.stderr_contains("standard input");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@codspeed

codspeed Bot commented Nov 12, 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 M3M0KA:fix/files0-stdin-alias (d6a1028) with main (0172cc6)

Open in CodSpeed

@M3M0KA

M3M0KA commented Nov 13, 2026

Copy link
Copy Markdown
Author

Thanks for the feedback! I think I've fixed everything now. Let me know if I missed anything :)

@M3M0KA

M3M0KA commented Nov 14, 2026

Copy link
Copy Markdown
Author

Hey @sylvestre, I have no clue why Codecov is still failing, can I get some help on this please?

@M3M0KA

M3M0KA commented Nov 15, 2026

Copy link
Copy Markdown
Author

Update: I did some research and found out that Rust redirects fd 0 to /dev/null when stdin is closed at startup, which explains why the branch isn't covered. How should we proceed from here?

Comment thread src/find/mod.rs
}
#[cfg(unix)]
if name != "-" && config.interactive_exec {
let file = std::fs::File::open(name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the file is opened here and then again in Files0Paths::open, with the same error message copied.
could Files0Paths be built from this File instead?

@M3M0KA

M3M0KA commented Nov 25, 2026

Copy link
Copy Markdown
Author

I hope this approach is fine

Comment thread src/find/mod.rs Outdated
Box::new(BufReader::new(file))
};

let source = if name == "-" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name == "-" is tested a third time here, could source be returned from the branch above with the reader, no?

@M3M0KA

M3M0KA commented Nov 27, 2026 •

Copy link
Copy Markdown
Author

I combined all of the name == "-" checks into a single one, as I think this makes the code a bit cleaner.

@github-actions

Copy link
Copy Markdown

Commit d6a1028 has test result changes:

GNU findutils testsuite:

Test results comparison:
  Current:   TOTAL: 508 / PASSED: 472 / FAILED: 32 / SKIPPED: 4
  Reference: TOTAL: 508 / PASSED: 471 / FAILED: 33 / SKIPPED: 4

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + tests/find/readdir_race

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