Repository navigation
[P0] fix(#511): cat inline path re-expands already-expanded argv - silent wrong-file read - #552
Merged
Merged
Conversation
…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
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #511. P0: silent wrong-file read = silent data corruption class.
Repro (from the issue)
GNU:
cat: $FIX/c: No such file or directory, status 1. rubash (before this PR): silently printedhi— it readzz/c. Escaped\$FIX/cwas equally broken;echo/head/wcand 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 throughexpand_wordagain. 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'sexternal_sedfixed 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 inexpand_command_wordsbefore dispatch, so the second pass never carried semantic weight. Untouched: option parsing,/dev/fd/Nand<(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.exebuilds (see hygiene note below):896ef0d4(chore: init — the initial import)a313729b)41de8319(#500, the range's good anchor)a800dc5b(#501)origin/master(1245798b)Every commit of
41de8319..origin/masterreproduces, 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 theexternal_catrefactor (it was inherited by 1bcae28/#415 from the earliercat_file_operandsloop).Bisect/test hygiene note (why the suspicion was plausible)
target/debug/bash.exein 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 spawnsbash.exe(instead ofCARGO_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 userubash.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)
origin/master: same 10 targets — 69 named failures#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 freshorigin/mastervs 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 theOnceLockcaching.cargo fmt --checkclean.Same-class follow-ups (deliberately NOT in this P0)
The sibling wrappers in the same file re-expand already-expanded argv with
expand_wordexactly like cat did:external_mkdir,external_touch,external_cp,external_rm,external_rmdir(e.g.mkdir '$FIX/d'createszz/d). Same silent-corruption class; they deserve their own issue + tests rather than widening this P0's blast radius.