Skip to content

fix(executor): normalize assoc subscripts so ANSI-C spellings match variable lookups (#734) - #822

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/734
Oct 18, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/734

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #734 (items 1-3; item 4 lives with the quote-state family as noted in the ticket).

Root cause

Both the write (A[sub]=v) and the read (${A[sub]}) funnel through the single expand_subscript_string pass (GNU subst.c:11063), but its output kept two decode products the finished key must not carry:

  1. ANSI_C_IFS_GUARD (U+E401) left in the key. escape_decoded_ansi_c_quotes prefixes every decoded $'...' blank with the guard; the resolved assoc key kept it. A guard-form key only matched another $'...' spelling, while the variable and dq-literal spellings computed the clean bytes and missed the cell — the ticket's item-1 repro (A[$'x\ty']=1 then ${A[$k]} → [] vs GNU [1]). declare -p then rendered the marker inside the stored key (item 2: ee 90 81 in the output bytes), and the compound-kvlist entry (which already strips) disagreed with the direct entry.
  2. Decoded backslash eaten on the assignment path. After decoding $'a\\b' → a\\b, the escape pass re-read the decoded backslash as syntax and dropped it, storing ab (item 3). GNU decodes the assignment word at parse time — parse.y:5551-5572 read_token_word() $' arm: ansiexpand (:5566) + sh_single_quote re-quote — so the subscript reaches expand_subscript_string (arrayfunc.c:392-398 assign_array_element_internal) with the backslash single-quoted and it survives (GNU stores 61 5c 62). The ${A[...]} reader hands the raw spelling to the runtime decode without that protection, so GNU's own read of ${A[$'a\\b']} keys on ab and misses the a\\b cell — a write/read asymmetry that is GNU behavior, pinned as parity in the corpus (r08/r11).

Fix (src/executor/subscript_expansion.rs)

  • expand_subscript_string strips the bare (E400-unescaped) guard from the resolved key. User data U+E401 rides the E400 pair and survives (rubash#735 discipline). All spellings now store and look up the same clean cell; declare -p renders GNU's [$'x\ty'] form with no marker bytes.
  • New expand_assignment_subscript_string — used by the two assignment entries (prepare_array_element_assignment_word, the quoted-assignment arm in command_prepare.rs) — doubles decoded $'...' backslashes so the escape pass collapses them back to one literal backslash, the way GNU's parse-time quoting protects them. Readers keep the eating semantics, matching GNU.

GNU differential evidence (GNU bash 5.3.0, WSL, script files; all rows byte-identical after the fix)

row case GNU 5.3.0 rubash before rubash after
r01 A[$'x\ty']=1 → ${A[$k]} [1] [] [1]
r02 $'...' write → $'...' read [1] [1] [1]
r03 A[$k]=1 → ${A[$'x\ty']} [1] [] [1]
r04 $'...'` write → dq-literal-tab read [1] [] [1]
r05 dq-literal write → $'...'` read [1] [] [1]
r06 stored key bytes ($'x\ty') 780979 780979* 780979
r07 stored key bytes ($'a\b') — item 3 615c62 6162 615c62
r08 ${A[$'a\\b']} self-read [] (GNU quirk) [1] []
r09 ${A[a\\b]} bare read of r07 cell [1] [1] [1]
r10 ${A['a\\b']} sq read of r07 cell [1] [1] [1]
r11 bare a\\b write → $'...'` read [] [] []
r12 $'a\b'+a\b` writes one cell ["a\\\\b"] one cell one cell
r13 $'x\ty'` + dq-tab writes one cell [$'x\ty'] two cells one cell
r14 declare -p key bytes — item 2 [$'x\ty'] no marker ee9081 in key [$'x\ty'] no marker
r15/16/16b membership ${+x} / [[ -v ]] var+ansi set/set/set set/unset/unset set/set/set
r17 unset "A[$k]" A=() cell survived A=()
r18 unset 'A[$'x\\ty']' (raw ANSI-C operand) cell remains cell remains cell remains
r19 kvlist A=([$'x\ty']=cv) → var read [cv] [cv] [cv]
r20 kvlist key bytes ($'a\b') 615c62 615c62 615c62
r21 user $'\\uE401' key (#735 interplay) ee9081 + [g] eaten → [] ee9081 + [g]
r22 $'p\\q' (unrecognized escape) key 705c71, self-read [] ate backslash 705c71, self-read []
r23 mixed "ab"$'c\td' write, tab reads []/[] []/[] []/[]

* r06 iterated clean before only because the key-listing path strips markers at its own boundary; the stored key still carried them (visible to declare -p, r14) and key matching used the guard form (r01/r03/r04/r05).

Tests

tests/issue734_assoc_ansic_subscript_regressions.rs — 15 tests encoding the GNU-verified expectations above (storage bytes, cross-spelling reads, declare -p rendering bytes, membership, unset, kvlist agreement, #735 PUA interplay, unrecognized-escape parity).

Suite impact

Full cargo test --no-fail-fast vs baseline 7047baf3: zero new failures, zero flipped tests in either direction. The only delta was 2 baseline-only failures in the known-flaky executor::path cache tests (green in isolation). Rebased over #788 (d248d9f) — no file overlap.

Out of scope (same family, different entry points)

  • printf -v / unset ANSI-C operand spellings: GNU 5.3.0 itself behaves erratically there (a printf -v 'A[$'a\\b']' operand created a literal $a\\b key on GNU); left untouched, worth its own ticket.
  • Item 4 (declare b=($h) plain-array entry-point) belongs to the quote-state family, as the ticket notes.

…ariable lookups (#734)

A key written through $'...' and the same key reached through another
entry point landed in different cells, and the internal marker leaked
into the stored representation:

  k=$'x\ty'; declare -A A; A[$'x\ty']=1
  printf '[%s]\n' "${A[$k]}"          # GNU: [1]   rubash: []
  declare -p A                            # GNU: [$'x\ty']  rubash: marker bytes in key
  declare -A B; B[$'a\\\\b']=1        # GNU key 61 5c 62  rubash key 61 62

Root cause. Both write and read funnel through the one
expand_subscript_string pass (GNU subst.c:11063), but its output kept
two decode products the finished key must not carry:

1. The ANSI_C_IFS_GUARD (U+E401) that escape_decoded_ansi_c_quotes puts
   before every decoded $'...' blank stayed in the resolved key. The
   guard-form key matched only another $'...' spelling; the variable
   and dq-literal spellings computed the clean bytes and missed the
   cell, declare -p rendered the marker inside the key, and the kvlist
   entry (which already strips) disagreed with the direct entry.

2. On the assignment entries the decoded backslash of $'a\\b' was then
   re-read as escape syntax by mask_subscript_escapes and dropped.
   GNU decodes the assignment word at parse time (parse.y:5551-5572
   read_token_word $' arm: ansiexpand :5566 + sh_single_quote
   re-quote), so the subscript reaches expand_subscript_string
   (arrayfunc.c:392-398 assign_array_element_internal) with the
   backslash single-quoted and it survives. The ${A[...]} reader hands
   the raw spelling to the runtime decode without that protection, so
   GNU's own read of ${A[$'a\\b']} keys on ab and misses the a\b cell
   — the asymmetry is GNU behavior and is pinned as parity.

Fix. expand_subscript_string strips the bare (E400-unescaped) guard
from the resolved key — user data U+E401 rides the E400 pair and
survives (rubash#735) — and the two assignment entries
(prepare_array_element_assignment_word, the quoted-assignment arm in
command_prepare) use expand_assignment_subscript_string, which doubles
the decoded $'...' backslashes for the escape pass to collapse, the
way GNU's parse-time quoting protects them.

Verification: 25-row differential against GNU bash 5.3.0 (WSL, script
files) — write/read cross-spelling matrix, stored-key bytes, declare -p
rendering bytes, membership (-v and ${+x}), unset via variable and via
raw ANSI-C operand, compound kvlist, user PUA U+E401, unrecognized
escape — all rows byte-identical after the fix. New corpus in
tests/issue734_assoc_ansic_subscript_regressions.rs (15 tests). Full
cargo test --no-fail-fast vs baseline 7047baf^: zero new failures,
zero flipped tests both directions (the 2 baseline-only failures are
the known-flaky executor::path cache tests, green in isolation).

Rebased over #788 (d248d9f, declare compound operand) — no overlap;
declare/assign.rs untouched.

@caomengxuan666 caomengxuan666 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent review (cr-822 @ b92b6e6, separate worktree)

Verdict: APPROVE-WITH-NITS — fix is correct and well-anchored; no regressions; two nits below, neither blocking.

Verified independently (WSL GNU bash 5.3.0 oracle, own probes)

  • Anchors check out: vendored arrayfunc.c — akey = expand_subscript_string (sub, 0) at :393 (the 392-398 assign_array_element_internal window); parse.y — ansiexpand at :5566 and the sh_single_quote re-quote at :5572 (5551-5572 $' arm). The reader/assignment phase-asymmetry story matches the sources and the observed GNU behavior.
  • Differential re-run, 4 sampled rows + extras, all byte-identical: guard-form row (${A[$k]} → [1]), item-3 key bytes (A[$'a\\b'] → 615c62), declare -p rendering bytes (identical hex, no ee9081), unset "A[$k]" → A=(). Extras: compound A=([k]=v) and declare -A B=([k]=v) entries, kvlist alternation, user $'\uE401' (ee9081 + [g], #735 E400-pair discipline holds), $'p\\q' (705c71), blank keys (" x "), trailing backslash (615c), mixed $'a\\$x' (615c2478), glob char (612a62), and the pinned reader asymmetry ([] / [1] / [1]).
  • Suites: issue734_assoc_ansic_subscript_regressions 15/15 green. Full cargo test --no-fail-fast --workspace failure sets on cr-822 vs baseline 7047baf are identical in my environment (41 env-dependent failures on both sides, zero flips in either direction); executor::path::tests 72/72 green in isolation.
  • Hygiene: cargo fmt --check clean; single commit over 7047baf, 4 files, nothing extraneous; PROVENANCE GPL discipline respected (line-ref citations as traceability pointers, no GPL fixtures in MIT paths). The pinned GNU quirk is clearly labeled in the corpus (ansi_c_backslash_read_asymmetry_matches_gnu, "pinned here as parity") and in expand_assignment_subscript_string's doc — if GNU ever fixes the asymmetry, the red diff will point straight at the decision.

Nit 1 — arithmetic assignment entry still mints raw-spelling keys (pre-existing; please annotate)

((A[$'x\\ty']=1)) and let are A[...]= assignment entries outside the two swapped entries, and they still store the undecoded spelling:

  • GNU 5.3.0: ((N[$'x\\ty']=1)) stores 78 09 79; ${N[$k]} with k=$'x\\ty' prints [1].
  • rubash (cr-822 and baseline alike): stores $'x\\ty' verbatim (24 27 78 5c 74 79 27), so the variable read prints [] — the ticket's item-1 symptom on a fourth write entry.

This is not a regression from this PR and the fix surface is right to keep narrow, but the PR body's out-of-scope list doesn't mention it while the ticket's own taxonomy calls this the "四入口各一份" family and the PR says "Closes #734 (items 1-3)". Suggest adding an out-of-scope bullet + follow-up ticket (the fix likely belongs in the arithmetic lvalue path, with its own differential — let "O[$'a\\\\b']=2" is additionally a GNU-inconsistent-operand row: GNU stores $a\\b, rubash $'a\\\\b').

Nit 2 — data point for the parked unset-operand follow-up

With the operand spelling that actually decodes to the stored key, GNU and rubash diverge (pre-existing, both sides of this PR):

declare -A A; A[$'x\\ty']=1
unset 'A[$'"'"'x\\ty'"'"']'   # operand is literally A[$'x\\ty']

GNU 5.3.0 keeps the cell (declare -p A=([$'x\\ty']="1" ) — unset does not ANSI-C-decode the operand); rubash removes it (A=()). The PR's r18 row covers the x\\\\ty spelling where both keep the cell, so this is just an extra row for the ticket the out-of-scope section already promises — no change requested here.

@caomengxuan666
caomengxuan666 merged commit 3c61129 into master Oct 18, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/734 branch October 18, 2026 17:40
caomengxuan666 added a commit that referenced this pull request Oct 19, 2026
…(rubash#828)

The fourth write entry — arithmetic ((...)) assignments and their let
"A[...]=v" form — stored the raw $'...' spelling instead of the decoded
key: ((A[$'x\ty']=1)) kept 24 27 78 5c 74 79 27 where GNU bash 5.3.0
stores 780979 and ${A[$k]} reads [1].

The assoc subscript of an arithmetic expression is expanded once by
expand_arithmetic_assoc_subscripts (arithmetic/mod.rs) before the parser
sees it, and that pass never touched the ANSI-C spans. GNU reaches
expand_subscript_string from this entry with the raw spelling, and the
runtime pass behaves differently per caller:

- ((...))/$((...))/nested-subscript readers: expand_word_internal's
  ANSI-C arm decodes the span with the product quoted, so decoded bytes
  ride as data and decoded backslashes survive (((D[$'a\\b']=2))
  stores 61 5c 62; the ${} reader keeps its pinned write/read
  asymmetry).
- let: the operand was word-expanded once (let.def passes EXP_EXPANDED),
  so $' is NOT re-recognized as ANSI-C — the $ stays literal data, the
  span quotes drop as shell quotes and the raw spelling rides verbatim
  (let "B[$'x\ty']=1" stores $x\ty = 24 78 5c 74 79). GNU's own
  scrambled-key quirk, pinned as parity.

expand_assoc_subscript_once now rewrites literal $'...' spans per entry
(ArithAnsiSpanMode::Decode / ::LetOperand — transform_arith_ansi_c_spans,
carrier-marked so the embedded walker expands only the text outside the
spans). Bare (non-$') spellings are deliberately untouched: the (( ))
body reaches the evaluator after the lexer's own escape pass, so the
bare-escape product stays as recorded in assoc corpus rows.

Known divergences that stay (probed 2026-10-13, pre-existing on master):
- bare escapes inside arithmetic subscripts keep one more backslash than
  GNU (((G[a\\b]=2)) second cell: x\ty vs xty);
- the unset operand that decodes to a stored key still deletes where GNU
  keeps the cell (CR Nit 2 — pinned in the new suite as the documented
  divergence for the parked unset-operand follow-up).

Tests: tests/issue828_arith_let_assoc_ansic_subscript_regressions.rs —
22 tests encoding the GNU-verified rows (storage bytes, cross-entry
reads, ++/command-read forms, let dq/sq operand forms, declare -p
rendering, membership, unset, array_expand_once boundary). GPL
discipline: behavior implemented from WSL GNU 5.3.0 probes; vendored
source used as semantic reference only.

cr-839 round 1 (BLOCKER): the span scanner now tracks single/double quote
state (mirroring #822 decode_ansi_c_spans� in_single discipline and
extending it with in_double — arithmetic subscript text carries live
dollar-quote-blind double-quoted segments). A $�...� span only opens
outside both quotes: ((E3["pre$�x�post"]=1)) stores the raw spelling
pre$�x�post (70 72 65 24 27 78 27 70 6f 73 74, GNU/master parity — was
prexpost), and �a$�b�c� sits back on the master-known raw shape
�27 61 24 27 62 27 63 27� (was abc, losing the dollar bytes). Three new
pins (both CR shapes + the let-entry forms through the shared scanner)
and an E3 row in the issue-828 catalogue case. Serial
(--test-threads=1) full-suite bidirectional comparison vs master
992d5c5: failure sets identical (38 vs 40; outliers are the known
signal-timing/path-cache env flakes, green in isolation on both sides —
issue526 fails 2/7 identically on master and this branch).
caomengxuan666 added a commit that referenced this pull request Nov 2, 2026
fix(#822-engine): winsize bind writes both storages
SpookyWaste pushed a commit to SpookyWaste/rubash that referenced this pull request Nov 12, 2026
bind_lines_columns inserted only into shell_state.env_vars (the mirror
read by get_env / child-env composition) while $name expansion reads
shell_state.variables (the record store) FIRST with a mirror fallthrough
(variable_state.rs shell_variable_value). Once a LINES/COLUMNS record
existed - parent env import at Executor::new (init.rs:949 ->
VariableStore::from_environment) or one user assignment - the
resize/startup bind left the record stuck at its old value while the
child env tracked: shell-internal and child views diverged (niubash#822,
Linux PTY probes A4/A8).

GNU citation (vendored tree, bash.git @9c465866):
- variables.c:1039-1055 sh_set_lines_and_columns: binds LINES (:1051)
  then COLUMNS (:1054) via bind_variable - ONE storage in GNU; rubash's
  variables/env_vars split is our implementation detail, and a bind
  writing only one of the two is the bug.
- variables.c:3225 bind_variable -> :3054 bind_variable_internal: an
  existing entry takes the assign_value: arm (:3151); only the value
  cell is replaced (:3207 var_setvalue) so attributes survive, and
  exported_p triggers the export-env rebuild (:3214-3215) so an
  exported COLUMNS stays exported with the updated value.

Fix: mirror set_env's dual-write (public_accessors.rs:839): after the
mirror insert, update the record's scalar in place; absent records stay
absent (the mirror fallthrough keeps serving them, matching GNU's
non-exporting fresh bind); the G-01 process-env mirror is unchanged.
bind_interactive_console_winsize and bind_interactive_screen_size ride
the same path.

WSL GNU Bash 5.3.x pins (evidence/822-dual/wsl/pin-output.txt): parent
exports COLUMNS=200 in a 30x100 pty -> $COLUMNS=100, child env
COLUMNS=100, declare -x COLUMNS="100"; resize -> tracks both ways;
user assignment COLUMNS=55 then resize -> 120 then 80/24; exported
assignment -> value tracks, stays exported.

Engine tests (api/consumer_tests.rs): the two record-present shapes
fail on base ($COLUMNS stuck at 200/55) and pass with the fix; a
no-record regression guard pins the mirror fallthrough and that the
bind creates no record and exports nothing.

pywinpty ConPTY matrix (headless, evidence/822-dual/matrix):
import-200 stuck at 200/55 across 100/120/80 columns pre-fix, tracks
with the fix; fresh-shell case byte-identical before/after.
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.

assoc subscripts via ANSI-C quoting do not match variable lookups, and declare -p leaks U+E401 into the stored key (bytes verified without od)

1 participant