Skip to content

locale: decode $'\u/\U under the surviving locale after a failed setlocale - #3622

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3619-surviving-locale-ansic-decode
Nov 16, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3619-surviving-locale-ansic-decode

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

GNU locale.c set_locale_var is stateful: a failed setlocale() warns but leaves the previously effective locale in force. rubash's \u/\U decode gate (src/lexer/ansi.rs → locale::locale_name_selects_utf8) decided from the requested locale name alone — any non-UTF-8 charset name returned false without checking whether setlocale could actually activate it. Under LC_ALL=zh_TW.big5 (no big5 archive) with a UTF-8 locale already in force, GNU still decodes $'\\u3b1' → α while rubash emitted the literal \u03B1 (glob2.sub divergence, Refs #2666).

Fix

locale_name_selects_utf8 now falls back to the effective codeset (nl_langinfo(CODESET)) when the named locale is not activatable — same model for an un-activatable UTF-8 name. All consumers share the stateful result: the $'\u' decode gate, echo -e, and the multibyte flag in trap_stack_builtins.

Verification

  • Byte-diff vs vendored GNU 5.3.20 (script files): LC_ALL=zh_TW.big5/zh_CN.gb18030 → a α/c 中; LC_ALL=C → literal \u03B1. Identical stdout and identical setlocale warnings.
  • New pin tests/issue3619_ansic_surviving_locale_decode.rs: 2 tests, red pre-fix / green post-fix.
  • Regression: 20 locale/ansic pin files green; 4 failures in issue353 are pre-existing env-bound and identical without this change.

Fixes #3619

…equested name

GNU locale.c set_locale_var is stateful: a failed setlocale() warns
but leaves the previously effective locale in force, so under an
un-activatable name (zh_TW.big5 / bogus names) a surviving UTF-8
locale still decodes $'\u3b1' to α, while an ACTIVATABLE non-UTF-8
name (C) does impose byte semantics.

