Skip to content

fix(expand): decode ANSI-C quotes in compound assignment parameter-operator words (Fixes #4248) - #4259

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/4248-compound-assign-param-ansi-c
Nov 28, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/4248-compound-assign-param-ansi-c

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Expand ANSI-C $'...' quoting inside ${x:-...} (and sibling parameter-operator RHS forms) within compound array assignment elements, matching GNU Bash 5.3.20 byte-for-byte in both non-POSIX and POSIX modes (Fixes #4248).

GNU C Specification

  • third_party/bash/arrayfunc.c:557 (expand_compound_array_assignment()): splits the compound body via parse_string_to_word_list() (:581) and expands each element via expand_words_no_vars() (:610).
  • third_party/bash/parse.y:4053-4070 (parse_matched_pair()): translates $'...' inside ${...} via ansiexpand(), and parse.y:4030-4036 implements Austin Group Interp-221 (double-quoted ${...} in POSIX mode keeps $'...' literal).
  • third_party/bash/parse.y:5564-5572 (read_token_word()): wraps decoded ANSI-C bytes as quoted data so decoded quotes/whitespace/glob chars do not re-enter shell syntax or field splitting.
  • third_party/bash/subst.c:9777 (parameter_brace_expand()), subst.c:7966 (parameter_brace_expand_rhs()), subst.c:4462 (expand_string_for_rhs()), and subst.c:4807 (dequote_string()).

Root Cause & Changes

  1. src/executor/assignment_expansion.rs:
    • Route whole-word ${var op word} compound elements through the real braced-parameter expansion path (expand_quoted_parameter_word when double-quoted, expand_word when unquoted) instead of blind .replace("\\\"", "\"").replace("\\'", "'") unescaping (which broke $'e\'f' and dropped quoting context).
    • Propagate double-quote context into whole_word_parameter_single_value for both element and [sub]= forms.
    • Convert ANSI_C_IFS_GUARD to IFS_GLUE for field splitting and decode compound expansion carriers before storage so ANSI-C blanks ($'a\tb', $'a b') remain single elements while outer spaces split normally.
  2. src/executor/embedded_parameters.rs:
    • Add the $'...' decode arm in the embedded parameter walker (preserving raw $'...' in heredocs and inside double-quote spans, keeping #828 intact), using existing escape_decoded_ansi_c_quotes / escape_decoded_ansi_c_quotes_unprotected carrier protection so decoded syntax bytes remain data.
  3. src/executor/parameter_words.rs:
    • Align decode_double_quotes_in_quoted_parameter_word with GNU parse.y:4030-4036 (Interp-221): keep $'...' literal inside double-quoted ${...} in POSIX mode while still decoding unquoted ${...} operator words.
  4. tests/issues_4200_4299.rs + tests/issues_4200_4299/issue4248_compound_ansic_operator_rhs.rs:
    • Added grouped regression suite covering the 20-case compound/assoc/subscript/scalar/pattern/replacement matrix, decoded syntax-byte data preservation, and POSIX vs non-POSIX double-quoted operator $'...'.

What this PR deletes

  • Deletes the blind .replace("\\\"", "\"").replace("\\'", "'") string corruption in whole_word_parameter_single_value (src/executor/assignment_expansion.rs).
  • Deletes the obsolete in_sq POSIX single-quote-span state in decode_double_quotes_in_quoted_parameter_word (src/executor/parameter_words.rs) that contradicted GNU Interp-221 behavior.

Verification

  • WSL GNU Bash 5.3.20 (/usr/local/bin/bash) matrix:
    • Non-POSIX 21/21 cases: 100% byte-exact.
    • POSIX (--posix) 21/21 cases: 100% byte-exact.
  • WSL Linux:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked: clean (0 warnings).
    • cargo test --test issues_4200_4299 --locked: 5/5 passed.
    • cargo test --test issues_0600_0999 issue828 --locked: 39/39 passed.
    • cargo test --test regression upstream_suite_slice_snapshots_{quote,nquote,rhs_exp} --locked: all passed.
  • Windows:
    • cargo test --lib: 1,447 passed, 0 failed.
    • cargo test --test issues_4200_4299 --locked: 5/5 passed.
    • python scripts/check-rustfmt.py: ok (226 targets).
    • python scripts/check-marker-literals.py: OK.
    • python scripts/check-api-ledger.py: OK (298 symbols).

Fixes #4248

@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain review — needs runtime verification, please hold off merging.

The non-POSIX core of this fix is well-evidenced (parse.y:4053-4070, subst.c:4215-4233) and the c1–c11 matrix plus the decoded-syntax-bytes test cover the #4248 faces. CI is clean: census 9 FAIL all known, 0 SIGABRT, goldens only the 2 standing snapshots.

But there are two oracle questions the code alone can't settle:

R1 (blocking): the POSIX arm contradicts the GNU source. The new test posix_double_quoted_operator_ansic_is_literal asserts that under --posix, f=("${x:-$'a;b'}") yields the literal $'a;b' while the unquoted form decodes to a;b. Yet parse.y:4053 ansiexpands $' inside ${...} words with no posixly_correct condition, and wraps the result in sh_single_quote in the double-quoted brace context — i.e. GNU should give the decoded value under POSIX too, not the literal. The subst.c:1586 posix-literal branch you may be leaning on lives in the heredoc extractor and is conditioned on dolbrace_state; under the state machine (1773-1788) it points the other way from the test's dq-literal / uq-decode split. This is the #1905 shape — an implementation changed to match an unproven assertion — so the oracle decides: I'll run the --posix dq/uq forms against the real GNU bash 5.3.20 oracle, and the assertion or the arm goes, whichever the oracle says.

R2 (verify with R1): the in_sq deletion crosses a state condition. parse.y:4035 and subst.c:1741 say ' is non-special only when dolbrace_state != DOLBRACE_QUOTE/QUOTE2; in QUOTE state ' is still a span delimiter. The PR deletes in_sq tracking outright, making ' DATA_SQUOTE in every state. Pattern words in ${x%...} / ${x#...} containing a bare ' under POSIX have no coverage — I'll add one oracle probe for that form during the same verification run.

Also: the base (55b0aebb) is behind master, so please rebase before merging once verification lands.

@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain follow-up — the R1/R2 concerns are withdrawn; this PR now passes the gate, merge after rebase.

I ran the deciding probes against the real GNU bash 5.3.20 oracle (cloud), and the oracle proves my read-only review wrong:

  • R1: under --posix, the double-quoted operator word f=("${x:-$'a;b'}") genuinely yields the literal $'a;b', and the unquoted form decodes to a;b — byte-identical to the new test's assertion, confirmed in two oracle runs. My inference from parse.y:4053 / subst.c:1586 pointed the wrong way: those source lines don't determine the actual condition combination in the double-quoted brace context. Oracle rules; the POSIX arm and the assertion both stay.
  • R2: pattern words with a bare ' behave identically between the PR binary and the oracle in all four dq/uq × POSIX/non-POSIX cells (stripped; ' inside the value is data). The in_sq deletion causes no behavioral divergence.

Full runtime evidence: new pins 3/3 pass on the PR tree and all 3 go red on base — on base the compound-element $'...' leaks the truncated literal $'a;b (the closing quote swallowed), which is the #4248 disease itself. 13 probe forms × 2 modes are byte-identical between the PR binary and the oracle, including the heredoc literal form.

Remaining ask, same as before: the base is stale (mergeable=false now), so please rebase onto current master; once mergeable with a fresh CI showing the same known census names, I'll merge it.

@caomengxuan666
caomengxuan666 force-pushed the fix/4248-compound-assign-param-ansi-c branch from fdceb7b to 84a3dcb Compare November 28, 2026 05:56
@caomengxuan666

Copy link
Copy Markdown
Member Author

Rebased onto current master (f9c2b9599) as 84a3dcb25 (resolved the 2-line tests/issues_4200_4299.rs registration conflict with #4257).

  • RUSTFLAGS="-D warnings" cargo check --lib --locked: clean (0 warnings)
  • cargo test --test issues_4200_4299 --locked: 10/10 passed
  • python scripts/check-rustfmt.py: ok (226 targets)

Fresh CI is running now.

@caomengxuan666
caomengxuan666 merged commit 29384e2 into master Nov 28, 2026
11 of 13 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/4248-compound-assign-param-ansi-c branch November 28, 2026 06:35
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.

[gnu-baseline] compound assignment element: ${x:-$'…'} inside array element is stored literally, not expanded

1 participant