Skip to content

WIP: fix(parser): heredoc io_number requires adjacency — spaced 3 <<X keeps its argument (Refs #2467) - #2468

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2467-heredoc-fd-spacing
Nov 1, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2467-heredoc-fd-spacing

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2467

根因

Parser 侧,不是词法侧。src/parser/token_actions.rs 的 TokenKind::HereDoc 臂是全仓唯一不查相邻性的重定向臂:

let fd = redirect_operator_fd(&token.value)
    .or_else(|| take_heredoc_fd_prefix(&mut state.current_cmd));

take_heredoc_fd_prefix → take_redirect_fd_prefix(src/parser/redirections.rs)无条件弹出命令的最后一个词、只要它全是数字。于是 echo 3 <<X 在处理 << 时把参数 3 弹走当作 fd 3,head -n 3 <<EOF 则丢了 -n 的参数直接报错。其余每个重定向臂(同文件的 RedirectIn 臂、redirections.rs 的整条 assign 路径含它自己的 HereDoc 臂)都走带 column 相邻性检查的 take_adjacent_redirect_fd_prefix。

词法侧经核查均无问题:number_redirect.rs 与 scanner.rs::finish_prefixed_input_redirect 只在数字后紧贴 </> 时融合;alias_stream.rs 的数字词规则要求词尾下一字节即 </>。

GNU 依据

  • parse.y read_token_word()(got_token 裁决):全数字 token 仅当终止它的字符是 </> 时才是 NUMBER;空格终止即 WORD。
  • parse.y:629 语法 NUMBER LESS_LESS WORD:io_number 形态只在融合拼写 3<<X 时成立。
  • token 模型里「被 < 终止」⇔ 数字词 column 终点 == 操作符 column,正是相邻性检查所测。

修复

  • HereDoc 臂改走 take_adjacent_redirect_fd_prefix(与全部兄弟臂一致),附 GNU 裁决注释。
  • 删除失去唯一调用者的 take_heredoc_fd_prefix wrapper。
  • 融合形态走 redirect_operator_fd 在 or_else 之前命中,逐字节不变。src/lexer/continuation.rs 未触及。

探针(脚本文件;GNU = 5.3.20 oracle)

例 GNU 修复前 修复后
head -n 3 <<EOF(a/b/c/d) a b c rc=0 head: option requires an argument -- 'n' rc=1 == GNU
echo 3 <<X 3 空行(3 被吞) == GNU
echo 42 <<X / echo 3 <<-X / echo 3 <<'X' / 管道形 42/3/3/3 同被吞 == GNU
阴性:echo 3<<Y、head -n 3 < /dev/null、echo 3 > out.txt、head -n 3 <<< ignored、cat <&3 3<<X、exec 3<<X / exec 3<<-X 各如期 与 GNU 一致 == GNU(不变)

电池(scrubbed fd 环境,scrubbed_cargo.py)

引擎侧修复同时覆盖 niubash #791(同一 bug 的 niu 跟踪单)。

The HereDoc arm of handle_token derived the heredoc fd through
take_heredoc_fd_prefix, which popped the command's last word whenever
it was all digits — with no adjacency requirement. Every other
redirection arm (and the second HereDoc arm in redirections.rs) goes
through take_adjacent_redirect_fd_prefix, which requires the digit
word to END exactly at the operator token.

GNU parse.y read_token_word() (got_token): an all-digit token is a
NUMBER only when the character that ended it is `<` or `>`; a
whitespace-terminated digit token is a plain WORD, and the grammar's
NUMBER LESS_LESS WORD (parse.y:629) engages only for the fused
spelling. So `echo 3 <<X` keeps `3` as an argument and the heredoc
lands on fd 0, while fused `3<<X` keeps working through
redirect_operator_fd.

Route the arm through take_adjacent_redirect_fd_prefix and drop the
now-unused unchecked wrapper. The fused forms are byte-unchanged
(negative controls in tests/issue2467_heredoc_fd_spacing.rs).
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 1, 2026 13:29
@caomengxuan666
caomengxuan666 merged commit fa34dbf into master Nov 1, 2026
13 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2467-heredoc-fd-spacing branch November 2, 2026 01:09
caomengxuan666 added a commit that referenced this pull request Nov 8, 2026
`tests/cat_redirect_stream_regressions.rs::shell_bin()` trusted
`CARGO_BIN_EXE_bash` unconditionally, but the `bash` bin is a
Windows-only shim (required-features windows-shim; Cargo.toml: "Unix
builds never produce it") — cargo still sets the env var at compile
time on Linux, so both tests fail with spawn NotFound on the
never-built `target/debug/bash`. Found while running the full
`cargo test` for rubash #2885 (the failure is pre-existing and
unrelated to this lane: the test file is untouched since #2468, and a
fresh single-target run fails identically).

Fix, test-only: take the bash path only when the file exists, else
fall through to rubash (the existing branch 2). Behavior-preserving
on Windows (the shim exists there, so it is still taken) and makes
the full suite green on Linux. CI never runs the full integration
suite (ci.yml invokes only --lib and --test regression), which is
why this survived on master.
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