Skip to content

fix(printf): %S fails with EILSEQ in a single-byte locale (Refs #3008) - #3043

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3008-printf-S-c-locale
Nov 8, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3008-printf-S-c-locale

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3008

根因

printf %S 的渲染路径(format_value,src/builtins/printf/value.rs)从未在失败方向上问过 locale 的问题:无论当前 locale 是单字节还是多字节,%S 都按字节原样渲染,从不报错。

GNU bash 5.3 的语义(builtins/printf.def):

  • :527-547 —— %S 只有在 locale_mb_cur_max > 1(:532)时才走宽字符路径(getwidestr / printwidestr)。
  • 单字节 locale 下指令落到 PF 宏(:153)——把 %S 格式与字节串参数交给 libc printf。glibc 把参数按 wchar_t * 读取,转换以 EILSEQ 失败;PF 于是经 builtin_error 报 strerror(EILSEQ) 并以 PRETURN(EXECUTION_FAILURE) 收尾:该指令不产生任何输出(连宽度填充也没有),此前已产生的输出保留,-v 仍绑定失败前已渲染的内容。
  • %ls 在 modstart 截断(:490)下被改写为 %s、%C 走窄 getchr 路径——两者在 C locale 下照常打印,不在本单范围内。
  • 多字节 locale 下 getwidestr 对非法序列逐字节加宽兜底(:1570 MB_INVALIDCH),永不失败——所以单字节是唯一的失败方向。

修法

format_value 在 byte_mode 计算之后加一道前置判定:说明符为 S 且 crate::locale::is_multi_byte_active() 为假(与 #2979 的 byte_mode 决策同一个 guard-aware 的 active-locale 问题)时,返回空渲染 + stop_output(PRETURN 形状)+ 一条 fatal 诊断(正文 printf: Invalid or incomplete multibyte or wide character,即 strerror(EILSEQ))。

format_value 经 codegraph 普查只有一个调用方(render_one_pass)。

本 PR 删除了什么

没有删除任何内容。src/builtins/printf/value.rs 净增 25 行(注释 + 前置判定);新增回归钉文件一个(208 行)。

注记:钉的 width 格在接手复核时按 GNU oracle 探针订正过——原期望「前缀字面量一并丢弃」与 GNU 不符(PRETURN 冲刷已产出部分,前缀 [ 保留),已改为 oracle 形状并在钉内注明。

验证(车道自报)

  • fail-first(未修树,基 446d7662):钉 8 failed / 2 passed(2 过 = C.UTF-8 对照 + %ls/%C 守卫格)。
  • 修后钉:10 passed / 0 failed(格式化后的交付形态复跑同数)。
  • GNU oracle 探针 p1–p10 对修后二进制逐字节比对(stdout + stderr):10/10 一致(含空串参数、%lS、%.2S、缺参、-v、UTF-8 对照、%ls/%C 守卫)。
  • 全量 lib:1197 passed / 0 failed / 4 ignored,与基 446d7662(worktree 独立跑)逐数一致。
  • RUSTFLAGS="-D warnings" cargo check --locked --lib --bins:rc=0;hygiene clean;两处改动文件 rustfmt --check clean;无 CR。

UNVERIFIED-BY-CAPTAIN

GNU bash 5.3 printf.def:532 gates the %S wide path on locale_mb_cur_max > 1; in a single-byte locale the directive falls to the PF macro (printf.def:153) and fails with EILSEQ. Ten cells: C-locale ASCII / non-ASCII / env-sourced / missing arg / prefix-kept / width / -v store / printf -v locale flip, plus the C.UTF-8 control and the %ls + %C scope guard. Fail-first on the unfixed tree: 8 failed / 2 passed.
format_value rendered %S as bytes without ever asking the locale question in the failing direction. GNU: printf.def:527-547 sends %S through getwidestr/printwidestr only when locale_mb_cur_max > 1; otherwise the directive falls to the PF macro (printf.def:153), which hands the %S format and the byte-string argument to libc printf. glibc reads the argument as a wchar_t *, the conversion fails with EILSEQ, and PF reports strerror(EILSEQ) via builtin_error and PRETURNs with EXECUTION_FAILURE: nothing is emitted for the directive (not even width padding), output already produced stays, and -v still binds what was rendered before the failure.

The fix keys the failure on the S specifier alone, gated by locale::is_multi_byte_active (the same guard-aware question the #2979 byte_mode decision asks): %ls is rewritten to %s by the modstart truncation (printf.def:490) and %C takes the narrow getchr path, so both keep printing in a single-byte locale; in a multibyte locale getwidestr cannot fail (invalid sequences widen byte-wise, printf.def:1570), which makes this the only failing direction.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 8, 2026 00:21
@caomengxuan666
caomengxuan666 merged commit e8a3d01 into master Nov 8, 2026
19 of 20 checks passed
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