Skip to content

fix(lexer): complete the fully-single-quoted for-word fast path (#629) - #633

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/issue629-data-dquote
Oct 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/issue629-data-dquote

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

What

Closes #629.

The fully-single-quoted for-list fast path in
expand_for_word_values_result (src/executor/command_words.rs) stands in for
the whole expansion walker, but returned a word the walker would never produce.
Two downstream finalisation steps were missing.

1. The quote-data carrier decode. The '-quoted interior carries
DATA_DOLLAR (0x1f, markers.rs:135), DATA_DQUOTE (0x18, markers.rs:89)
and DATA_BACKTICK (0x1a, markers.rs:95) for every expansion-trigger byte
inside it (lexer/quotes.rs:321/325/329), and the walker's last step decodes
the whole family (decode_word_position_carriers, markers.rs:378). The fast
path restored only the protected-dollar marker:

return Ok(vec![word.replace('\u{1f}', "$")]);

so the loop variable was bound with live 0x18/0x1a bytes, and the raw compound
element serialised them back through quote_array_value / decode_ansi_c_quoted
as $'...\030...'.

2. The word-prefix marker drop. A word that begins with a quoted tilde is
prefixed with QUOTED_WORD_PREFIX (0x1b, markers.rs:107) so that no later
tilde pass touches it; the walker drops it in expand_marked_or_special_word
(expand_word.rs:515). The fast path kept it, so for v in '~' bound \x1b~
(a second defect in the same contract, pre-existing on master).

Why (GNU C source is the spec)

GNU keeps no marker inside a single-quoted span: parse.y:3877 parse_matched_pair
reads the interior literally, only CTLESC/CTLNUL are protected
(parse.y:5694 got_character), and subst.c:4807 dequote_string is the only
removal. A quoted tilde is literal too — the tilde is special only unquoted, at
word start. So the fast path must run the same whole-family decode and the same
prefix drop: it must return what the walker returns for this word.

Fix

Strip QUOTED_WORD_PREFIX, then call decode_word_position_carriers(word).

a=(); for v in 'he said "hi"'; do a+=("$v"); done; printf %s "${a[0]}" | od -An -tx1
  master: 68 65 20 73 61 69 64 20 18 68 69 18   (live 0x18 carriers)
  branch: 68 65 20 73 61 69 64 20 22 68 69 22
  GNU   : 68 65 20 73 61 69 64 20 22 68 69 22

for v in '~'; do printf %s "$v" | od -An -tx1
  master: 1b 7e
  branch: 7e
  GNU   : 7e

Verification

  • Class differential vs GNU: 880 generated cases (for-word quoting shape ×
    consumer: =, +=, [i]=, +=([i]=), a[i]=, scalar, bare binding, with
    $/`/"/~/glob content) compared byte-for-byte with od.
    Branch 880/880; master 821/880 — the 59 divergences are all inside this
    change's blast radius and are fixed here, none introduced.
  • Hand matrix: 16/16 byte-exact; the leading-tilde family ('~', '~x',
    '~root') matches GNU where master emitted the 0x1b prefix.
  • cargo test --lib: 584/584.
  • Test-target failure sets vs master 2f40ddb, serial
    (--test-threads=1): cli_tests 1 vs 1; executor_tests 1172 = 1172 with
    only the nondeterministic command_chaining::part_080 rows flipping (each
    passes in isolation; the same family flips in the other direction between runs
    of master itself); array_compound_quote_regressions,
    issue316_for_list_glob_regressions, issue316_quoted_head_glob_residue,
    marker_leak_golden, array_store_parity all 0 = 0.

Measured on Termux/Android aarch64 against the local bash 5.3.20; the repository
oracle is WSL bash 5.3.0, and the rule being applied is taken from the vendored
5.3 source (parse.y:3877, parse.y:5694, subst.c:4807) rather than from a
version's behaviour.

The fully-single-quoted for-list fast path in expand_for_word_values_result
stands in for the whole expansion walker, but returned a word the walker would
never produce. Two downstream finalisation steps were missing.

1. The quote-data carrier decode. The `'`-quoted interior carries DATA_DOLLAR
   (0x1f, markers.rs:135), DATA_DQUOTE (0x18, markers.rs:89) and DATA_BACKTICK
   (0x1a, markers.rs:95) for every expansion-trigger byte inside it
   (lexer/quotes.rs:321/325/329), and the walker's last step decodes the whole
   family (decode_word_position_carriers, markers.rs:378). The fast path
   restored only the protected-dollar marker:

       return Ok(vec![word.replace('\u{1f}', "$")]);

   so the loop variable was bound with live 0x18/0x1a bytes, and the raw
   compound element serialised them back through quote_array_value /
   decode_ansi_c_quoted as `$'...\030...'`.

2. The word-prefix marker drop. A word that begins with a quoted tilde is
   prefixed with QUOTED_WORD_PREFIX (0x1b, markers.rs:107) so that no later
   tilde pass touches it; the walker drops it in expand_marked_or_special_word
   (expand_word.rs:515). The fast path kept it, so `for v in '~'` bound
   `\x1b~`.

GNU keeps no marker inside a single-quoted span: parse.y:3877 parse_matched_pair
reads the interior literally, only CTLESC/CTLNUL are protected (parse.y:5694
got_character), and subst.c:4807 dequote_string is the only removal. A quoted
tilde is literal too - the tilde is special only unquoted, at word start. So the
fast path must run the same whole-family decode and the same prefix drop: it
must return what the walker returns for this word.

Fix: strip QUOTED_WORD_PREFIX, then call decode_word_position_carriers(word).

Byte evidence (`printf %s "${a[0]}" | od -An -tx1`) for
`a=(); for v in 'he said "hi"'; do a+=("$v"); done`:
  before: 68 65 20 73 61 69 64 20 18 68 69 18  (raw 0x18 carriers)
  after : 68 65 20 73 61 69 64 20 22 68 69 22  == GNU bash
and for `for v in '~'; do printf %s "$v"`:
  before: 1b 7e
  after : 7e  == GNU bash

Regression tests added to tests/array_compound_quote_regressions.rs: the
compound `=`, `+=`, `[i]=` and `+=([i]=)` forms, the backtick and dollar
carriers, the leading-tilde prefix (`'~'`, `'~user'`, `'~/`', `'~$x'`), the
non-prefix tilde (`'x~y'`), and the clean paths (`a[0]="$v"`, non-fully-quoted
for-words).

Gates: lib 584/584. Byte-exact differential against GNU over 880 generated
cases (for-word quoting shape x consumer, compared with od): branch 880/880,
master 821/880 - the 59 pre-existing divergences are all in this change's blast
radius and are now fixed, none introduced. A 16-case hand matrix is 16/16, and
the leading-tilde family (`'~'`, `'~x'`, `'~root'`) matches GNU where master
emitted the 0x1b prefix. Test-target failure sets vs master 2f40ddb, compared
serial (--test-threads=1): cli_tests 1 = 1 with no new failure;
array_compound_quote_regressions, issue316_for_list_glob_regressions,
issue316_quoted_head_glob_residue, marker_leak_golden and array_store_parity are
0 = 0; executor_tests is 1172 = 1172 apart from the nondeterministic
command_chaining::part_080 coproc family, whose membership moves between runs of
the same tree (three consecutive runs gave 1173, 1172 and 1174, each with a
different set of part_080 rows), and every flagged row passes in isolation on
both the branch and master - host-timing flakiness, not a regression.

Measured on Termux/Android aarch64 against the local bash 5.3.20; the repository
oracle is WSL bash 5.3.0, and the rule being applied is from the vendored 5.3
source (parse.y:3877, parse.y:5694, subst.c:4807) rather than from a version.
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.

for-list word with a double quote leaks the DATA_DQUOTE carrier into compound array append

1 participant