Repository navigation
WIP: rubash 2216+2217 — dollar-bracket arith in dquotes + expand_word result protection - #2226
Merged
Merged
Conversation
rubash#2217: old-style arithmetic $[expr] printed literally inside
double quotes (echo "x=$[1+1]" gave x=$[1+1]; GNU gives x=2). The
expansion walkers already had $[ arms; the gap was the lexer's
double-quote cookers in src/lexer/quotes.rs: a $ not followed by
(, {, a special, or a name-start was encoded as DATA_DOLLAR
(literal data), so the cooked word never presented a live $[ to
either walker. The raw double-quoted span copiers likewise ended
a span at a " inside $[...] (no $( -style skip).
Fix by mechanism, mirroring the existing $( handling in the same
functions: both cookers and both raw span copiers now recognize
$[ and copy the bracket body as one unit, with plain bracket
counting — the same rule the parse.y hunt port and both walker
collectors already use. Cookers additionally resolve \\ and \"
inside the body to the single character (the only resolutions
the arithmetic pre-expansion does not itself perform there);
raw copiers copy verbatim because their spans are re-cooked
later. GNU anchors: subst.c:10912 param_expand case '[' hands
the span to extract_arithmetic_subst (subst.c:1301), built on
extract_delimited_string (subst.c:1361), and evaluates through
the shared arithsub path (expand_arith_string with
Q_DOUBLE_QUOTES|Q_ARITH, subst.c:10868); parse.y:3877
parse_matched_pair counts brackets without quote tracking.
Regression pins in tests/issue2217_dollar_bracket_arith_regressions.rs
(8 tests: dquoted basic/variants/inner quotes/bad expression,
assignment positions, unquoted + heredoc controls, literal
contexts, unterminated EOF verdict), each verified against the
GNU oracle (bash 5.3.20) as a script file, red on master, green
here. Residual, deliberately not pinned: quote-state-aware
extraction for pathological shapes ($["1+1"] with unescaped
quotes inside an enclosing dquote span, $[1\]] close-bracket
escaping) — GNU's extract_delimited_string tracks quote state,
rubash's layers uniformly count brackets; master diverged on
those shapes too. See the cluster REPORT.
Refs #2217
rubash#2216: redirect operands expanded through the &self expand_word gate hit the same trailing-unescape corruption #2210 fixed in the &mut walker: with body='a\(b', > "t_$((1+1))_$body" created t_2_a(b; GNU creates t_2_a\(b. expand_word's trailing pass (unescape_remaining_shell_escapes plus the \' collapses, gated on the source word containing $( or a backtick) runs over the assembled string and cannot tell expansion-data backslashes from deferred source escape pairs. Port the #2210 protect+restore invariant to the non-mut walker behind a flag scoped to the expand_word path only: - expand_embedded_parameters_inner gains a protect_results parameter; every existing entry passes false (byte-identical behavior for all ~25 consumers: patterns, heredoc, compound assignments, parameter transforms, the braced-expansion tree). - The new entry expand_embedded_parameters_protecting_results passes true and is called only from expand_word's gated path. In that mode, user-data splice sites (plain name, ${} result, $@, $*, positional digits) carry their backslashes as ASSIGN_EXPANSION_BACKSLASH — the registry carrier #2210 used — via protect_result_backslashes; data backslashes the walk itself emits (DATA_BACKSLASH restore at the tail, the literal fall-through push, single-quoted-span content) ride as the same carrier. Numeric-only arms ($?, $$, $!, $#, $-, $(( )), $[ ]) need no protection; command-substitution results already carry their own protection. - expand_word restores the carrier to \ immediately after its trailing pass, unconditionally, so the carrier never leaks to callers when the gate does not fire. GNU invariant (carrier justification per AGENTS.md, against parse.y:5694-5706 got_escaped_character): expansion results merged into a word are CTLESC-protected at splice time — subst.c:4773 quote_string, applied at subst.c:11862 add_quoted_string — and subst.c:4807 dequote_string strips only the protection bytes; assembled data is never re-scanned as source. GNU redir.c redirection_expand receives the word already protected the same way. Pins: issue2210_redirect_target un-ignored (now green) plus issue2216_redirect_comsub_trigger, issue2216_redirect_source_literal, issue2216_redirect_var_only_control in tests/issue2210_arith_dquote_rescan_regressions.rs; all #2210 pins in that file still pass. Verified byte-for-byte against the GNU oracle as script files (redirect filename matrix and the #2210 probe batteries). Refs #2216
caomengxuan666
marked this pull request as ready for review
October 30, 2026 13:54
This was referenced Oct 30, 2026
caomengxuan666
added a commit
that referenced
this pull request
Oct 30, 2026
Master carries PR #2218 (rubash#2211: comsub cd session-cwd anchoring, operand decline gates, logical_destination_display // seam fix in paths.rs), PR #2224 (census) and PR #2226 (walker) since this branch forked at 01836d3. One conflict, in command_substitution_cd_pwd_output_impl (src/executor/command_substitution.rs), resolved as the union of both sides per the cluster REPORT overlap map: - operand read stays quote-aware (split_shell_words_with_quote_info, #2219): the raw-empty decline is decided pre-expansion; - #2211's post-expansion declines kept (leading '-', CDPATH search, expanded-empty) and operand_form is set only after every decline has passed; - #2219's unquoted-blank (field-splitting) decline kept; - downstream untouched: session-cwd anchoring (#2211), the cdable decline keyed on operand_form, and the #2220 logical rendering tail over paths.rs's seam-fixed logical_destination_display. Re-verified on the merged tree (Linux): issue2219 9/9, issue2220 9/9 (incl. the //etc pin), issue2221 5/5, issue1415 adjacency 1/1, issue2211 14/14 (the session-// + relative-operand seam cells are pinned there), --lib 983 pass / 0 fail.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #2216
Refs #2217
Two sibling faces of the #2210 walker work, one commit per issue.
Refs #2217 —
$[expr]inside double quotes (commit 50a31d9). The expansion walkers already had$[arms; the gap was the lexer's double-quote cookers encoding the$as DATA_DOLLAR before any walker could see it, and the raw span copiers ending a span at a"inside$[...]. Both cookers and both raw copiers now carry$[...]as one unit with plain bracket counting (the rule the parse.y hunt port and both walker collectors already use); cookers resolve\\/\"in the body, raw copiers stay verbatim. GNU: subst.c:10912param_expandcase[, subst.c:1301extract_arithmetic_subst, subst.c:1361extract_delimited_string, parse.y:3877parse_matched_pair. New pins:tests/issue2217_dollar_bracket_arith_regressions.rs(8 tests).Refs #2216 — redirect operands through the
expand_wordgate (commit 92e62e8). Ports #2210's protect+restore invariant to the non-mut walker behind a flag set only by the newexpand_embedded_parameters_protecting_resultsentry, called only fromexpand_word's gated path; all other entries passfalse, so the ~25 other consumers are byte-identical. User-data splice sites carry backslashes as ASSIGN_EXPANSION_BACKSLASH;expand_wordrestores the carrier right after its trailing unescape pass. GNU: subst.c:4773quote_string, subst.c:11862add_quoted_string, subst.c:4807dequote_string. Pins:issue2210_redirect_targetun-ignored +issue2216_redirect_comsub_trigger/issue2216_redirect_source_literal/issue2216_redirect_var_only_control.Local verification (targeted, build slot): both pin files green (15 + 8 tests); script-file probe batteries byte-identical to the GNU oracle (bash 5.3.20) for the
$[...]shape matrix, the redirect filename matrix, and the #2210 probe2/probe3 batteries. Full verification rides this PR's CI.Known residual (documented in the cluster REPORT, not pinned): quote-state-aware delimited extraction for pathological shapes (
$["1+1"]with unescaped quotes inside an enclosing dquote span;$[1\]]escaped close bracket) — GNU'sextract_delimited_stringtracks quote state while all rubash layers count brackets; master diverged on those shapes as well.