Skip to content

fix(expansion/pipeline/invocation): four fast-path fidelity bugs found via differential testing vs GNU bash - #101

Merged
caomengxuan666 merged 4 commits into
unixwin:masterfrom
SuperYogurt:fix/quote-fidelity-fastpaths
Sep 14, 2026
Merged

caomengxuan666 merged 4 commits into
unixwin:masterfrom
SuperYogurt:fix/quote-fidelity-fastpaths

Conversation

@SuperYogurt

Copy link
Copy Markdown
Contributor

Why

rubash is used daily as a Windows replacement shell under an agent harness (thousands of rubash -c '<script>' invocations against GNU bash reference behaviour). A 36-family byte-exact differential battery against GNU bash surfaced four fast-path fidelity bugs: places where an emulation shortcut produced silently wrong output instead of failing loudly. Each reproduction below is byte-checked against GNU bash after the fix.

Commits

1. fix(invocation): accept combined -lc/-ilc without exploding long options

bash -lc 'echo hi' is rejected; harnesses and tmux default-shell spawn exactly this form. Also adds a guard the previous combined-flag expansion lacked: long options must never be exploded (--rcfile was split into -r -c -f ... once l joined the whitelist, because every character of rcfile is a whitelisted flag char).

2. fix(expansion): preserve quote literals through cmdsub and assignment-word re-expansion

Command substitution's word-based fast paths use a splitter that drops quote delimiters, but GNU subst.c always parses the body — so bodies whose quotes must survive into the output arrive corrupted:

v=$(echo '{"a":1}')   # printed {a:1}, bash prints {"a":1}
echo "a\"b"           # printed ab — a de-escaped `"` reached the word
                      # scanners as a bare delimiter and was swallowed
                      # (lexer now emits the \x18 data-DQ marker)
printf '[%s]' a='b c' # printed [a=b][c] — the `=`-shaped argument word
                      # lost its single quotes across the
                      # embedded-parameter re-scan; now hoisted with the
                      # same sentinel the assignment-storage path uses
$(for i in 1 2 3; do echo $i; done; echo x)
                      # rejected: split_shell_words keeps `;` attached to
                      # `done`, so the closed-compound precheck misread
                      # "done;" as unclosed

The routing predicate (command_substitution_quotes_are_semantic) deliberately fires only for quotes whose literal value is semantically visible (nested quotes, \"/\ inside "): top-level delimiters merely group words, stripping them matches bash, and bodies routed through the full executor regress the heredoc old-style backtick literal and the IFS read splitting cases (verified: those two probes fail with an unconditional detour).

Pins six quote shapes in command_substitution_preserves_nested_quote_literals.

3. fix(pipeline): fall back to real commands when inline filters see unmodeled flags

Inline emulations of sort/head/grep/tail/uniq inside $(... | cmd) silently swallowed or mis-consumed flags they do not model — the worst kind of failure because output looks fine:

echo "$(printf 'b\na\nc\n' | sort -r)"   # printed a b c (ascending)
echo "$(printf 'foo\nbar\nfoo\n' | grep -c foo)"
                                         # printed nothing — "-c" became the pattern
echo "$(printf 'a\na\nb\n' | uniq -d)"   # printed a b (plain dedup)
echo "$(printf 'x\ny\nz\n' | head -c 4)" # printed all three lines (line counting)

Each arm now bails out to the real parser whenever operands are not exactly the modeled shape. Pins GNU bash's byte-exact output in cmdsub_inline_filters_run_real_commands_for_unmodeled_flags.

