Skip to content

fix(head): /dev/stdin-family operands read the real fd 0 like cat/wc (#1452) - #1456

Merged
caomengxuan666 merged 1 commit into
mainfrom
fix/1452-head-devstdin
Oct 30, 2026
Merged

caomengxuan666 merged 1 commit into
mainfrom
fix/1452-head-devstdin

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Problem

head -N /dev/stdin on a true anonymous pipe silently printed nothing and exited 0, while cat /dev/stdin and wc -l /dev/stdin on the same pipe with the same argv worked — head-specific (#1452).

Root cause

head never routed /dev/stdin to the stdin stream: the operand went through open_binary_file(), which correctly binds fd-0 pseudo-devices to the real fd 0 but as a std::ifstream over a dup'd FILE* (src/utils/file_io.cppm:208-216). The file branch then ran the seekable-file decoder probe stream_needs_text_decoding on it (src/commands/head.cpp:852):

  • on a pipe, MSVC tellg() returns a buffer-computed 0 (not -1), so the probe reads 4096 bytes and rewinds with in.seekg(original) (src/commands/head.cpp:207-231);
  • the rewind fails on a pipe (ESPIPE), seekg sets failbit, and the probe bytes are lost;
  • every later read loop gates on in.good() → nothing is read; failbit != badbit, so no diagnostic — silent zero output, exit 0.

Differential proof pre-fix: lines-form on a pipe broken while the -c bytes form (which skips the probe), file-redirect form (seekable), head -3 -, cat /dev/stdin and wc -l /dev/stdin (all read std::cin, never seek) were all correct — exactly the reported head-specific face.

Fix

Route - and fd-0 pseudo-device operands (/dev/stdin, /dev/fd/0, /proc/self/fd/0, /proc/<pid>/fd/0) into the existing stdin branch, the same shape cat (cat.cpp:406-421) and wc (wc.cpp:449-454) already use for #1056:

  • closed stdin: - keeps head: error reading 'standard input': Bad file descriptor ([gap] tee/cat 等在 stdin 被关闭(<&-)时静默成功,GNU 报 Bad file descriptor (uutils#9795) #973); /dev/stdin-family reports GNU's dangling-symlink ENOENT — head: cannot open '/dev/stdin' for reading: No such file or directory (GNU-verified text, rc=1);
  • open stdin: set_stdin_binary() + output_head(std::cin, config) — no seek anywhere, and because &in == &std::cin the live-pipe ReadFile fast path still applies: yes | head -3 /dev/stdin exits after 3 lines (measured 0.03 s).

No new message shapes (both diagnostic forms already exist in head.cpp), no I18N catalog impact.

Verification (GNU oracle: WSL GNU bash 5.3.20 + coreutils head, script-file probes)

cell GNU before after
cat f | head -3 /dev/stdin (pipe) a01 a02 a03, rc=0 empty, rc=0 a01 a02 a03, rc=0
seq 1 100 | head -5 /dev/stdin 1..5 empty 1..5
head /dev/stdin (pipe) 10 lines empty 10 lines
head -c 10 /dev/stdin (pipe) a01\na02\na0 correct unchanged
head -2 /dev/stdin < f a01 a02 correct unchanged
head -3 /dev/fd/0, /proc/self/fd/0 (pipe) a01 a02 a03 empty a01 a02 a03
head -3 -n -3 /dev/stdin (pipe) 7 lines empty 7 lines
cat f | cat /dev/stdin, wc -l /dev/stdin correct correct unchanged
-v header ==> /dev/stdin <== — identical
yes | head -3 /dev/stdin 3 lines, prompt exit — 3 lines, 0.03 s

Tests

  • New differential regression tests/differential/regressions/71-head-dev-stdin-pipe.case — the runner pipes stdin on both sides (printf '%b' "$stdin" | bash -c "exec <subject> …"), so it exercises the true pipe face. Discrimination proven: OUT_DIFF on a pre-fix shipped build, PASS here.
  • runner.sh --corpus regressions: 71 cases → 65 PASS, 6 KNOWN_DIFF (whitelisted, unrelated commands), 0 unexpected.
  • Unit tests: ^head\. 38/38 PASS; head/cat/wc-related filter 108/108 PASS (cat|grep|sed .nul_txt_reads_file_issue1333 fails locally on pristine main too — pre-existing, environment-dependent, unrelated).
  • Builds: CMake Ninja Release, with and without PCRE2 (pcre2-10.47 tarball verified against the sha256 pinned in CMakeLists.txt).

Fixes #1452

…1452)

head opened /dev/stdin through open_binary_file()'s dup'd-FILE* ifstream and
then ran the seekable-file decoder probe (stream_needs_text_decoding) on it.
On a pipe the probe's seek-back fails, sets failbit, and discards the probe
bytes, so every later read yielded nothing: head silently printed zero lines
while exiting 0 (cat/wc read std::cin directly and were unaffected).

Route "-" and fd-0 pseudo-device operands (/dev/stdin, /dev/fd/0,
/proc/self/fd/0, /proc/<pid>/fd/0) into the existing stdin branch: closed
stdin keeps the "-" EBADF diagnostic (#973) while the /dev/stdin family
reports GNU's dangling-symlink ENOENT (#1056), and open stdin is read via
std::cin so the live-pipe fast path still exits after N lines.

Verified against WSL GNU coreutils: the pipe face now prints exactly N lines
(-n/-c/default/-n -N, /dev/fd/0, /proc/self/fd/0, -v header), redirect and
byte-mode faces are unchanged, and `yes | head -3 /dev/stdin` still exits
promptly. New differential regression regressions/71-head-dev-stdin-pipe.case
fails on pre-fix builds and passes here.

Fixes #1452
@caomengxuan666
caomengxuan666 merged commit dd1ec0e into main Oct 30, 2026
8 of 9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/1452-head-devstdin branch November 2, 2026 01:13
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] head -N /dev/stdin 在真匿名管道上静默零输出、exit 0(cat/wc 同面正常——head 专有;#1442 follow-up 证据齐)

1 participant