Skip to content

[errexit] set -e 误杀 if 体内 && 列表失败:rubash 整壳退出 rc=1,GNU 豁免后打印 R rc=0 #507

Closed

Description

@caomengxuan666

复现(stdin 驱动:printf '%s' "$s" | shell)

set -e; if true; then false && true; fi; echo R
stdout+stderr rc
GNU bash 5.3.0(1)-release (WSL) R 0
rubash origin/master 764b9dd(debug) (空) 1

rubash 在执行 echo R 之前整壳退出,无任何输出。

根因

false && true 中 false 不是 AND-OR 列表的最后一个命令,其失败被 errexit 豁免;if 体是普通命令列表,&& 列表整体失败同样不得触发退出。rubash 把 if 体内 && 列表的失败当成了 errexit 触发条件直接终止。

规范/源码依据

  • POSIX XCU 2.14 (set -e):"The -e setting shall be ignored when executing the compound-list following the while, until, if, or elif reserved word, a pipeline beginning with the ! reserved word, or any command of an AND-OR list other than the last."
  • Bash Reference Manual(set -e):"The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test following the if or elif reserved words, part of any command executed in a && or || list except the command following the final && or ||..."
  • GNU 实测:set -e; false && true; echo ok 打印 ok(rubash 此形式正确,分歧只在复合命令体内)。

跨实现互证(huck)

huck(jdstanhope/huck,另一 Rust bash 实现)在同一语义点有 issue #676 "set -e exits on a failing && list when it is the LAST command of an if/for/case/brace body"(portable, sev:high,已关闭=已修复)。同一错误在两个独立实现中出现,说明这是 Rust 重写 shell 的高频坑;huck 的修复可作参考。

证据

  • 语料出处:huck errexit_adjudication 家族 harness(fragment F067,label: if body)
  • 复核:origin/master 764b9dd 仍复现;旧构建 41de831 行为一致(期间 perf/docs 两提交未涉及)
  • 原始输出:语料回放账本 replay_ledger.jsonl(F067)

Activity

  1. added
    bug
    P1-high
    on Oct 12, 2026
  2. caomengxuan666 commented on Oct 12, 2026

    @caomengxuan666
    MemberAuthor

    审计增量(huck 443-issue 全量对照,issues-audit we-diverge A1):本 issue 的 if 体格在 764b9dd 仍复现;同根的其余复合构造体末尾格全部同病,不需要新开 issue,统一在此跟踪:

    set -e
    while true; do false && echo y; break; done; echo W    # rubash 退出 rc1 / GNU 打印 W rc0
    for i in 1; do false && echo y; done; echo F           # rubash 退出 rc1 / GNU 打印 F rc0
    case x in x) false && true;; esac; echo C              # rubash 退出 rc1 / GNU 打印 C rc0

    边界矩阵(GNU 实测 + rubash 对照,LEDGER huck#676):

    • 顶层 / { } 体 / 子壳 / 函数调用处:rubash 与 GNU 一致(该退的退)✔
    • 非 body 末尾的 && 列表、体末尾普通 false:双侧一致 ✔
    • 仅复合构造体末尾 + && 列表经豁免路径失败这一格错:if/while/for/case 四形全中。

    跨实现互证:huck#676 "set -e exits on a failing && list when it is the LAST command of an if/for/case/brace body"(portable,sev:high,已修复)——与 #507 正文引用的 huck#676 同一条,此处补齐 while/for/case 三形的语料证据(huck errexit_adjudication harness,fragment F067 族)。

  3. caomengxuan666 commented on Oct 12, 2026

    @caomengxuan666
    MemberAuthor

    Fixed and merged (see the linked PR in timeline).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1-highbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions