Skip to content

[builtins] 写失败退出码丢失:exec >&- 后 printf 报 write error 但 rc=0(GNU 1) #510

Closed

Description

@caomengxuan666

复现

exec >&-; printf "end\n"
stdout+stderr rc
GNU bash 5.3.0 bash: line 1: printf: write error: Bad file descriptor 1
rubash 764b9dd ...rubash.exe: line 1: printf: write error: Bad file descriptor(消息一致) 0

根因

printf 检测到了写失败并打印了与 GNU 相同的诊断,但没有把失败反映到退出码——写失败的退出状态被丢弃。exec >&- 防呆 + printf ... || fail 是脚本常用模式,rc=0 会静默吞掉数据未落盘的事实。

规范/源码依据

  • bash 内建 printf:写失败 → 报 printf: write error: ... 且内建返回 1(builtins/printf.def:write error 路径置 exit_status=1)。
  • POSIX printf 工具约定:发生写错误时退出状态 >0。

跨实现互证(huck)

证据

  • 语料出处:builtin_write_error_diff_check.sh:46(fragment F145,label: printf-closed)
  • 复核:764b9dde 仍复现;旧构建 41de831 行为一致

Activity

  1. added
    bug
    P1-high
    on Oct 12, 2026
  2. 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