Repository navigation
fix: four P1-high semantic divergences - errexit body adjudication, stdin parse-error status, printf write-failure status, dirstack EX_USAGE - #553
Merged
Conversation
…djudication per failure set -e; if true; then false && true; fi; echo R killed the whole shell rc=1 (GNU prints R, rc 0). The top-level walker re-adjudicated the compound's aggregate status: the inner exempt failure (an AND-OR short-circuit arm, POSIX XCU 2.14, or a ! pipeline) had already had its own errexit/ERR look in the nested walk, so the second look at the inherited status fired where GNU's execute_if_command (execute_cmd.c 5200 family) hands the body status up with no errexit check of its own. command_status_produced_by_body() gates BOTH consumers at the post-execute_command site (the ERR-trap fire rubash#344 ordered, and the errexit promotion) for the in-process pass-through kinds: if/elif/else, while/until, for (word + arithmetic), case, select. Fresh statuses keep adjudicating: subshell aggregates (fork boundary - (false && true) exits 1), [[ ]]/(( )) (the innermost failing command), function calls (the call's status belongs to it), a compound's own failed redirection. Tests tests/issue507_errexit_body_andor_regressions.rs: construct matrix (if/elif/else, for, while, until, case, !), aggregate-status preservation, the ERR consumer, and the safety half - plain inner failures, subshell, function call, final AND-OR arm, [[ ]], (( )), failed redirect still exit 1. 4/7 red on master, green here; GNU oracle WSL 5.3.0 2026-10-12.
…atus to the executor
printf 'echo "${x:-' | rubash reported the GNU diagnostic but exited 0
(GNU exits 2, error.h EX_SYNTAXERR); the identical file-driven run
already exited 2. The read-time parse-error fast paths in run_source*
return the failure status without touching executor state, and
run_stdin_script discarded the returned group status - the final exit
read the stale pre-error last_exit_code. The file driver returns
run_source's status directly, which is why only the stdin path lost it
(F120/F123/F127/F128/F139, five fragments, one root).
run_stdin_script now commits each gathered group's status via
set_last_exit_code before consulting the parse-error flag, at both
group-execution sites - GNU evalstring.c parse_and_execute leaves the
group status in last_command_exit_value for the reader to exit with.
This also carries #516 F181's rc half (its diagnostic-shape divergence
stays that issue's scope).
Tests tests/issue508_stdin_parse_error_status_regressions.rs: the five
issue fragments exit 2 via stdin, the F181 rc half, mid-script syntax
error stops reading with rc 2, ordinary status flow unchanged (exit 5,
DISCARD arithmetic failure then continue, failed group rc 1),
file-driven contract locked. 3/5 red on master, green here; GNU oracle
WSL 5.3.0 2026-10-12.
… merge exec >&-; printf "end\n" reported GNU's diagnostic (`printf: write error: Bad file descriptor`) but exited 0; GNU exits 1 (builtins/printf.def printf_builtin -> sh_chkwrite, builtins/common.c 320-334, folds the failed flush into the builtin's status). echo already returned 1; printf's wrapper returned the builtin's nominal status, which the dispatch site's `self.exit_code = self.execute_printf(cmd)?` used to overwrite the failure status the output router had recorded (route_builtin_buffered_output sets exit_code=1 and raises last_builtin_write_failed; the flag had no reader on the printf path). execute_printf now merges take_builtin_write_failed() into the returned status - the same merge execute_pwd already applies - so `||` sees the failure (exec >&-; printf x || exit 7 -> 7) and errexit promotes it (set -e; exec >&-; printf x exits 1). No output still means no write and no failure (exec >&-; printf "" -> 0, GNU-verified). Tests tests/issue510_printf_write_error_status_regressions.rs: repro rc 1 with diagnostic, the || handler, the errexit promotion, the empty-output no-failure case, and ordinary statuses untouched. 2/5 red on master, green here; GNU oracle WSL 5.3.0 2026-10-12.
…a plain failure
dirs +x / dirs -Q / popd +x / pushd +q exited 1 where GNU exits 2
(builtins/common.c get_numeric_arg -> builtin_error("%s: invalid number")
+ return EX_USAGE), and dirs -l +q was worst: the flag branch ignored the
operand entirely, printed the stack and exited 0 where GNU exits 2 with
`dirs: +q: invalid number`.
- dirs: the display flags are now walked off the front (they combine,
`dirs -l -v`, GNU dirs.def) and the first remaining word is validated:
a +-shaped non-number is `invalid number` + usage -> 2; any other word
is `invalid option` + usage -> 2; a valid +-N displays that entry
(bare, or ` N dir` under -v). Replaces dirs_index_or_error, which
only recognized a leading valid index and let everything after the
first flag slide. `-Q` now takes the get_numeric_arg path like GNU
(`invalid number`, matching GNU's message for it) instead of the
pre-flag `invalid number` arm with status 1.
- pushd: a +-shaped operand that is not a number is `invalid number` +
usage (it used to fall through to the Dir arm and fail as
`No such file or directory`); `pushd -n +q` validates after the -n.
- popd: a +-shaped non-number is `invalid number` (was `invalid
argument`); a bare non-word stays `invalid argument` - GNU rc 2 for
both.
- the parse-failure status is EX_USAGE (2) for all three builtins.
Valid forms are untouched: dirs/-l/-p/-v/-c, dirs +0, dirs -v +0 (and
the out-of-range index stays a plain failure 1, GNU rc 1), pushd/popd
round trips.
Tests tests/issue509_dirstack_usage_status_regressions.rs: the issue
table (8 shapes, rc 2 + diagnostic + usage line), the silent-pass leak,
and the valid-form preservation set. 2/3 red on master, green here;
GNU oracle WSL 5.3.0 2026-10-12.
Companion expected-value updates: tests/executor_command_chaining/
part_025.rs test_dirs_redirects_stderr / test_dirs_appends_stderr pinned
the pre-fix status 1 for `dirs bad`; GNU's EX_USAGE verdict is 2, so
their pinned expectations move with the fix (messages unchanged).
caomengxuan666
added a commit
that referenced
this pull request
Oct 12, 2026
… - the #507 body-exemption gate was type-blind (#557) PR #553's #507 pass-through gate (command_status_produced_by_body) exempted if/loop/for/case/select compounds from the post-execute ERR adjudication, and the exemption leaked one flavor it must not cover: the compound's OWN failed redirection. redir12.sub's `trap 'echo got error ERR' ERR; while ...; done </dev/null > unwritable-file` stopped printing "got error ERR", so upstream_suite_slice_snapshots went red on master (CI run 38195839266) and every PR was blocked. The wrapper's open() failure surfaces as ExecuteError::IoError; the walker's IoError arm falls through to the same gated ERR site the #507 commit added its skip at, so the trap went silent for exactly the kinds whose redirects the wrapper owns. Semantics (GNU oracle, vendored bash-5.3-16-gb4608166): GNU runs the ERR trap and the errexit jump at the compound level when the compound's own redirection fails (execute_cmd.c:849-884 - the "WHILE FOR CASE etc. with redirections" path latches was_error_trap before dispatch, sets the failure status, runs the trap, then exits under errexit; the upstream redir.right expectation pins the behavior as "got error ERR" + "after ERR trap: 1"). That path is ORTHOGONAL to the one #507 fixed: an exempt inner failure (a `&&`/`||` short-circuit arm or a `!` pipeline) reaches the compound as a body-inherited AGGREGATE with no wrapper failure, and GNU's execute_if_command family still hands that status up with no ERR/errexit look of its own. One gate keyed on node kind cannot see the difference - this restores the adjudication for the wrapper-failure flavor only. Fix: in the walker's ExecuteError::IoError arm, fire maybe_run_error_trap when the failed node is a pass-through compound kind. maybe_run_error_trap's own gates (inverted, &&/|| context, suppress, errtrace depth, recursion) keep every #507 exclusion, and the gated post-site stays skipped for these kinds, so nothing double-fires. GNU cross-check (WSL 5.3.0, stdout byte-equal after the fix): the repro prints `got error ERR` + `after ERR trap: 1`; #507's guard `set -e; if true; then false && true; fi; echo R` still prints R with rc 0; `set -e; trap 'echo E' ERR; false` still prints E and exits 1. No golden or snapshot bytes touched: redir.snap passes unchanged against the recorded baseline. Tests: cargo test --test regression upstream_suite_slice_snapshots green; full serial cargo test --no-fail-fast vs pristine master (f33c5e3), failure sets compared name-by-name bidirectionally - the only delta is upstream_suite_slice_snapshots (red on master, green here), no other test flips either way; #553's 20 new tests (issue507 x7, issue508 x5, issue509 x3, issue510 x5) and the part_025 dirs companions stay green; cargo fmt clean.
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.
Four P1-high semantic bugs from the huck-corpus health check, one commit each, every fix with its own minimal repro suite and GNU oracle evidence (WSL GNU bash 5.3.0, 2026-10-12, stdin-driven
printf '%s' "$s" | shell | tr -d '\0').全量
cargo test --no-fail-fast失败目标集与 fresh master(1245798b)逐名双向一致(详见各节与文末 parity 说明)。cargo fmt --check干净(pre-commit 钩子亦逐 commit 强制)。#507 — errexit: 豁免的内层失败被复合命令体二次裁决(dd2486af)
现象:
set -e; if true; then false && true; fi; echo R→ rubash 整壳退出 rc=1 无输出;GNU 打印Rrc=0。根因:顶层 walker 在
execute_command返回后对命令的聚合状态做 errexit 提升,而 if/for/while/case/select 的体就在同一 walker 里跑过——内层豁免失败(AND-OR 短路臂 /!管道,POSIX XCU 2.14)已在嵌套行走中获得自己的裁决机会;能活着从复合命令返回的非零聚合状态必然来自豁免失败。二次裁决即 huck#676 的同一错误形态("answered for ERR and not for errexit… reasoned from an OUTCOME rather than a mechanism")。修法:
command_status_produced_by_body()(if/while/until/for/case/select)同时 gate 两个消费者——ERR trap 触发与 errexit 提升(GNU execute_cmd.c:5200 族把体状态原样上交,不做自己的 errexit 检查;一次失败一次裁决,在产生它的位置)。保持裁决的"新鲜状态":子 shell 聚合(fork 边界,(false && true)仍 rc=1)、[[ ]]/(( ))(自身即最内失败命令)、函数调用(调用的状态属于调用,f(){ false && true; }; f仍 rc=1)、复合命令自身的重定向失败({ :; } > /nonexistent/x仍 rc=1)。测试(tests/issue507_errexit_body_andor_regressions.rs,7 用例):构造矩阵(if/elif/else、for、while、until、case、
!)、聚合状态保留(status=$?仍 1)、ERR 消费者(trap 'echo E' ERR不再二次触发)、安全半边 9 形状仍 rc=1、无 errexit 全通过、复合重定向失败仍致命。master 上 4/7 红,本分支全绿。#508 — stdin 驱动的语法错误退出码丢失(711ce1c1)
现象:
echo "${x:-等 5 片段经 stdin 执行:诊断正确但 rc=0;文件执行 rc=2 正确。GNU 一律 2(error.hEX_SYNTAXERR)。根因:run_source* 的读时解析错误快速路径返回失败状态但不写 executor 状态;
run_stdin_script丢弃 run_source 返回的组状态,最终退出码读的是陈旧的last_exit_code()(错误前最后一次成功命令的 0)。文件驱动直接使用 run_source 的返回状态,所以只有 stdin 路径丢码——5 片段同根因。修法:
run_stdin_script在两处组执行点把组状态set_last_exit_code提交回 executor 再查 parse-error 标志(GNU evalstring.c parse_and_execute 把组状态留在 last_command_exit_value 供 reader 退出用)。普通组状态流不变(exit 5→5、DISCARD 算术失败后续读、失败组 rc=1 均复测)。顺带带掉 #516 F181 的 rc 半边(x='abc{def'; echo \${x%%[<{(]*}stdin 下 rc 0→2;其诊断形状分歧仍归 #516)。测试(tests/issue508_stdin_parse_error_status_regressions.rs,5 用例):5 片段 rc=2、F181 rc 半边、脚本中部语法错误停读 rc=2、普通状态流不变、文件驱动契约锁定。master 上 3/5 红,本分支全绿。
#510 — printf 写失败退出码丢失(2212d733)
现象:
exec >&-; printf "end\n"诊断一致但 rc=0;GNU rc=1(builtins/printf.def → sh_chkwrite 把 flush 失败折进内建状态,builtins/common.c:320-334)。echo 已是 1。根因:缓冲输出路由(route_builtin_buffered_output)已打印诊断、置 exit_code=1 并升起
last_builtin_write_failed旗——但该旗在 printf 路径上没有读者;execute_printf返回内建名义状态 0,被派发点的self.exit_code = self.execute_printf(cmd)?覆盖。修法:
execute_printf合并take_builtin_write_failed()(与 execute_pwd 已有的合并相同):exec >&-; printf x || exit 7→ 7;set -e下退出 1;无输出仍无失败(exec >&-; printf ""→ 0,GNU 实证)。测试(tests/issue510_printf_write_error_status_regressions.rs,5 用例):复现 rc=1+诊断、
||处理器、errexit 提升、空输出不误判、常规状态不变(文件重定向写穿、格式失败仍 rc=1)。master 上 2/5 红,本分支全绿。#509 — dirstack 数字参数验证统一 EX_USAGE=2(16d0c5db)
现象:
dirs +x/dirs -Q/popd +x/pushd +q→ rubash rc=1(GNU 2,builtins/common.c get_numeric_arg → EX_USAGE);dirs -l +q最糟——旗分支无视操作数,打印栈 rc=0(GNU rc=2dirs: +q: invalid number)。修法:dirs 的显示旗改为走前端(可组合,
dirs -l -v,GNU dirs.def),其后第一个词校验:± 形非数字 →invalid number+usage → 2;其他词 →invalid option+usage → 2;合法 ±N 显示该条目(-v 下带序号)。pushd:+ 形非数字 →invalid number(原跌进 Dir 臂报 No such file);popd:± 形非数字 →invalid number(原invalid argument),裸词保持invalid argument。三内建解析失败状态统一 EX_USAGE=2。合法形态全部不动(dirs/-l/-p/-v/-c、dirs +0、dirs -v +0;越界索引仍是普通失败 1,GNU rc 1)。测试(tests/issue509_dirstack_usage_status_regressions.rs,3 用例):issue 表 8 形状(rc=2+诊断+usage 行)、静默通过泄漏、合法形态保留集。master 上 2/3 红,本分支全绿。既有测试预期值更新:tests/executor_command_chaining/part_025.rs 的 test_dirs_redirects_stderr / test_dirs_appends_stderr 原钉
dirs badrc=1——正是本修复判定的旧行为;GNU EX_USAGE 裁决为 2,钉值随语义修复移动(诊断断言不变)。全量回归 parity
cargo test --no-fail-fast失败目标 10 个(--lib、cli_tests、executor_tests、issue298_299、issue331、issue377、issue382、parameter_transform_tests、pb10k_prompt_channel_regressions、regression)。part_025的两个 dirs 钉值测试——本修复的预期值更新(见 [builtins] dirstack 族数字参数验证退出码错:dirs/popd/pushd 非数字 → rc=1,dirs -l +q 静默 rc=0(GNU 统一 2) #509 节),已随之移动并复跑确认;(2)executor_tests command_chaining::part_036::test_wait_n_preserves_status_for_later_explicit_pid_wait——master 上单测复跑 5/5 通过、且 master 自身两次全量跑中 executor_tests 失败数在 25/26 间摆动,属批次内计时 flake,与本批四个修复无语义交集。cargo fmt --check干净;pre-commit 钩子(rustfmt)逐 commit 强制通过。