Skip to content

fix(executor): alternate rhs $ escape resolves at walk time (Refs #3116) - #3684

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3116-alternate-dollar-escape
Nov 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3116-alternate-dollar-escape

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Problem

echo [${x:-a\$b}] printed [ab]/[a\\bZZ]-style wrong output: in an unquoted ${var:-word} alternate rhs, rubash pushed the raw \ and let the next walk iteration re-scan $ as a live expansion trigger.

Root cause

GNU runs the alternate rhs through parameter_brace_expand_rhs (subst.c:7966) → expand_string_for_rhs (subst.c:4462) → expand_word_internal (subst.c:11229), whose backslash branch (subst.c:11614-11678) consumes \ before ANY character in unquoted context and emits the char CTLESC-quoted — \$ is a literal $ that can never re-trigger expansion.

Rubash's non-mut walker (expand_embedded_parameters_inner) only consumed \'/\" in the unquoted branch; every other pair fell through with the backslash emitted as text and the following char re-scanned live. The trailing context-free unescape_unquoted_rhs_escapes could only save pairs that survive intact — hence $ (live trigger) was wrong while \q/} were right by accident.

Fix

In the protect_ifs+protect_data walk — expand_parameter_word's rhs engine (parameter_words.rs:291→311) — the backslash arm now resolves live triggers at walk time where quote state is still known: \$→DATA_DOLLAR, \``→DATA_BACKTICK, \→ASSIGN_EXPANSION_BACKSLASH carrier, \→line join, "/' keep their literal-char results. Pairs GNU leaves to quote removal (\q, }`, escaped whitespace→IFS_GLUE) still fall through to the caller's trailing pass unchanged.

Gated on protect_ifs && protect_data, which is exclusively the alternate-rhs walk (expand_embedded_parameters_protect_ifs_and_data); every other consumer is byte-identical.

Test legs

  • cargo test --test issue3116_alternate_source_dollar_escape: 23/23 green (was 16/23 on master; the 7 RED cells were the $ live-trigger family)
  • issue3117_alternate_dq_span_backslash: 11/24 now green (CBSDQUOTE cells $, \, ", `, \ all pass; the 13 non-CBSDQUOTE span cells are [bug] alternate word: dq-span \q wrongly unescaped (${x:-"a\qb"} yields [aqb] not [a\qb]) #3117's own fix)
  • RUSTFLAGS=-D warnings cargo check --bin rubash: clean
  • GNU reference: subst.c:11614-11678 expand_word_internal backslash branch, syntax.h:58 CBSDQUOTE
  • fmt: cargo fmt hit Windows os error 206; used per-file rustfmt --edition 2021 as sanctioned fallback

Generated with Devin

)

GNU subst.c:11614-11678 expand_word_internal's backslash branch consumes
\\ before ANY character in an unquoted context and emits the character
CTLESC-quoted, so \$ in a ${var:-word} alternate rhs is a literal $
that can never re-trigger expansion. The non-mut walker's unquoted
backslash arm pushed the raw \\ and re-scanned $ as a live trigger;
in the protect_ifs+protect_data walk (expand_parameter_word's rhs
engine) live triggers now emit their carriers (DATA_DOLLAR /
DATA_BACKTICK / ASSIGN_EXPANSION_BACKSLASH) at walk time, where quote
state is still known. Pairs GNU leaves to quote removal (\q, \},
escaped whitespace) still fall through to the caller's trailing pass.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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