locale_name_selects_utf8 decided from the requested name alone: any
non-UTF-8 charset name returned false without checking whether
setlocale could activate it — so $'\u3b1' stayed literal where GNU
decodes (glob2.sub run-glob-test divergence, Refs #2666).

Fix: when the named locale is not activatable, fall back to the
effective codeset (nl_langinfo CODESET) instead of assuming bytes.
Every consumer of the predicate — the ansi.rs $'\u'/'\U' gate, the
echo -e face, and the multibyte flag in trap_stack_builtins — now
shares the same stateful model.

Fixes #3619

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3622 第1轮 — 建议:FAIL
head: fd1f649 base(merge-base): 27e1d0d 文件: 2 个 +112/-1
关联 issue: #3619(状态:open,我核过的时间:2026-11-16 本地)

机制核查:

  • 语义方向正确:GNU locale.c:185-237 set_locale_var 是 stateful 的——setlocale() 失败时 x==0,locale_utf8locale 只在 *lc_all && x 时更新(locale.c:231-233),失败保留前一 locale;消费点是 subst.c:6739-6740 等 locale_utf8locale 判断,所以「不可激活的名字」不应把解码切到字节语义。rubash 侧 locale_activatable/effective_locale_selects_utf8 用 setlocale 试探 + nl_langinfo(CODESET) 读存活 locale,与 init_locale(attempt_setlocale(LC_ALL,""))和赋值 watcher(locale_assignment_warning)的状态模型自洽。Windows 分支保持原模型(locale_activatable=true / effective=false),产品面在 Windows 零 delta。
  • 无关改动:无(locale.rs + 新钉)。

腿(逐项给数字与名字):

  • 钉自跑:cargo test --test issue3619_ansic_surviving_locale_decode → 0 passed / 0 failed(#![cfg(unix)],Windows 整文件 cfg 掉 = VOID)。
  • fail-first:同 stem 在 base 27e1d0d 上同 0 tests(VOID)。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 失败名集 {同左} / 差集 ∅。
  • -D warnings:本 PR 文件 0 新增(cargo check --lib 干净;--tests 撞无关 stem 既有 dead_code)。
  • 卫生:fmt rc=0(2 文件逐个);marker-lint OK;utf8 脚本不存在(VOID)。
  • CI:本 PR 自己的红 = Cross-target check (linux/android):error[E0425]: cannot find function 'nl_langinfo' in crate 'libc' + error[E0425]: cannot find value 'CODESET' in crate 'libc'(Android 目标)。其余红 = 基线 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell(另加 master-red issue2859、Unrecognized option: 'c' 的既有噪声)。
  • GNU 对照(WSL GNU bash 5.3.20,脚本 target/probes/l3619.sh):LC_ALL=C.UTF-8 环境下 LC_ALL=bogus_LOCALE; echo a $'\u3b1'; echo B$'\u03b1'B → a α / BαB(存活 UTF-8 locale 仍解码);随后 LC_ALL=C; echo d $'\u3b1' → d \u03B1(可激活的 C 立即切字节语义,十六进制大写)。钉期望与 5.3.20 逐字节一致(PR 正文写 5.1 oracle,5.3.20 复核相同)。
  • Windows 对照(rubash.exe,同一脚本):三行全 literal(a \u03B1 / B\u03B1B / d \u03B1),因为 Windows 分支 locale_activatable=true、effective=false;这是既有平台模型,不属本 PR 账,但说明本 PR 的行为在本 lane 无法判别。

F 点(建议 FAIL):

  • F1: Android 目标编译红。#[cfg(unix)] 把 Android 也算进去,但 libc crate 在 target_os="android" 下没有 nl_langinfo/CODESET,src/locale.rs:296-303 直接 E0425,cargo check --tests --target aarch64-linux-android 红(CI job 132497712114)。修法:#[cfg(all(unix, not(target_os = "android")))](或在 android 上回退到请求名模型),钉文件同理收窄;不要用 cfg(unix) 一把抓。
  • 备注: PR 正文的 GNU oracle 版本写 5.1;本项目基线是 5.3.20,虽结论一致,请在正文改口径。

未跑/VOID 的腿:

  • 钉自跑 / fail-first / 行为判别:整条修复面是 cfg(unix),Windows 上 0 tests 且行为零 delta,VOID。本 lane 给不出「head 修好、base 坏」的执行证据,只有源码语义 + GNU 5.3.20 期望核对。请 Linux oracle 补两腿后再推进。

…#3619)

libc exposes neither nl_langinfo nor CODESET for Android targets, so
the cfg(unix) arm failed the linux/android cross-target check. Android
now shares the non-Unix fallback (requested-name model), matching its
pre-change behavior; the pin gets the same gate since the fallback
cannot observe the surviving locale there.
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3622 第2轮 — 建议:PASS
head: 91e74fa base(merge-base): 27e1d0d 文件: 2 个 +7/-4(累计 diff 见 PR)
关联 issue: #3619(状态:open,我核过的时间:2026-11-16 本地)

机制核查:

  • 产品模型正确:GNU locale.c setlocale 失败后保留前一 locale,$'\u' 解码门应查「存活 locale」而非「请求名」。PR 以 locale_activatable(probe_utf8_locale/setlocale 探测)+ effective_locale_selects_utf8(nl_langinfo CODESET)实现,方向与 GNU 一致。
  • F1(android cross-target 编译红):#[cfg(unix)] 误吞 android——libc crate 的 android 绑定无 nl_langinfo/CODESET(CI E0425×2,本地 registry libc-0.2.186 android/ 目录 grep 证实无此符号)。已收窄为 all(unix, not(target_os = "android")),android 走 any(not(unix), target_os = "android") 兜底返回 false(= 改动前行为,Windows 同臂已编译验证)。钉同步收窄。
  • diff 无无关改动。

腿:

  • 钉自跑:issue3619_ansic_surviving_locale_decode — WSL 2 passed / 0 failed;Windows 编译通过、cfg 后 0 tests(预期)。
  • fail-first:merge-base 27e1d0d + 钉 — 0 passed / 2 failed,红名 = {unactivatable_name_keeps_surviving_utf8_locale, activatable_non_utf8_name_still_forces_bytes},base 实测输出 a \u03B1(旧字面量行为),与 PR 声称一致。
  • lib 双边:head 5f08323b7(=91e74fa8 内容)1247/0/4;base 27e1d0d 1247/0/4;差集 ∅。
  • -D warnings:本 PR 文件 0 警告;crate 级仅存量基线警告 unused import std::io::Read(command_substitution_values.rs:4,merge-base 已存在)。
  • 卫生:rustfmt --check 两文件 clean;marker-lint OK;utf8 clean。
  • CI(旧 head fd1f649):红 4 项 = {Cross-target check (linux/android)(本 PR 的账,本 commit 修复)、GNU regression goldens and canaries、Rust tests、Rust tests (macOS)};后三者失败名均为基线 issue2875_input_procsub_child_runs_as_rubash_shell。新 head 91e74fa 的 android check 以新 run 为准。
  • GNU 对照:WSL GNU bash 5.3.20(1),脚本 target/probe3619-gnu.sh — LC_ALL=bogus_LOCALE 下 b α + cannot change locale 警告;LC_ALL=C 下 d \u03B1(大写 hex 字面量)——与钉期望及 rubash head 一致。

F 点:无。

续作记录:

未跑/VOID 的腿:

  • android cross-target 本地复跑:本机无 NDK clang(psm build script 需 aarch64-linux-android-clang),无法本地 cargo check android;cfg 正确性以 libc 源码核对 + 与 Windows 同臂编译通过佐证,最终以 CI 新 run 为准。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 15:29
@caomengxuan666
caomengxuan666 merged commit 3f47bf1 into master Nov 16, 2026
10 of 12 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.

[bug] $'\u/\U decode consults the requested locale name, not the surviving locale after a failed setlocale

1 participant