Skip to content

[P0] fix(#555): mkdir/touch/cp/rm/rmdir re-expand already-expanded argv - silent wrong-path corruption - #558

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/555-file-builtin-family
Oct 12, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/555-file-builtin-family

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Closes #555. Same P0 silent-corruption class as #511 (fixed by #552): the five sibling wrappers in src/executor/external_file_builtins.rs — external_mkdir, external_touch, external_cp, external_rm, external_rmdir — re-ran expand_word over argv the simple-command executor had already expanded once. A single-quoted/escaped operand like $FIX/d arrives with its quotes removed and a live-looking dollar, so the second pass re-scanned the literal text and silently created/touched/copied-into/REMOVED the wrong path. Worst member: rm '$FIX/r' deleted an existing zz/r with exit 0.

Fix (argv as-is, the #552/#177 rule)

GNU anchors (vendored bash-5.3-16-gb4608166, behavior-level)

  • Simple-command words expand exactly once at dispatch: execute_cmd.c:4617 (execute_simple_command, single expand_words pass) -> execute_cmd.c:4938 (execute_disk_command receives the expanded words) -> execute_cmd.c:6126-6133 (shell_execve/execve on that final argv). 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: the shell contracts with them on final argv, and their diagnostics quote the operand verbatim (verified behaviorally: WSL GNU bash 5.3.0 script-file probes, 2026-10-12 — every quoted form fails naming the literal operand with status 1 and the guard fixture survives; unquoted $VAR still expands).

Tests

tests/issue555_file_builtin_family_regressions.rs — 22 cases mirroring the issue511 suite:

  • shared family helper (Case table + assert_literal_operand_contract): guard-path polarity, literal-path application, literal text in diagnostics, exit status — one row per wrapper so the next wrapper added to this file cannot quietly reintroduce the second expansion;
  • per builtin: quoted $VAR, escaped $VAR, unset-variable literal, plus rm/touch failing-operand loop continuation;
  • live-expansion controls (unquoted $VAR for all five), command-substitution dispatch, pathname-at-dispatch control.

Evidence (real rubash.exe, probes + cargo):

  • RED on unfixed origin/master (f33c5e3): 19/22 fail; the 3 passes are the two control rows and the rmdir-unset row (no discriminating power on the bug).
  • GREEN on this branch: 22/22 pass.
  • Full cargo test --no-fail-fast on both (172 targets each): failure sets bidirectionally identical by name except the 19 issue555 names — 67 pre-existing failures shared byte-for-byte (including the redir golden reds owned by other waves), zero fix-only failures.

Notes / follow-up

  • external_mkfifo in the same file still has a second expand_word, but its operand only feeds an error message (mkfifo always fails on Windows) — no filesystem action, no corruption path. Left out of this issue's scope; worth a row in this table if it ever gains a create path.
  • rubash's own wrapper leniencies (mkdir's create_dir_all implies -p; rmdir swallowing per-operand errors) are pre-existing contracts, unchanged here; the suite pins the re-expansion invariant within them.

…gv - silent wrong-path corruption

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 merged commit 5d4c250 into master Oct 12, 2026
7 of 8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/555-file-builtin-family 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-class] mkdir/touch/cp/rm/rmdir re-expand already-expanded argv (siblings of #511)

1 participant