Repository navigation
fix(head): /dev/stdin-family operands read the real fd 0 like cat/wc (#1452) - #1456
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
head -N /dev/stdinon a true anonymous pipe silently printed nothing and exited 0, whilecat /dev/stdinandwc -l /dev/stdinon the same pipe with the same argv worked — head-specific (#1452).Root cause
head never routed
/dev/stdinto the stdin stream: the operand went throughopen_binary_file(), which correctly binds fd-0 pseudo-devices to the real fd 0 but as astd::ifstreamover a dup'dFILE*(src/utils/file_io.cppm:208-216). The file branch then ran the seekable-file decoder probestream_needs_text_decodingon it (src/commands/head.cpp:852):tellg()returns a buffer-computed0(not-1), so the probe reads 4096 bytes and rewinds within.seekg(original)(src/commands/head.cpp:207-231);ESPIPE),seekgsetsfailbit, and the probe bytes are lost;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
-cbytes form (which skips the probe), file-redirect form (seekable),head -3 -,cat /dev/stdinandwc -l /dev/stdin(all readstd::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:-keepshead: 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);set_stdin_binary()+output_head(std::cin, config)— no seek anywhere, and because&in == &std::cinthe live-pipeReadFilefast path still applies:yes | head -3 /dev/stdinexits 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)
cat f | head -3 /dev/stdin(pipe)seq 1 100 | head -5 /dev/stdinhead /dev/stdin(pipe)head -c 10 /dev/stdin(pipe)a01\na02\na0head -2 /dev/stdin < fhead -3 /dev/fd/0,/proc/self/fd/0(pipe)head -3 -n -3 /dev/stdin(pipe)cat f | cat /dev/stdin,wc -l /dev/stdin-vheader==> /dev/stdin <==yes | head -3 /dev/stdinTests
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.^head\.38/38 PASS; head/cat/wc-related filter 108/108 PASS (cat|grep|sed .nul_txt_reads_file_issue1333fails locally on pristine main too — pre-existing, environment-dependent, unrelated).Fixes #1452