(The companion bug — the pipeline first stage spawning through CreateProcess and resolving to System32 sort.exe — was fixed here in an earlier revision of this branch, but is superseded upstream by c59c5da4 (run external pipelines through the real executor, issue #76), which covers it from the better side. That commit is intentionally not included.)

4. test(tilde): unique fixture roots under thread-parallel cargo test

tilde_user_resolves_passwd_home flakes under cargo test: fixtures are named per-process while tests run as threads of one process, so concurrent runs share (and remove_dir_all) each other's root. Per-call unique roots.

Test plan

  • cargo test --lib: 384 passed, 0 failed (Windows host + Linux CI expected identical)
  • cargo test --test cli_tests: full-suite differential against origin/master run on the same machine with exact failure-set comparison (not just counts): 0 new failures, and 69 failing tests on master become 65 on the branch — the quote-fidelity changes in commit 2 also make four preexisting failures pass (bashdb_compat::eval_can_reparse_quoted_associative_array_append, bashdb_compat::line_continuation_after_apostrophe_inside_double_quotes_keeps_one_command, bashdb_compat::quoted_assignment_like_argument_is_not_array_assignment_parse_error, compat_issue_regressions::quoted_process_substitution_stays_literal), plus the 2 new regression tests.

Known-deferred (not in scope, found by the same battery)

  • A [ directly after the opening quote of a double-quoted word around a $(...) leaks the 0x11 glob carrier into output — systemic marker-protocol work, test shapes in commit 3 avoid it on purpose.

Harnesses spawn '<shell> -lc <script>'; GNU bash parses the combined
login+command form. Expand combined short flags for 'l' as well, but
only for single-dash words: the previous 'l' whitelist addition also
exploded '--rcfile' (every char whitelisted) into '-r -c -f ...'.
…-word re-expansion

Command substitution took word-based fast paths whose splitter drops
quote delimiters, so bodies with semantically visible quotes arrived at
the emulated command corrupted: $(echo '{"a":1}') reached echo as the
bare word {a:1}, and a de-escaped \" inside double quotes degraded to a
literal backslash (lexer emitted a bare quote that downstream scanners
toggled state on and swallowed: echo "a\"b" printed ab, bash prints
a"). Route such bodies through the real parser/executor; top-level
quote delimiters merely group words and keep the fast paths the heredoc
backtick-literal and IFS read regressions rely on.

Also:
- argument words shaped name='a"b' lost the quote across the
  embedded-parameter re-scan (parameter_core) — hoist data quotes with
  the same sentinel the assignment-storage path uses
  (hoist_data_double_quotes, now pub(in crate::executor));
- the unclosed-compound precheck misread 'done; echo x' as an unclosed
  loop because split_shell_words keeps ';' attached to the terminator
  word — trim trailing separators before comparing;
- regression test command_substitution_preserves_nested_quote_literals
  pins six quote shapes against GNU bash output.
…odeled flags

The inline emulations of sort/head/grep/tail/uniq inside cmdsub
pipelines silently ignored or mis-consumed flags they do not model:
sort -r ran as sort -u, head -c emitted whole lines, a grep flag
(e.g. -c) became the pattern, any uniq flag was dropped. Bail out to
the real parser (which runs the actual tool) whenever the operands are
not exactly the modeled shape.
Per-process temp roots let concurrent tilde_* tests share (and
remove_dir_all) each other's passwd fixture, making
tilde_user_resolves_passwd_home flake; give every call its own root.
@caomengxuan666
caomengxuan666 merged commit f9a4e4e into unixwin:master Sep 14, 2026
@caomengxuan666

Copy link
Copy Markdown
Member

Thank you @SuperYogurt for this contribution — merged. 🙏

The four fixes here are exactly the class of bug that's hardest to catch by hand: fast paths that produce plausible-looking but wrong output instead of failing loudly. Byte-exact differential testing against GNU bash is the right way to surface them, and the guard against exploding long options (--rcfile → -r -c -f ...) was a nice catch on top of -lc itself.

The quote-literal preservation work (commit 2) also fixed four pre-existing test failures as a bonus — much appreciated.

We'll credit this in the next release notes. If you run more differential batteries, the "known-deferred" \x11 glob-carrier leak after "$(...)" would be a great follow-up issue/PR — happy to collaborate on it.

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.

2 participants