Repository navigation
fix: ignore set -e inside a body run left of && or || - #355
ferdingler wants to merge 7 commits into
Conversation
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changes requested. The fix is right for the direct execution path, but it makes a child sh ignore its own set -e when it runs left of && or ||.
| Finding | Where | |
|---|---|---|
| 🔴 | sh build.sh || handler: a set -e child script runs past its failure and exits 0 (regression) |
inline, src/exec.rs:2371 |
| 🟡 | #259 still reproduces when the list's output is captured: $(f || h), { f || h; } > file, … done | cmd |
inline, src/exec.rs:2361 |
Both have a small fix that I checked in a scratch build. Everything else I threw at the counter held up.
What I verified, and non-blocking notes (5)
Verified (head 8aac37a, base dbb6189, debug builds, bash 5.2.15 as the reference)
cargo test --workspace --all-targetson head: 2733 passed, 0 failed.- The seven new
.expectedfiles match what bash prints. Three cases fail on base and pass on head (ignored_in_and_body,ignored_in_or_body,ignored_nested_tested_call); the other four pass on both and act as guards. - About 450 hand-written and 1,860 generated scripts compared across bash, base and head. Apart from the two findings and note 4, every difference on head is also on base.
- The counter is restored after
return,exit,break/continue, command-not-found, nounset errors, a failedevalorsource, 2,000 loop iterations, 40-deep groups, and aShell::runor MCP call that times out mid-body. No panics. - Function, group, subshell,
evaland.bodies match bash left of||, left of&&and mid-chain. The last element, the right-hand side and the next statement still exit. - Not run: Python and Node bindings, release builds,
cargo fmt/clippy(CI is green on those).
Reading order: src/exec.rs:2359-2374, then the child-shell setup at src/exec.rs:4221-4227, then the second loop at src/exec.rs:1954-1967.
Non-blocking
- Stale comments:
src/os.rs:224andsrc/shell.rs:420,:455still describe the counter as counting!only. - Test gap: no case has control flow leave a tested body. A build that skips the decrement on the error path passes the whole suite. This case catches it (bash: no output, status 1):
set -e for i in 1 2; do { false; break; } || echo x; done false echo unreachable
- Test gap: #259 names
./sourcebodies. They work on head (. ./lib.sh || echo caught) but no case covers them. - Contrived edge, new on head: an explicit
set -einside a$( )used as aforlist, inside a tested function, is now ignored.set -e; g() { false; echo a; }; f() { for x in $(set -e; g); do echo "got $x"; done; echo f-end; }; f || echo cprintsf-endon bash and base,got athenf-endon head. - Same on base, so not part of this review and not filed:
set -e; { false; echo x; } > f.txtkeeps going (bash exits 1): the capturing path never applies errexit to a compound.set -e; for i in 1 2; do if break; then :; fi; done; false; echo unreachableprintsunreachable:execute_ifreturns on?atsrc/exec.rs:2685before restoringopt_errexit.if fwherefrunsset -eexits, because conditions zeroopt_errexitinstead of using the counter.x=$(f)applies errexit insidef(bash turns it off in command substitutions).
|
Addressed the review in c63d8c8 (tests first in 83257f9). Non-blocking notes: 1) updated the stale counter comments in os.rs and shell.rs; 2) added @strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. Both findings from my last review are fixed at c63d8c8, and I couldn't break either fix.
| Previous finding | Status | |
|---|---|---|
| 🔴 | Child sh left of &&/|| ignored its own set -e |
Fixed |
| 🟡 | #259 still reproduced when the list's output was captured | Fixed, including the two early returns |
Notes 1 to 3 are done. Deferring 4 ($(set -e; g) inside a tested function) and 5 (the bugs that are also on base) is fine by me; both still behave as described.
CI: the Rust macOS and Windows jobs were still running when I posted this; every finished job passed.
What I verified
Head c63d8c8, previous head 8aac37a, base dbb6189, debug builds, bash 5.2.15 as the reference.
cargo test --workspace --all-targets: 2733 passed, 0 failed.- The four new
.expectedfiles match what bash prints.child_sh_in_tested_contextandignored_in_captured_bodyfail on8aac37aand pass now.break_out_of_tested_bodyandignored_in_sourced_bodypass on both. - 54 targeted scripts compared across bash, base and head, 32 of them written to attack the two fixes. 47 match bash. The other 7 are the
$(set -e; g)edge, four bugs that are also on base, and two shapes that hit the captured-compound bug below. - Re-ran 176 of the earlier adversarial scripts against this head: no crashes, and the only two that matched bash on base but not here are both the
$(set -e; g)edge. - Counter leak: in one MCP session with
set -e, I timed out five captured or tested shapes ({ { sleep 3; } | cat && echo x; } > out.txt,x=$(sleep 3 && echo x), a pipedforloop, a redirected group calling a function,sh -c 'sleep 3' || …). After each one,false; echo LEAKexited 1 with no output. A build without the early-return decrements printsLEAKon the first shape. - Not run: Python and Node bindings, release builds,
cargo fmt/clippy(not installed in my sandbox).
One behaviour change to be aware of, from the bug you chose to leave: set -e; { f || true; false; echo no; } > out.txt used to exit 1 like bash, but only because f killed the script. Now f || true is handled correctly and the group carries on past false, since the capturing path never applies errexit to a compound. Same root cause as set -e; { false; echo x; } > f.txt.
…d-context # Conflicts: # src/exec.rs
|
@strandly-the-agent Please review this PR Merged origin/main (82da43e) to resolve the conflict with #356 in |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. The merge with #356 is resolved correctly, and the new case guards the reset. The one red check, Rust (windows-latest), fails on main too: it is #406, with a fix proposed in #411.
- Merge resolution:
run_pipelinekeeps #356's child-shell option resets and addserrexit_ignored = 0next to them (src/exec.rs:4309-4313). The rest of the PR diff against main is unchanged fromc63d8c8. set_e_child_sh_in_tested_function: its.expectedmatches bash 5.2.15. With the reset line deleted, it fails together withset_e_child_sh_in_tested_contextandsh_c_negated_child_keeps_its_errexit.- Windows CI:
compound_pipe_into_function_redirected_from_clioverflows the 1 MiB main-thread stack. The same test fails on Windows for #409 (a dependabot bump), #301 and #353, so it does not come from this PR.
What I verified
Head 6904c2b, main 819f5ee, debug builds, bash 5.2.15 as the reference.
cargo test --workspace --all-targets: 2779 passed, 0 failed.cargo fmt --checkis clean.- All 15
set_e_*differential cases match bash when replayed through the CLI. - 17 targeted scripts match bash: child
sh/lashunder||,&&,!,xargsand functions; inheritedset -u/pipefailnot reaching the child; the #259 shapes in direct, captured, redirected and piped contexts;breakout of a tested body;source. - Stack: on Linux, the failing script needs a 1012 KB main-thread stack on both main and this head (binary search with
ulimit -s), so this PR doesn't add to it. I could not test on Windows. - Not run: Python and Node bindings.
Description
With
set -eon, a failure inside a function, brace group, subshell orevalkilled the whole script even when that body was on the left of||or&&. Sof || echo caughtnever got to run the handler, and the script just exited 1. Bash ignoresset -efor everything run in that position, so the body keeps going and the script carries on.Now the left side of
&&and||ignoresset -eall the way through whatever it runs, the same way!already did. The last command of an&&/||chain and plain untested calls still exit early, as in bash. Small change in the executor.Related Issues
Fixes #259
Documentation PR
N/A
Type of Change
Bug fix
Testing
I added differential cases for function, group, subshell and
evalbodies under||,&&,if/whileand!, a nested function call, and two cases that must still exit early. On main the||,&&and nested cases failed (theifand!ones already matched bash). With the change they all match bash, and the full test suite passes.cargo test --workspace --all-targets,pytest tests/python,npm test)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.