Repository navigation
fix(exec): keep a quoted empty word as an argument - #332
Conversation
vfs::absolute read "" as the cwd, so a command given an empty operand acted on the cwd: rm -rf "" walked it and, joining "" with each child name, removed top-level paths named like its children. An empty path is now ENOENT, as in POSIX, in the one helper both kernels use. cd "" stays a no-op, as in bash, mkdir -p "" fails, and an empty symlink target is refused, as symlink(2) does.
A double-quoted part now makes a field even when it comes out empty, so "" and "$unset" pass an empty argument, as in bash. "$@" with no arguments still makes no field. A quoted empty command name now fails with 127 instead of running the next word. Fixes strands-agents#299
parse_primary checked for two more words but read three, so [ a != ] panicked and ended the script. It now needs all three, so the test is false and the script goes on.
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changes requested: one blocker, and it only affects custom kernels. On the default VfsKernel this is correct and safe: the #299 repro matches bash, rm -rf "" leaves the cwd alone, and [ a != ] no longer panics. But a kernel passed to Shell::with_kernel now receives "" for "$unset", and on the repo's own MemKernel that makes rm -rf "$unset" delete the cwd and a root-level directory, where base fails safely.
| Item | Result |
|---|---|
🔴 rm -rf "$unset" on a custom Kernel |
Deletes files; base prints missing operand (inline, src/exec.rs) |
🟡 cd with an empty HOME or OLDPWD |
Fails; base and bash return 0 (inline, tested fix) |
| 🟡 Error text for an empty operand | mv: no such file or directory names no operand (inline, tested fix) |
| Tests | cargo test --workspace --all-targets passes on 3abe869: 2,683 passed, 0 failed |
What I verified
- Reviewed head
3abe869against base55d7816, debug builds on Linux aarch64, compared with bash 5.2.15. - The #299 repro prints
1 2 2 [x]on base and2 2 2 [][x]on head. cargo test --workspace --all-targetson head exits 0 with 2,683 passed. The differential suite counts as one test and covers 121 cases. I did not run the Python or Node suites,cargo fmtor clippy.- "All four fail on
main" holds: with head's differential cases on base, 4 cases fail, andtest_binary_op_missing_rightpanics atsrc/builtins/test.rs:97. - I re-ran each inline finding myself on head, base and bash. Both suggested fixes were applied to a copy of head, where the lib,
custom_kernel,differential,shell_integrationandvfs_unitsuites still pass. - From the adversarial pass, which I spot-checked but did not re-run in full:
- Expansion: about 22,700 word forms and 12,900 random multi-word cases on bash, base and head. No word where base matched bash and head does not.
- Empty paths on
VfsKernel: 254 commands with"", none changed the cwd. test: 8,000 random expressions. Head never panics; base panics on 333.
- One adversarial probe (
rm -rf ""/) was run under real bash in my sandbox by mistake. GNUrmrefused it, and I checked that the sandbox and both worktrees were intact. No result here depends on it.
Questions (1 blocking, 3 non-blocking)
- Blocking: should Shell refuse an empty path before it reaches the kernel, or is each kernel expected to? See the inline comment on
src/exec.rs. - The description says Box is the only other caller of
vfs::absolute. Boxmaindoes not call it yet: it has a private copy (crates/shell/src/resolve.rs:21) that maps""to the cwd, and only the open box#534 switches tovfs::absolute. So Box gets the compile error only if box#534 merges before its pin moves past this PR. Is that order certain? I read the Box source and did not run it. Shell::list_files("")and the MCPlist_dirtool with""go from listing the cwd to NotFound. Is that intended? If so it is worth a line in the description. The API pass ran both; I did not re-run them.- Could the description add
Fixes #322? #321's panic is fixed here, but it also asks for status 2, soRefs #321fits better. Does this PR supersede #320?
Appendix: non-blocking (3)
- Coverage:
empty_path.shdoes not exercisermdir,chmod,lsorreadlink. The adversarial pass reverted the guard inlist_dir,remove_file,remove_dir,set_permissions,rename(destination) andread_linkin a scratch copy: all four suites still passed, andrmdir ""removed the cwd. Addingrmdir "$e"andchmod 600 "$e"to the case would pin them. I did not re-run the mutant. - Pre-existing, filed #335:
cat,tee,mkdir,rmdir,chmod,cp,sed -iandwcstop at the first operand that fails. This PR makes it easier to hit: withLOGunset,echo data | tee "$LOG" | catprints nothing on head, where base and bash printdata. - Pre-existing, filed #334:
[ "" -lt 3 ]is true. With this PRunset n; [ "$n" -lt 3 ]goes from false to true; bash reports an error and is false.
The Kernel trait now states that an empty path names no file, and the test MemKernel resolves through vfs::absolute, so a custom kernel has the rule to follow and the repo's own example follows it. cd maps an empty directory to `.` after choosing it, so an empty HOME or OLDPWD stays put, as in bash. The empty-path error names its operand as '', and empty_path covers rmdir and chmod.
|
Thanks. All three inline points are addressed in ef2024c, with
Coverage:
|
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. All three findings are fixed on f8783fc, and I found nothing new in the delta or the merge of main.
| Previous finding | Status |
|---|---|
🔴 rm -rf "$unset" on a custom Kernel |
Fixed in ef2024c. My repro on MemKernel now exits 0 and removes nothing, as bash does |
🟡 cd with an empty HOME or OLDPWD |
Fixed. Both return 0 and stay put, as in bash |
| 🟡 Error text for an empty operand | Fixed. mv "$S" x prints mv: no such file or directory: '' |
| Tests | cargo test --workspace --all-targets passes on f8783fc: 2,698 passed, 0 failed. CI Gate is green |
The blocker is closed by making each kernel own the rule, which the Kernel trait now states. A third-party kernel that still maps "" to the cwd is not protected by Shell; I take that as the decided trade-off, not an open item.
What I verified
- Reviewed
3abe869..ef2024c(7 files) and the mergef8783fc. Debug builds on Linux aarch64, headf8783fcagainstmain@2a1495eand bash 5.2.15. - Blocker: the test from my earlier inline comment, appended to
tests/custom_kernel.rsin a copy off8783fc, passes.rmreturns 0, and the cwd and/data/db.txtare kept. cd:HOME=; cd,OLDPWD=; cd -,cd ""andset -e; HOME=; cdmatch bash.cd ""keepsPWDand setsOLDPWDas bash does.- Empty paths on
VfsKernel:rm -rf,rmdir,mvandchmodon"$unset"leave the cwd alone with bash's statuses. empty_path.shrun under bash reproduces the committed.expectedstdout exactly, including the newrmdir=1andchmod=1.- Merge of
main: 22[[ ]]forms from #325 with"","$unset","$1"and"$@"give the same status on head,mainand bash. cargo test --workspace --all-targetsonf8783fcexits 0. I did not run the Python or Node suites,cargo fmtor clippy locally; CI ran the Python and Node suites and they pass.
Description
A double-quoted word that expands to nothing is dropped, so a command sees shifted arguments.
f "" xpasses one argument, not two, and"$unset"disappears. A test such as[ "$p" != "" ]reachestestas[ /a != ], which nvm hits when it loads.In
expand_part_split, a double-quoted part now makes a field even when it comes out empty."","$unset"and"$1"with no arguments each pass one empty argument, as in bash."$@"with no arguments still makes no field, because it is the one quoted form that may expand to nothing.Keeping the word means commands now get
""as an operand, andVfsKernelread an empty path as the cwd.rm -rf ""would then act on the cwd, andtouch "",mv ""andchmod ""would change it.vfs::absolutenow refuses an empty path withNotFound, as POSIX does, and its error names the operand as''. EveryVfsKernelmethod gets that rule from the one helper. TheKerneltrait now states the rule, so a custom kernel knows to follow it, and the testMemKernelresolves throughvfs::absolutetoo. Three commands follow bash on top of it:cdstays put when the directory it chose is empty, whether from"",$HOMEor$OLDPWD;mkdir -p ""fails; andln -s "" lfails, assymlink(2)does.The same rule reaches the embedding API:
Shell::list_files("")and the MCPlist_dirtool with""now fail withNotFound, where they listed the cwd.vfs::absolutenow returnsio::Result<String>. Box will call it once strands-agents/box#534 merges, and it will handle the error when it next moves its pin.The last commit fixes a panic in
test.parse_primarychecked for two more words but read three, so[ a != ]ended the script. It is now false and the script goes on. bash exits 2 with a message there, where Shell exits 1. That gap applies to everytesterror, so it is left for its own change.The reproduction in #299 now prints what bash prints:
Related Issues
Fixes #299. Fixes #322. Refs #321: the panic is fixed, but
teststill exits 1 where bash exits 2.Documentation PR
None.
Type of Change
Bug fix
Testing
cargo test --lib: 86 passedcargo test --test vfs_unit: 98 passed.absolute_against_cwdnow also checks that""isNotFound.cargo test --test custom_kernel: 15 passedcargo test --test shell_integration: 2032 passed, including[ a != ], withmainmerged in.cargo test --test differential: passed. Twoknown-differencemarkers come out, inquote_empty_argsandcase_glob_and_fallthrough, and two cases are new:empty_command_nameandempty_path, which runsrm,touch,mkdir,mv,ln,rmdir,chmod,test -eandcdon"". All four fail onmain.cargo fmtis clean, and clippy reports nothing in the changed lines.I ran the relevant test suites for the bindings I touched (
cargo test --workspace --all-targets,pytest tests/python,npm test)If I touched Rust, I ran
cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.