Skip to content

fix(trap): verbatim trap-action source echo (Refs #3328) - #3483

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3328-parse-source-span-fidelity
Nov 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3328-parse-source-span-fidelity

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

What

Trap-action source echo lines are the parser's re-serialized text instead of
the action's own line: trap 'echo inaction; echo (' EXIT reports
`echo inaction ; echo (' where GNU reports `echo inaction; echo ('.
The stored action string itself is verbatim (trap -p proves it) — only the
diagnostic echo was re-serialized.

Root cause

The six trap-action re-parse sites in src/executor/trap_exec.rs
(run_exit_trap_action, run_debug_trap, run_return_trap,
run_pending_signal_traps, maybe_run_error_trap_inner,
run_sigchld_trap_for_reaped_child) re-parsed the action with the bare
parse(), which carries no source text, so the stored
__RUBASH_PARSE_SOURCE__ fell back to the token-join reconstruction in
offending_line_text (src/parser/parse_loop.rs:3416).

Fix

Thread the action string as ParseLoopOptions::source_text at those six
sites — the same shape the eval/comsub re-parses already use. No parser file
touched, no other option changed (stray_close_is_error stays false,
source_line_offset stays 0). Full census of __RUBASH_PARSE_SOURCE__
producers/consumers and the no-source re-parse sites is in
fix-3328/REPORT.md §2 (coproc None sites and the alias-retry
PARSE_SOURCE_SPAN__ path deliberately left alone: #3267 guardrail and a
separate carrier).

GNU anchors

Vendored bash-5.3 tree, verified line by line:

  • signal traps: trap.c:496 parse_and_execute (trap_command, "trap", pflags)
  • EXIT trap: trap.c:1071 parse_and_execute (trap_command, "exit trap", pflags)
  • DEBUG/RETURN/ERR: _run_trap_internal (trap.c:1109) →
    trap.c:1200 parse_and_execute (trap_command, tag, flags)
  • diagnostic: error.c:300 parser_error; verbatim echo:
    parse.y:6814 print_offending_line

Tests

New tests/issue3328_parse_source_span_fidelity.rs (8 pins: EXIT / DEBUG /
RETURN / ERR / USR1 / CHLD, a second spacing shape echo a;echo(, and an eval
control). Pins assert only the echo line (verbatim present, re-serialized form
absent). The missing trap-context infix and the rc=2 abort on the
DEBUG/RETURN/ERR/CHLD faces are adjacent divergences left for their own lanes.

本 PR 删除了什么

无删除。本 PR 是纯加法:六处 parse() → parse_with_options + source_text
(+67/−6 行,均为同一调用点的改写),加一个新测试文件。无公开面、无行为面
变更,除 trap 动作解析失败时回显行文本本身。

UNVERIFIED-BY-CAPTAIN

Not yet compiled or run — no build slot this round (local build banned, box
disk full, old server ssh timeout). CI first check and the captain review round
must run the new stem, the full lib/base alignment,
RUSTFLAGS="-D warnings" cargo check --locked --lib --bins, and the GNU 5.3.20
oracle comparison (local 5.2.21 was reference only). Note: the X299 direct
lane key was installed during this round (MEMORY.md 2026-11-13 08:2x), so the
review round can run build legs via X299 direct instead of the full box.

Refs #3328

Root cause: the six trap-action re-parse sites in
src/executor/trap_exec.rs (run_exit_trap_action, run_debug_trap,
run_return_trap, run_pending_signal_traps, maybe_run_error_trap_inner,
run_sigchld_trap_for_reaped_child) re-parsed the action with the bare
parse(), which carries no source text, so the stored
__RUBASH_PARSE_SOURCE__ fell back to the token-join reconstruction
(`echo inaction ; echo (` for `echo inaction; echo (`). The stored
action string itself is verbatim (trap -p proves it); only the
diagnostic echo was re-serialized.

Fix: thread the action string as ParseLoopOptions::source_text at
those six sites — the same shape the eval/comsub re-parses already
use. No parser file touched, no other option changed
(stray_close_is_error stays false, source_line_offset stays 0).

GNU anchors (vendored bash-5.3): trap.c:496 (signal traps,
parse_and_execute "trap"), trap.c:1071 (EXIT trap, "exit trap"),
trap.c:1109-1200 (_run_trap_internal, DEBUG/RETURN/ERR tags),
error.c:300 (parser_error), parse.y:6814 (print_offending_line
echoes the input line verbatim).

Tests: new tests/issue3328_parse_source_span_fidelity.rs (8 pins:
EXIT/DEBUG/RETURN/ERR/USR1/CHLD, a second spacing shape, and an
eval control). Pins assert only the echo line (verbatim present,
re-serialized form absent); the missing trap-context infix and the
rc=2 abort on the DEBUG/RETURN/ERR/CHLD faces are adjacent
divergences left for their own lanes.

UNVERIFIED-BY-CAPTAIN: not yet compiled or run — no build slot this
round (local build banned, box disk full, old server ssh timeout);
CI first check and the captain review round must run the new stem,
the full lib/base alignment, RUSTFLAGS="-D warnings" check, and the
GNU 5.3.20 oracle comparison (local 5.2.21 was reference only).

Refs #3328
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3483 第1轮 — 建议:PASS
head: 33037b8 base(merge-base): 3745712 文件: 2 个 +214/-6
关联 issue: #3328(状态:open,我核过的时间:2026-11-15 本地)

机制核查:

  • GNU trap 动作经 parse_and_execute 以动作字符串为解析输入,parser_error 回显动作原文(trap.c:496 信号面、trap.c:1071 EXIT、trap.c:1109+ _run_trap_internal;回显走 error.c:300-316 的 offending-line 打印)。diff 把 6 处 trap-action 重解析点由 bare parse(&tokens)(其诊断回显走 token-join 重序列化,产生 echo inaction ; echo ()改为 parse_with_options + source_text=动作原文——面覆盖 EXIT、普通信号(USR1/CHLD)、RETURN、ERR、DEBUG 及缓存动作两处路径。唯一剩余 bare parse(&tokens)(trap_exec.rs:10146)在 debugger_evaluate(bashdb EvaluationRequest 自有诊断通道),非 trap 执行面,正确地未动。机制正确,单文件机械改动,无无关面。

腿(逐项):

  • 钉自跑:issue3328_parse_source_span_fidelity 8 passed / 0 failed
  • fail-first:merge-base 同钉 7 failed / 1 passed(早轮已跑),红集 = 6 trap 面 + spacing 变体;绿对照 = eval——与 PR 声称一致
  • lib 双边(Windows):head {issue2084_session_end_restores_host_process_state}(1349/1/4)= base → 差集 ∅
  • -D warnings:lib+bins 0 警告
  • 卫生:rustfmt --check clean;marker-lint OK(117/268);utf8-boundary-hygiene clean
  • CI:3 红全部 issue2875_input_procsub_child_runs_as_rubash_shell=基线(GNU goldens / Rust Linux / Rust macOS 各一)
  • GNU 对照:wsl /usr/local/bin/bash 5.3.20(1)-release 脚本探针(target/devin-w8-audit/probe3328.sh):EXIT/DEBUG/RETURN/ERR/USR1/CHLD 六面均回显 echo inaction; echo (' 原文;spacing 面回显 echo a;echo('(双空格保留)——与钉断言逐字节一致。注记:eval 对照 GNU 只报 syntax error、不回显源行,rubash 的 eval 诊断本就有回显(超集行为、非本 PR 面),该对照格语义是「若回显则须原文」,与本修复不变量一致

F 点:无。

续作记录:无。

未跑/VOID 的腿:无。

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