Skip to content

fix(assignment): restore \x18 escaped-quote carrier in quoted-assignment fast path (niubash #103) - #108

Merged
caomengxuan666 merged 2 commits into
masterfrom
feat/issue103-assignment-quote
Sep 16, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
feat/issue103-assignment-quote

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Sep 16, 2026 •

Copy link
Copy Markdown
Member

Problem

niubash #103 (v1.1.2 regression): escaped double quotes inside a quoted assignment store the raw lexer carrier byte instead of the literal character:

x="q\"q"; printf '%s' "$x" | od -An -tx1
# expected: 71 22 71   (q"q)
# actual:   71 18 71   (q<CAN>q)

Root cause

The word lexer emits control-byte carriers for escaped characters inside double quotes (\x18 for \", \x17 for \'). Command-argument expansion restores these, but the quoted-assignment fast path in src/executor/assignment_expansion.rs only restored \x1f ($), \x1a (backtick), \x14 (backslash) and the ANSI-C markers — missing \x17/\x18, so the carrier byte leaked into storage.

This is a side effect of the #99 quote-carrier rework.

Fix 1 — scalar fast path (3f23f8d)

Add the two missing carrier restorations to the fast path. All reporter scenarios verified byte-exact vs GNU bash 5.3.0(1) (WSL oracle): single/multiple escapes, propagation, ${x:-none}, heredoc, redirected output.

Fix 2 — array element side findings (742a544)

The issue's array variants had two further bugs, both fixed here after differential testing against the WSL GNU 5.3.0(1) oracle:

  1. Double quote removal: compound elements were escape-decoded first (\" -> data "), then remove_shell_quotes ran over the decoded text and re-parsed the data quote as an operator pair. Reordered to GNU's sequence: quote removal on the raw token, carrier restore, then unquote.
    • x=(q\"q): qq -> q"q; x=("q\"q"): q\"q -> q"q
  2. sh_double_quote escaping: quote_array_value/quote_assoc_display_key escaped " as \\" (two backslashes) instead of \", so the storage round-trip surfaced a spurious backslash: x=('a"b'), x=("a\"b"), x[1]=c\"d all read back with \". Now single-backslash, matching the declare storage path.

Verification

  • 3 new regression tests (fix 1) + 3 array regression tests (fix 2) in tests/assignment_quote_roundtrip_regressions.rs.
  • cargo test --lib: 397 passed. cargo fmt --check + scripts/check-utf8-boundary-hygiene.sh green.

Known remaining gap (pre-existing, separate)

An ANSI-C $'...' element whose decoded value contains a quote reaches the storage split with a bare untagged " (upstream decoder does not emit the E010/E011 markers on this path), so quote removal treats it as an operator: x=($'a"b') stores ab instead of a"b. Broken before this PR; will file separately.

Closes unixwin/niubash#103 (rubash side).

…e quoted fast path (niubash #103)

The word lexer emits \x18 for `\"` inside double quotes (the niubash #99
family of quote carriers). Command-argument expansion restores it, but the
quoted-assignment fast path in executor/assignment_expansion.rs restored
only the $ / backtick / backslash / ANSI-C carriers - the \x17 (`\'`) and
\x18 (`\"`) escaped-quote carriers were left raw in the stored value.

Result: `x="q\"q"` stored q<CAN>q (0x18) - length intact, exit status 0,
no diagnostic, and the bad byte survives into redirected files
(`printf '%s' "$x" > out` writes 71 18 71). niubash v1.1.1 was correct
because the lexer did not emit the carrier yet; the #99 fix introduced it
without extending this restore chain.

Restore both carriers alongside the others. Safety argument is the same
as for the existing restores: a $'...' value never contains a raw carrier
(the ANSI-C decoder octal-escapes every control byte into a U+E000 pair),
so a raw \x17/\x18 here is always a lexer carrier, never data.

Regression tests: tests/assignment_quote_roundtrip_regressions.rs covers
single/multiple escapes, propagation through parameter expansion,
${x:-none}, heredoc bodies, and byte-exact redirected output.

Known pre-existing issue NOT in this patch (reported in niubash#103 as a
side note): compound array elements go through a different path
(normalize_single_element_array_assignment strips the outer quotes of
`arr=("q\"q")` without resolving the inner escape, storing q\"q; the
unquoted `x=(q\"q)` drops both characters, storing qq). GNU stores q"q in
both cases. Follow-up needed in parameter_decode.rs /
normalize_single_element_array_assignment.

🤖 Generated with [WorkBuddy](https://workbuddy.cn)
…-backslash sh_double_quote

Compound array elements ran quote removal twice. The raw token first had
its backslash escapes decoded (`\"` -> data `"`), and the partially_quoted
pass then ran remove_shell_quotes over the DECODED text, which re-parsed
the data quote as an operator pair and stripped it. The order is now the
GNU one: remove_shell_quotes over the raw token (leaving escaped quotes
as \x17/\x18 data carriers), restore the carriers, then unquote. This
matches the storage-side behavior of quote.tests array6.sub and fixes
niubash #103 side findings against GNU bash 5.3.0(1) (WSL oracle):

  x=(q\"q)      stored qq        now q"q   (71 22 71)
  x=("q\"q")    stored q\"q      now q"q
  x=('a"b')     read back a\"b   now a"b
  x=("a\"b")    read back a\"b   now a"b
  x[1]=c\"d     read back c\"d   now c"d

The read-back half came from quote_array_value / quote_assoc_display_key
escaping a bare `"` as `\\"` (two backslashes) where shquote.c
sh_double_quote emits `\"`; the doubled backslash survived the storage
round-trip as a spurious literal byte. The declare storage path already
used the single-backslash form (storage.rs quote_double) and was correct.

Known remaining gap (pre-existing, not addressed here): an ANSI-C `$'...'`
element whose decoded value contains a quote reaches the storage split
with a bare untagged `"` (the upstream decoder does not emit the E010/
E011 markers on this path), so remove_shell_quotes treats it as an
operator pair: x=($'a"b') stores ab instead of a"b.

Tests: 3 new cases in tests/assignment_quote_roundtrip_regressions.rs;
cargo test --lib 397 passed; cargo fmt --check and
scripts/check-utf8-boundary-hygiene.sh clean.
@caomengxuan666
caomengxuan666 merged commit ef959e8 into master Sep 16, 2026
3 checks passed
@caomengxuan666
caomengxuan666 deleted the feat/issue103-assignment-quote branch September 16, 2026 03:44
caomengxuan666 added a commit that referenced this pull request Sep 16, 2026
fix: niubash #106/#107/#108 regressions + restore #103 array fix lost in rescue merge
caomengxuan666 added a commit that referenced this pull request Sep 16, 2026
…as-char sites

- README/README.zh-CN: new "Fixed today" section covering niubash #106 (CRLF
  line-terminator handling), #107 (-c GNU first-non-option-argument), #108
  (type output capture), plus the nameref/history convergence and the
  debug-instrumentation cleanup from the local batch landing.
- check-utf8-boundary-hygiene: extend the execution_misc.rs allowlist entry
  with the two reviewed byte->char widening patterns introduced by the batch:
  `out.push(byte as char)` under the 0x20..=0x7e printable-ASCII arm and the
  octal digit math `b'0' + ((byte >> n) & 0o7)` (always 0x30..0x37) — both can
  never involve a UTF-8 continuation byte.
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] v1.1.2 回归:变量赋值中 \" 被写成 0x18 控制字节(#99 修复的副作用)

1 participant