Skip to content

fix: preserve quoted command substitution newlines - #7

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/quoted-command-substitution-newline
Jul 22, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/quoted-command-substitution-newline

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes command substitution field splitting so raw word metadata is used to detect whether $()/backtick substitution was unquoted. This preserves internal newlines for quoted command substitution while keeping unquoted field splitting behavior.

Tests:

  • cargo test command_substitution

@caomengxuan666
caomengxuan666 merged commit 2aa1421 into master Jul 22, 2026
3 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/quoted-command-substitution-newline branch July 22, 2026 14:00
caomengxuan666 added a commit that referenced this pull request Sep 26, 2026
…sh#144)

After a \' escaped quote the GNU lexer re-enters single-quote state:
parse.y:5366-5398 read_token_word consumes the escape pair without
opening quote state (pass_next_character covers exactly one char,
parse.y:5358-5362), so the next ' opens a fresh parse_matched_pair
span (parse.y:5419-5437). Inside that span every byte is quoted data
-- subst.c:11882-11886 expand_word_internal case '\'' hands the whole
span to add_quoted_string without consulting the comsub scanner, and
dequote_string (subst.c:4807) later removes only the delimiters.

Rubash's tokenizer (skip_word_inner, has_unclosed_quotes,
has_unclosed_command_substitution) already modeled this correctly;
the divergence was in assignment-word quote removal, which stripped
the '...' delimiters but left expansion triggers bare, so the
assignment expander (expand_backtick_substitution_typed /
expand_mixed_command_substitution_assignment via scan_substitution_spans)
executed them: x='a'\''`b`' ran `b` (rc=127, value truncated to a');
a lone backtick (c03/c04) died as a fatal EOF. This killed
bash-completion at bash_completion:188 and ssh.bash:457 on load.

Fix, by the protect_fully_single_quoted_assignment convention
(classification.rs) and f8b6328's word-wide protection:

- quotes.rs remove_shell_quotes_outside_backticks: content of '...'
  spans now travels as data carriers ($ -> \x1f, ` -> \x1a,
  " -> \x18), matching how fully-single-quoted RHSes already travel.
- quotes.rs new remove_shell_quotes_assignment (word.rs $(-branch):
  same carrier protection for assignment words mixing spans with a
  live $(...) (`x='a'\''`b`'$(echo z)` keeps `b` literal).
- The word path deliberately stays unprotected: plain `echo 'a`b'`
  prints through paths that do not decode \x1a (probed: leaks \x032).
- assignment_expansion.rs: drop the `\'`->`'` collapse in the quoted
  restore block. Source \' already arrives as the bare \x17 carrier;
  a literal \ in front of it is data from the end of a single-quoted
  span (bash_completion:188 `'^([^\''` stores `^([^\` with the
  backslash; the collapse ate it).

Unblocked: `source bash_completion` rc=0, complete -p = 130 = GNU's
131 env-free entries minus the known complete -D gap (rubash#133);
ssh.bash loads rc=0; 452-file isolation matrix 451 OK vs GNU 452
(sole fail = tar.bash regex `{n,m}` quantifier, pre-existing bucket
#7). probes/mini c01-c05 match GNU byte-for-byte; 36-case class
matrix 32 pass (all 5 remaining divergences pre-exist on the base
build and live in word-position consumers -- case patterns,
here-strings, pipeline word decode -- tracked for the WORD_DESC
quote-flags port).

Verified: cargo build 0 warnings, cargo test --lib 446 green,
cargo check --tests for x86_64-unknown-linux-gnu and
aarch64-apple-darwin clean, rustfmt clean. src/lexer/continuation.rs
NOT touched (its \' tracking was already correct).
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