Skip to content

fix(executor): bad substitution latched to exit status, first-error-only (Refs #3019) - #3686

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3019-bad-subst-exit-status
Nov 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3019-bad-subst-exit-status

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Problem

${}, ${%%foo}, ${@[-1]}, ${(aO)foo} and the other mvdan/sh bad-substitution shapes printed GNU's byte-identical diagnostic but exited 0; a command carrying two bad substitutions reported BOTH where GNU reports only the first.

Root cause

GNU bad_substitution (subst.c:10273-10288) sets EXECUTION_FAILURE and returns the &expand_wdesc_error sentinel; call_expand_word_internal (subst.c:4288-4296) maps that to exp_jump_to_top_level(DISCARD) — the whole command unwinds before any later word expands, and the failed command seeds status 1 (so a script whose last command it is exits 1, while a mid-script one lets the next command overwrite the status; both probe-verified on WSL bash 5.3.20).

Rubash set the parameter_bad_substitution latch during word expansion, but (a) expand_command_words kept expanding every remaining word — second bad substitution reported — and (b) when all words vanished, the null-command path ran and reset exit status to 0, indistinguishable from the legitimate x=""; $x empty command (rc 0 in GNU too).

Fix

  • command_prepare.rs: the per-word expansion loop stops at the first bad-substitution / no-closing-brace latch — GNU's DISCARD unwinds the command, so later words never expand.
  • command_execute.rs: the all-words-vanished arm now gates on the absence of expansion-error latches, so a failed expansion falls through to abort_on_expansion_errors → ExpansionFailure(1) (DISCARD semantics: script continues; no ERR trap).

Test legs

  • cargo test --test issue3019_bad_substitution_exit_status: 19/19 green (was 2/19 on master — all 17 bad-subst cells + the two-substitutions cell were red on rc/first-report only)
  • GNU probes (WSL bash 5.3.20, script files): echo a${}b → diagnostic + rc 1 script continues; >f ${} → f NOT created + rc 1; x=""; $x → rc 0; "" → 127 — rubash now byte-identical
  • GNU reference: subst.c:10273-10288, subst.c:4288-4296
  • fmt: cargo fmt hit Windows os error 206; used per-file rustfmt --edition 2021 as sanctioned fallback

Generated with Devin

…nly (Refs #3019)

GNU subst.c:10273-10288 bad_substitution sets EXECUTION_FAILURE and
returns the expand_wdesc_error sentinel; call_expand_word_internal
(subst.c:4288-4296) DISCARDs the whole command, so no later word is
ever expanded and the null-command path is never reached.

Rubash printed the diagnostic but then (a) expanded every remaining
word of the command — a second bad substitution reported too — and
(b) let the all-words-vanished command take the legitimate empty-
command path, resetting exit status to 0. Now the per-word expansion
loop stops once a bad-substitution/no-closing-brace latch is set, and
the empty-words arm only runs when no expansion-error latch is set,
so such commands fall through to abort_on_expansion_errors (status 1,
DISCARD semantics: script continues). The `x=""; $x` legit-empty
control stays rc 0; `>f ${}` no longer creates f (GNU probe-verified).

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 merged commit 3559093 into master Nov 15, 2026
8 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 15, 2026
…after unset HISTTIMEFORMAT stay armed

Refs #3535

## 根因

GNU 的 `history_comment_char` 武装是粘性的:`variables.c:6168-6177 sv_histtimefmt` 在 HISTTIMEFORMAT 存在时把 `history_comment_char` 置 `'#'`(仅当其为 0),unset 时只清 `history_write_timestamps`,从不复位注释符。rubash 把武装建模为「HISTTIMEFORMAT 当前存在」(各 history 读取面采样 `get_env("HISTTIMEFORMAT").is_some()` 同时充当 write-timestamps 与 comment-char 武装),于是置过又撤掉之后,`history -r` 把 `#<epoch>` 行当普通条目读入、条目带空戳,列表渲染为 `??` 三行,与 GNU 的两行带戳不符。

已合入的 #3537(#3393)只建模了同样粘性的 `history_multiline_entries`,未动注释符武装——本 PR 补这一面。

## 修法(机制,非个案)

- `ShellState` 新增粘性闩 `history_comment_char_armed`(readline `history_comment_char` 进程全局的移植,随 Clone fork 边界传递);`Executor::new` 以进程环境导入的 HISTTIMEFORMAT 播种(GNU 对环境导入变量同样跑 sv_histtimefmt)。
- 赋值面闩定:`execute_command` 每命令边界在 HISTTIMEFORMAT 存在时闩定;临时(前缀)绑定在 `apply_temporary_assignments` 内闩定(其恢复先于命令边界,GNU 探针证实 `HISTTIMEFORMAT='[%Y] ' true` 同样粘性武装)。
- `SessionHistory` 读取面把两个 GNU 状态拆开传参:`comment_char_armed`(粘性,管 `#<digit>` 识别与读时戳,histfile.c:375-383 + history.c hist_inittime)与 `timestamps_armed`(现值 = history_write_timestamps,仅继续管 multiline 重组门,histfile.c:384——粘性不放宽该门,钉 multigate 格守此界)。

## GNU 锚点

- `variables.c:6168-6177` sv_histtimefmt(武装粘性本体)
- `lib/readline/histfile.c:375-383` + `:466`(文件首行戳仅借用本读、读毕复位——不闩定)
- `lib/readline/history.c:322-333` hist_inittime(条目戳首字 = 当前 comment char)
- `lib/readline/histfile.c:384`(multiline 门 = history_write_timestamps 现值)

期望值全部以 GNU bash 5.3.20 脚本文件实测(repro / 从未武装对照 / 前缀绑定 / 空值绑定 / `history -n` 面 / multiline 门,共 6 形)。

## 回归钉

`tests/issue3535_history_comment_char_sticky.rs`(6 格):武装后撤除的 `-r` 形、从未武装对照组、前缀绑定、空值绑定、`-n` 面、multiline 门不放宽。

## 本 PR 删除了什么

无删除。纯增性改动:新增一个 ShellState 布尔闩与其播种/闩定点、读取面签名增加一个参数(仓内 5 个调用点同步更新)、新增钉文件。`src/script_driver.rs`、`src/executor/init.rs` 的改动仅为上述闩的播种与调用点传参。

## Captain verification (merge round R5, head 84a58c5, base 78060e4)

- Pin `issue3535_history_comment_char_sticky`: 6/6 green on the merged shape (fail-first banked at delivery: 4 red / 2 green on base).
- Neighbors: issue3393 8/8, issue1669 2/2, both aligned with base.
- `RUSTFLAGS="-D warnings" cargo check --locked --lib --bins`: rc=0, zero warnings. `rustfmt --check` on all 9 changed files: rc=0.
- Full lib bilateral (head vs base 78060e4, bin-first): 1249/0/4 both sides, failure name-set diff empty.
- CI on the R5 head (run 43729056059, head == 84a58c5): 3 failing jobs (Rust tests, Rust tests (macOS), GNU regression goldens and canaries), each failing ONLY on the issue2875 baseline cell (`api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell`); niu-canary success. PASS comment 6894417642 stands.
- Intersection (merge-base eaa357f..78060e4 vs the PR's 9 files): exactly 2 files, both released by the captain after personally reading both sides' hunks — `src/executor/command_execute.rs` (tip #3686 @@ -468 and #3690 @@ -600, both in `execute_command_with_procsub_scope`, vs PR @@ -30 in `execute_command`; ~438 / ~570 lines apart, different functions, no text overlap) and `src/executor/temporary_assignments.rs` (tip #3669 @@ -1482 in `apply_shell_assignment` vs PR @@ -186 in `apply_temporary_assignments`; ~1296 lines apart, different functions, no text overlap). Rebase onto 78060e4 was clean, zero conflicts; the rebased diff is byte-identical to the R4 shape except the two base-blob index lines. Tip unmoved after all legs.

Refs #3535 (the issue stays open until the captain hand-closes it on this merge).
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