Skip to content

fix(quote): escape U+2028/U+2029 as $'...' octal in declare -p and ${x@Q} (Fixes #3946) - #4040

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3946-declare-q-u2028-u2029-octal
Nov 21, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3946-declare-q-u2028-u2029-octal

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Root cause

In UTF-8 locales, GNU ansic_shouldquote / ansic_quote (third_party/bash/lib/sh/strtrans.c:230-354) checks iswprint(wc) on wide characters: glibc treats U+2028 (LINE SEPARATOR) and U+2029 (PARAGRAPH SEPARATOR) as non-printable (Zl/Zp) so they are emitted as $'\342\200\250' / $'\342\200\251', while Cf format characters (U+200B, U+FEFF, U+00AD) remain printable raw. Rubash's is_printable_wide (arrays/storage.rs) and is_ansic_printable (parameter_replace.rs) only checked !ch.is_control(), printing U+2028/U+2029 raw.

Fix

  • src/executor/arrays/storage.rs (is_printable_wide) & src/executor/parameter_replace.rs (is_ansic_printable): use !crate::locale::is_glibc_cntrl(ch) (Cc union {U+2028, U+2029}) so declare -p and ${x@Q}/${x@K}/${x@k} emit GNU's $'...' octal escapes for U+2028/U+2029.
  • Add tests/issue3946_ansic_quote_line_paragraph_separator.rs (5 tests).

Verification

  • Verified byte-for-byte against WSL /usr/local/bin/bash (LC_ALL=C.utf8).
  • rustfmt --edition 2021 --check clean on all 3 files.
  • cargo test --test issue3946_ansic_quote_line_paragraph_separator: 5/5 pass.

What this PR deletes

−5 lines in total: the two old predicate expressions (!ch.is_control() in is_printable_wide in src/executor/arrays/storage.rs, −3 lines with its comment, and in is_ansic_printable in src/executor/parameter_replace.rs, −2 lines) and the outdated comment text attached to them. Nothing else is removed; both predicates now delegate to the existing shared model crate::locale::is_glibc_cntrl (Cc ∪ {U+2028, U+2029}) instead of testing is_control() alone.

Captain verification

Verified by the captain's oracle lane on the cloud host against the real GNU bash 5.3.20 oracle (~/oracle/bash-5.3.20/bin/bash, self-built 5.3 + patches 001–020), 2026-11-21. PR head 1012e90f and pristine base eb1ce9f2 both built on the cloud host (debug, jobs=1).

  • Seed matrix (issue [bug] declare -p and ${x@Q} print U+2028/U+2029 raw instead of GNU's $'...' octal form #3946's seed_r138c.sh, 12 checks, LC_ALL=C.utf8): oracle 12/12 PASS; this PR 12/12 PASS; pristine eb1ce9f2 fails exactly the 6 defect checks (declare -p / ${x@Q} × U+2028, U+2029, and the mixed value) and passes all 6 controls — the pristine divergence set is the fix face exactly.
  • Extra byte-compare matrix (script files, raw stdout captured and compared with cmp, LC_ALL=C.UTF-8): the pin-2 shapes (${x@Q}/${x@K}/${x@k}), the pin-5 shapes (array/assoc elements and element @Q), plus added @K/@k × U+2029 and mixed-value @K/@k shapes — PR output byte-identical to the oracle on all three probe files; pristine differs on every separator-bearing line (raw bytes instead of the $'\342\200\250' / $'\342\200\251' form).
  • Fail-first: the PR's 5 tests run against pristine eb1ce9f2 — declare_p_escapes_line_and_paragraph_separators, at_transforms_escape_line_and_paragraph_separators, and array_and_assoc_elements_escape_separators FAILED; the two control tests (c1_controls_stay_octal_escaped, format_characters_stay_raw) passed. On the PR tree all 5 tests pass.

The author's WSL byte-compare is thus confirmed against the captain's 5.3.20 oracle; the review's outstanding oracle and fail-first debts are cleared.

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