Repository navigation
Description
Without -D, GNU grep reads FIFOs, sockets and device nodes it finds while walking a tree
under -R (--dereference-recursive), and skips them only under -r. The GNU manual says
for -D: "By default, devices are read if they are on the command line or if the -R
(--dereference-recursive) option is used, and are skipped if they are encountered
recursively and the -r (--recursive) option is used." uutils grep skips them under both
-r and -R, so grep -R misses lines in a FIFO and does not report an unreadable socket.
Reproduction
A tree with a regular file and a Unix socket:
$ mkdir tree && echo hit > tree/a.txt
$ python3 -c 'import socket; socket.socket(socket.AF_UNIX).bind("tree/sock")'Rust (incorrect)
$ ./target/release/grep -R hit tree
tree/a.txt:hit
# Exit code: 0GNU (correct)
$ LC_ALL=C /usr/bin/grep -R hit tree
/usr/bin/grep: tree/sock: No such device or address
tree/a.txt:hit
# Exit code: 2With -r both print only tree/a.txt:hit and exit 0. With a FIFO tree/ff that some
process writes hit into, GNU -R also prints tree/ff:hit, uutils does not.
Root cause
process_dir in src/searcher.rs:185-188 skips every non-regular file unless -D read
was given:
// Skip non-regular files unless `-D read` was given explicitly.
if !file_type.is_file() && self.config.device_mode != DeviceMode::Read {
continue;
}It does not take -R (self.config.follow_symlinks) into account.
Test
A socket avoids the blocking that a FIFO would cause:
#[cfg(unix)]
#[test]
fn dereference_recursive_reads_special_files() {
use std::os::unix::net::UnixListener;
let (scene, _) = ucmd();
scene.fixtures.mkdir_all("tree");
scene.fixtures.write("tree/a.txt", "grep me\n");
let _sock = UnixListener::bind(scene.fixtures.plus("tree/sock")).unwrap();
// -R opens the socket, which fails like GNU.
let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));
c.args(&["-R", "grep", "tree"])
.fails_with_code(2)
.stdout_is("tree/a.txt:grep me\n")
.stderr_contains("tree/sock");
// -r still skips it.
let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));
c.args(&["-r", "grep", "tree"])
.succeeds()
.stdout_only("tree/a.txt:grep me\n");
}Reported by Anthropic's OSS Scanner (ANT-2026-V28VLYAJ).
Activity