Skip to content

[P0] fix(#511): cat inline path re-expands already-expanded argv - silent wrong-file read - #552

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/cat-fastpath-quote-passthrough
Oct 12, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/cat-fastpath-quote-passthrough

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #511. P0: silent wrong-file read = silent data corruption class.

Repro (from the issue)

FIX=zz; mkdir zz; echo hi > zz/c; cat '$FIX/c'

GNU: cat: $FIX/c: No such file or directory, status 1. rubash (before this PR): silently printed hi — it read zz/c. Escaped \$FIX/c was equally broken; echo/head/wc and redirect targets were fine because only the cat inline path ran a second expansion over its operands.

Root cause

external_cat's file-operand loop (src/executor/external_file_builtins.rs) ran each operand through expand_word again. The simple-command executor had already expanded the argv once (expand_command_words → execute_cmd.c execute_disk_command -> expand_words), so a single-quoted operand arrives with its quotes removed and a live-looking $ — the second pass re-scanned that literal text and opened a different file. The same file's external_sed fixed this exact pattern for sed in rubash#177 ("Re-running expand_word here would be a second expansion pass … Use the argv as-is"); cat was missed.

Fix (minimal, one loop): use the argv as-is in the operand loop. Option parsing (parse_cat_argv) already classifies from the same final argv — getopt sees post-expansion text exactly like the real cat.exe — and pathname expansion happened once in expand_command_words before dispatch, so the second pass never carried semantic weight. Untouched: option parsing, /dev/fd/N and <(cmd) operands, - stdin handling, and the inline pipeline stage / $(...) cat arms (those operate on raw pre-expansion words and keep their own first-pass expansion — verified, see tests).

Bisect conclusion — #501 is exonerated

The issue suspected the #501 hot-path rework. Probing with real rubash.exe builds (see hygiene note below):

commit reproduces?
896ef0d4 (chore: init — the initial import) BAD
v1.1.0 (a313729b) BAD
41de8319 (#500, the range's good anchor) BAD
a800dc5b (#501) BAD
origin/master (1245798b) BAD

Every commit of 41de8319..origin/master reproduces, back through the anchor to the initial import. The double expansion is as old as the repository; there is no introducing commit in history and #501 did not regress anything. The second expansion of cat operands predates even the external_cat refactor (it was inherited by 1bcae28/#415 from the earlier cat_file_operands loop).

Bisect/test hygiene note (why the suspicion was plausible)

target/debug/bash.exe in this repo is not rubash — it is the Windows install shim (src/bin/bash.rs) that forwards argv to the host shell. Any behavior bisect that spawns bash.exe (instead of CARGO_BIN_EXE_rubash → rubash.exe) silently tests the host shell and reports uniform results. The initial "first bad = a800dc5" impression came from exactly that trap; all conclusions above use rubash.exe.

Tests

tests/issue511_cat_quoted_operand_regressions.rs — 9 cases: the issue repro (single-quoted $FIX/c), escaped \$, unset-variable literal error text, double-quoted/unquoted expansion controls (GNU parity both ways), $(...)/backtick-in-single-quotes-stays-data, mixed-operand failure-then-continue semantics (GNU cat.c), pipeline-stage cat, $( ) cat, and a --operand stdin guard. 6/9 fail on unfixed master, 9/9 pass with the fix.

Full matrix (cargo test --no-fail-fast, this host, debug)

  • fixed branch: 168 suites, 10 failing targets — 69 named failures
  • fresh origin/master: same 10 targets — 69 named failures
  • bidirectional by-name diff: empty (the known host-env families: bashdb, pwd logical-mode, path virtual-system-root, heredoc-fd, upstream-suite-slice fixtures, …). New 511 tests pass on the branch and do not exist on master, as expected.

#501 semantic probes — no collateral

22 purpose-built probes over every surface #501 touched (test marks -t/-v/arrayref/-t-as-data, literal/quoted/glob/ANSI-C/comsub-payload words, assignment operators =/+=/:=/readonly, IFS-guarded $'...', $_/DEBUG-trap command text, function calls, arith loops, pipeline read), run on fresh origin/master vs this branch: stdout+stderr+rc byte-identical on all 22, and the cached debug switches (RUBASH_TRACE_APPAE=1, RUBASH_DBG_AAV=1) produce identical output with the OnceLock caching.

cargo fmt --check clean.

Same-class follow-ups (deliberately NOT in this P0)

The sibling wrappers in the same file re-expand already-expanded argv with expand_word exactly like cat did: external_mkdir, external_touch, external_cp, external_rm, external_rmdir (e.g. mkdir '$FIX/d' creates zz/d). Same silent-corruption class; they deserve their own issue + tests rather than widening this P0's blast radius.

…nsion pass

The file-operand loop ran each operand through expand_word AFTER the
simple-command executor had already expanded the argv once
(execute_cmd.c execute_disk_command -> expand_words). A single-quoted
or escaped operand like '$FIX/c' arrives at that loop with its quotes
removed and a live-looking dollar, so the second pass re-scanned the
literal text and silently opened zz/c where GNU reports
'cat: $FIX/c: No such file or directory' (status 1) - a silent
wrong-file read (issue #511, P0 data-corruption class).

Use the argv as-is, the same rule external_sed adopted in rubash#177.
parse_cat_argv already classifies options from the same final argv
(getopt sees post-expansion text, exactly like the real cat.exe), and
pathname expansion happened once in expand_command_words before
dispatch, so dropping the second pass restores single-expansion
semantics without touching option parsing, /dev/fd operands, '-' stdin
handling, or the inline pipeline stage arm (which operates on raw
pre-expansion words and keeps its own first-pass expansion).

Bisect (2026-10-12, real rubash.exe probes): every commit of
41de831..origin/master reproduces, back through 41de831 to the
initial import 896ef0d (chore: init) - the double expansion predates
the #501 hot-path rework the issue suspected; no introducing commit
exists in history. Tests: tests/issue511_cat_quoted_operand_regressions
(9 cases; 6 fail on unfixed master, 9 pass with this change).
@caomengxuan666
caomengxuan666 merged commit b699e2e into master Oct 12, 2026
8 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 12, 2026
… argv - the #511 family, closed (#558)

Five sibling wrappers in external_file_builtins.rs ran their operands
through expand_word AFTER the simple-command executor had already
expanded the argv once. GNU bash-5.3 expands simple-command words
exactly once, at dispatch: execute_cmd.c:4617 (execute_simple_command)
runs the single expand_words pass, execute_cmd.c:4938 hands the
expanded words to execute_disk_command, and those words become the
final execve argv (execute_cmd.c:6126 shell_execve -> :6133 execve) -
nothing downstream re-scans operand text. Redirections follow the same
once-only timing (redir.c:789/:909 redirection_expand, then
redir.c:701 redir_open opens the result verbatim). mkdir/touch/cp/rm/
rmdir are coreutils utilities in GNU: the shell contracts with them on
final argv, and the utility's diagnostics quote the operand verbatim
(verified behaviorally on WSL 5.3.0, script-file probes, 2026-10-12).

A single-quoted or escaped operand like '$FIX/d' therefore arrives at
those wrapper loops with its quotes removed and a live-looking dollar;
the second expand_word pass re-scanned that literal text and silently
created, touched, copied into, or REMOVED the wrong path: 'mkdir
$FIX/d' built zz/d, and 'rm $FIX/r' deleted an existing zz/r
with exit 0. Same P0 silent-corruption class as #511 (fixed by #552 for
cat) and #177 (sed, same file).

Use the argv as-is in every operand loop (mkdir, touch, cp, rm, rmdir)
and in cp's -t/--target-directory value consumption; pathname/glob
expansion already happened once at dispatch. rubash's own wrapper
leniencies (mkdir's create_dir_all implies -p, rmdir swallowing
per-operand errors) are pre-existing contracts this change does not
alter.

Tests: tests/issue555_file_builtin_family_regressions (22 cases; the
family table + shared assert_literal_operand_contract helper covers one
row per wrapper so the next wrapper added to this file cannot quietly
reintroduce the second expansion; live-expansion controls included;
19/22 fail on unfixed master, 22/22 pass with this change).
@caomengxuan666
caomengxuan666 deleted the fix/cat-fastpath-quote-passthrough branch October 13, 2026 02:00
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.

[P0] cat 快速路径忽略引号重新展开参数:cat '$FIX/c' 静默读成 zz/c(单引号/转义 $ 均失守;echo/head/wc 不受影响)

1 participant