Skip to content

test: pin read on write-only fd 0 must diagnose EBADF (Refs #3770) - #3773

Merged
caomengxuan666 merged 4 commits into
masterfrom
fix/3770-read-writeonly-fd0
Nov 16, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
fix/3770-read-writeonly-fd0

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3770

Pin only for now; the fix follows in a second commit.

Root cause (code-read, to be confirmed by the fix): after exec 0<<A 0>out.txt fd 0 is open but write-only (#3655 retires the heredoc read arm). GNU reports the hard read failure (builtins/read.def:911 — read: 0: read error: Bad file descriptor); rubash fails with the same rc and empty x but prints no diagnostic — the read transports return None without recording a hard error, so the builtin falls through to the silent-EOF arm.

Pin: tests/issue3770_read_writeonly_fd0.rs — 8 cells (fd-0 prototype with the full expectation of #3768's relaxed J cell, read -u 14 control, read -u 0, own-redirect, reverse-order, <> pair, closed-fd-0, EOF-silence). Every expectation byte-verified against GNU bash 5.3.20 (oracle probes in the lane workdir).

本 PR 删除了什么

Nothing yet (pin-only commit).

UNVERIFIED-BY-CAPTAIN

Pin only; the fix follows. The fd-0 cell is the full expectation
of #3768's relaxed J cell (ordering + diagnostic); the -u 14,
closed-fd-0, reverse-order, <>-pair and EOF cells are controls.

UNVERIFIED-BY-CAPTAIN
Formatting only (cargo fmt on the new stem).

UNVERIFIED-BY-CAPTAIN
…ly (Refs #3770)

GNU builtins/read.def:911 — a hard read(2) failure is reported
as `read: <fd>: read error: <strerror>` and the builtin fails;
it is not plain EOF. After `exec 0<<A 0>out.txt` fd 0 is open
but write-only (#3655 retires the heredoc read arm), and the
plain-`read` path reached the transports with no availability
gate (the -u arm diagnoses before the transports; plain read
never calls read_fd_is_available), so every transport returned
None and the builtin fell through to the silent-EOF arm.

Fix (mechanism): read_virtual_fd_stdin records a hard read
error when the table owns an open descriptor with no read arm —
the same read_error channel the EISDIR endpoint (#1495) and
hard read failures in read_file_bytes use, rendered by the read
builtin's existing error arm. `is_open` without
`is_open_for_read` is exactly open-but-not-readable: an entry
with no read arm at all (a pipeline stage's fd 0) has is_open
false, so its transports still own it, and a present-but-
exhausted read arm stays a real silent EOF.

Pin: tests/issue3770_read_writeonly_fd0.rs (8 cells; fd-0 cell
is the full expectation of #3768's relaxed J cell).

UNVERIFIED-BY-CAPTAIN
…read fd write-only (Refs #3774)

`read x 0>out2.txt` failed silently: execute_read never binds the
command's own redirects into the fd table (builtin_redirects.rs), so
the read funnel saw only fd 0's ambient readable process stdin and
read it to a silent EOF. GNU applies the redirect list in order
before the builtin runs (redir.c:237 do_redirections), so the
descriptor is open but write-only and builtin_read's read(2) fails
EBADF (builtins/read.def:911: builtin_error "%d: %s: %s", fd,
"read error", strerror(errno); EXECUTION_FAILURE, not EOF).

- read_redirected_fd.rs: own_list_leaves_fd_write_only — the
  output-side mirror of own_input_binding_scan (#2305). The last
  entry naming the fd across kinds wins; an Output / Append /
  ClobberOutput winner means write-only. Input-side winners stay
  readable (`0>o 0<i` reads the file, `0<i 0>o` diagnoses), and
  dup/close winners keep their existing serves.
- read_io.rs: the funnel judges the own-list winner before any
  serve and records read_error "Bad file descriptor", which the
  builtin renders through the existing read.def:911 arm.
- read_builtin.rs: read_fd_is_available counts an own output
  winner as available — sh_validfd judges the REDIRECTED state
  (read.def:377-381), so `read -u 3 x 3>file` reaches the funnel's
  read error instead of reporting an open fd as invalid.

Pin: tests/issue3770_read_writeonly_fd0.rs cell
issue_own_output_redirect_diagnoses (already on this branch) is the
target; the other seven cells must stay green.

This change deletes nothing; it adds one scan, one funnel gate and
one availability arm, all in the read path.

Refs #3774, Refs #3770.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 10:32
@caomengxuan666
caomengxuan666 merged commit c239f91 into master Nov 16, 2026
8 of 12 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

队长终审 PASS 并合入(merge c239f91)。

复核要点:机制与 GNU 锚点(read.def:911 / 377-381、redir.c:237)亲读一致;钉 fail-first 实测 5/8 → 舊头 7/8 → 终头 8/8(c4 格由 #3774 车道补齐,含 -u 形误报 invalid file descriptor 的同根附带面);leg 2b 已补(-D warnings rc=0、issue1477/issue1592 邻居绿)。CI 四红腿逐一按名:Linux/macOS/goldens 唯一红 issue2875 基线格;census 为 fail-loud 既存红集,本钉在 census 内 8/8 PASS。与合入时尖 d3f1cce 零交集已程序化核验。#3770、#3774 随合入自动关闭。残差 #3777(Windows 目录打开层诊断)另行归类。

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.

1 participant