Repository navigation
fix(printf): %S fails with EILSEQ in a single-byte locale (Refs #3008) - #3043
Merged
Merged
Conversation
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
marked this pull request as ready for review
November 8, 2026 00:21
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.
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)。:153)——把%S格式与字节串参数交给 libcprintf。glibc 把参数按wchar_t *读取,转换以 EILSEQ 失败;PF 于是经builtin_error报strerror(EILSEQ)并以PRETURN(EXECUTION_FAILURE)收尾:该指令不产生任何输出(连宽度填充也没有),此前已产生的输出保留,-v仍绑定失败前已渲染的内容。%ls在 modstart 截断(:490)下被改写为%s、%C走窄getchr路径——两者在 C locale 下照常打印,不在本单范围内。getwidestr对非法序列逐字节加宽兜底(:1570MB_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 形状并在钉内注明。验证(车道自报)
446d7662):钉 8 failed / 2 passed(2 过 = C.UTF-8 对照 +%ls/%C守卫格)。%lS、%.2S、缺参、-v、UTF-8 对照、%ls/%C守卫)。446d7662(worktree 独立跑)逐数一致。RUSTFLAGS="-D warnings" cargo check --locked --lib --bins:rc=0;hygiene clean;两处改动文件 rustfmt --check clean;无 CR。UNVERIFIED-BY-CAPTAIN