Repository navigation
fix(shell): a function pops its frame when a command in it fails - #240
Open
hyperpolymath wants to merge 1 commit into
Open
hyperpolymath wants to merge 1 commit into
hyperpolymath wants to merge 1 commit into
Conversation
A call to a shell function pushes a frame: the caller's positional parameters, and the old value of each variable the body declares `local`. `execute_function_call` popped the frame after the body. A command in the body that returned an error left through `?` inside the body loop, before the pop. The caller then kept the function's `local` values and had the function's arguments as its own `$1`, `$2`, ... and `$#`. The call depth stayed raised as well, so `local` and `return` were accepted outside any function. A nested call left one frame behind for each function the error passed through. The body now runs in a function of its own, `run_function_body`. Its result, whichever it is, comes back to one place in `execute_function_call`, which pops the frame and then returns the result. The error still goes to the caller. The body loop and the pop are otherwise unchanged. This fault was noted in the review of #233 and left out of it. Where the fault shows. At the prompt: the interactive loop reports an error and reads the next line with the same state. It does not show with `-c` or a script file on `main`: there the first error ends the whole run, before a later command can show what the caller kept. That is a separate fault and is not touched here. So this line from the review f() { local x=inner; mkdir /no/such/parent/dir; }; x=outer; f; echo "$x" given to `-c` prints the error and exits 1 before and after this change, where bash 5.2.21 prints `outer`. With the function in a sourced file, the same commands at the prompt printed `inner` before this change. They print `outer` now, as bash does. Tests. Four in the new tests/function_frame_tests.rs run the built shell at its prompt through a pseudo-terminal: a `local` variable, the positional parameters, a nested call, and `local` and `return` at the prompt after a function failed. The `after:` lines they expect are what bash 5.2.21 prints for the same lines. They drive both prompts (the line editor of the default build and the plain prompt of --no-default-features) and are built on Linux only. One test in function_and_script_tests.rs calls the library and runs on every platform. All five fail on the code before this change, with both builds, and pass after it. For the pseudo-terminal, the `nix` crate (already a dependency, same version) is also listed under dev-dependencies with its `term` feature, so the feature is on for test builds only. Cargo.lock does not change. cargo fmt --check and cargo clippy --all-targets -- -D warnings are clean. cargo test --no-fail-fast: 832 passed, 0 failed, 15 ignored, with OLDPWD and PWD set and with both unset. Also run as the CI matrix does: --all-features (839 passed) and --no-default-features (832 passed), no failures. No proof is added or changed, and no proof covers function frames. The evidence for this change is tests only. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J4QvDjYffb3HcnwwDyrsF9
Contributor
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 8 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
|
🔍 Hypatia Security ScanFindings: 18 issues detected
View findings[
{
"line": null,
"reason": "workflow .github/workflows/hypatia-scan.yml runs harden-runner in `egress-policy: audit` — telemetry-only, never blocks; on a protected-branch trigger this is not containment",
"type": "RE002",
"file": ".github/workflows/hypatia-scan.yml",
"level": "epi",
"rule_module": "research_extensions",
"action": "report",
"severity": "high",
"evidence": [
{
"span": {
"file": ".github/workflows/hypatia-scan.yml",
"end_line": 0,
"start_line": 0
},
"rule": "research_extensions/RE002",
"observations": 1,
"rule_version": "0.1.0"
}
],
"support": {
"kind": "pattern"
}
},
{
"line": 2293,
"reason": "expect() in hot path (3 occurrences, CWE-754, line 2293, 2322, 2930)",
"type": "expect_in_hot_path",
"file": "/home/runner/work/valence-shell/valence-shell/impl/rust-cli/src/parser.rs",
"level": "epi",
"rule_module": "code_safety",
"action": "flag",
"severity": "medium",
"evidence": [
{
"span": {
"file": "/home/runner/work/valence-shell/valence-shell/impl/rust-cli/src/parser.rs",
"end_line": 2293,
"start_line": 2293
},
"rule": "code_safety/expect_in_hot_path",
"observations": 1,
"rule_version": "0.1.0"
}
],
"support": {
"kind": "pattern"
}
},
{
"reason": "Code scanning (Hypatia): hypatia/structural_drift/SD022 -- Hypatia structural_drift: SD022 -- 0 day(s) old",
"type": "CSA001",
"file": "docs/audits/2026-11-16-hypatia-remediation.adoc",
"level": "epi",
"rule_module": "code_scanning_alerts",
"action": "review",
"severity": "medium",
"evidence": [
{
"span": {
"file": "docs/audits/2026-11-16-hypatia-remediation.adoc",
"end_line": 0,
"start_line": 0
},
"rule": "code_scanning_alerts/CSA001",
"observations": 1,
"rule_version": "0.1.0"
}
],
"support": {
"kind": "pattern"
}
},
{
"reason": "Code scanning (Hypatia): hypatia/code_safety/expect_in_hot_path -- Hypatia code_safety: expect_in_hot_path -- 0 day(s) old",
"type": "CSA001",
"file": "impl/rust-cli/src/parser.rs",
"level": "epi",
"rule_module": "code_scanning_alerts",
"action": "review",
"severity": "medium",
"evidence": [
{
"span": {
"file": "impl/rust-cli/src/parser.rs",
"end_line": 0,
"start_line": 0
},
"rule": "code_scanning_alerts/CSA001",
"observations": 1,
"rule_version": "0.1.0"
}
],
"support": {
"kind": "pattern"
}
},
{
"reason": "Code scanning (Hypatia): hypatia/research_extensions/RE002 -- Hypatia research_extensions: RE002 -- 0 day(s) old",
"type": "CSA001",
"file": ".github/workflows/hypatia-scan.yml",
"level": "epi",
"rule_module": "code_scanning_alerts",
"action": "update",
"severity": "high",
"evidence": [
{
"span": {
"file": ".github/workflows/hypatia-scan.yml",
"end_line": 0,
"start_line": 0
},
"rule": "code_scanning_alerts/CSA001",
"observations": 1,
"rule_version": "0.1.0"
}
],
"support": {
"kind": "pattern"
}
},
{
"reason": "Descriptile in retired location (layout drift, not a security issue); the current canon keeps descriptiles as clauses of the repo deed, converted by kyX (standards D312, D313)",
"type": "SD004",
"file": ".machine_readable/6a2/STATE.a2ml",
"level": "epi",
"target": ".machine_readable/descriptiles/STATE.a2ml",
"advisory": true,
"rule_module": "structural_drift",
"action": "move",
"severity": "medium",
"evidence": [
{
"span": {
"file": ".machine_readable/6a2/STATE.a2ml",
"end_line": 0,
"start_line": 0
},
"rule": "structural_drift/SD004",
"observations": 1,
"rule_version": "0.1.0"
}
],
"support": {
"kind": "pattern"
}
},
{
"reason": "Descriptile in retired location (layout drift, not a security issue); the current canon keeps descriptiles as clauses of the repo deed, converted by kyX (standards D312, D313)",
"type": "SD004",
"file": ".machine_readable/6a2/META.a2ml",
"level": "epi",
"target": ".machine_readable/descriptiles/META.a2ml",
"advisory": true,
"rule_module": "structural_drift",
"action": "move",
"severity": "medium",
"evidence": [
{
"span": {
"file": ".machine_readable/6a2/META.a2ml",
"end_line": 0,
"start_line": 0
},
"rule": "structural_drift/SD004",
"observations": 1,
"rule_version": "0.1.0"
}
],
"support": {
"kind": "pattern"
}
},
{
"reason": "Descriptile in retired location (layout drift, not a security issue); the current canon keeps descriptiles as clauses of the repo deed, converted by kyX (standards D312, D313)",
"type": "SD004",
"file": ".machine_readable/6a2/ECOSYSTEM.a2ml",
"level": "epi",
"target": ".machine_readable/descriptiles/ECOSYSTEM.a2ml",
"advisory": true,
"rule_module": "structural_drift",
"action": "move",
"severity": "medium",
"evidence": [
{
"span": {
"file": ".machine_readable/6a2/ECOSYSTEM.a2ml",
"end_line": 0,
"start_line": 0
},
"rule": "structural_drift/SD004",
"observations": 1,
"rule_version": "0.1.0"
}
],
"support": {
"kind": "pattern"
}
},
{
"reason": "Descriptile in retired location (layout drift, not a security issue); the current canon keeps descriptiles as clauses of the repo deed, converted by kyX (standards D312, D313)",
"type": "SD004",
"file": ".machine_readable/6a2/AGENTIC.a2ml",
"level": "epi",
"target": ".machine_readable/descriptiles/AGENTIC.a2ml",
"advisory": true,
"rule_module": "structural_drift",
"action": "move",
"severity": "medium",
"evidence": [
{
"span": {
"file": ".machine_readable/6a2/AGENTIC.a2ml",
"end_line": 0,
"start_line": 0
},
"rule": "structural_drift/SD004",
"observations": 1,
"rule_version": "0.1.0"
}
],
"support": {
"kind": "pattern"
}
},
{
"reason": "Descriptile in retired location (layout drift, not a security issue); the current canon keeps descriptiles as clauses of the repo deed, converted by kyX (standards D312, D313)",
"type": "SD004",
"file": ".machine_readable/6a2/NEUROSYM.a2ml",
"level": "epi",
"target": ".machine_readable/descriptiles/NEUROSYM.a2ml",
"advisory": true,
"rule_module": "structural_drift",
"action": "move",
"severity": "medium",
"evidence": [
{
"span": {
"file": ".machine_readable/6a2/NEUROSYM.a2ml",
"end_line": 0,
"start_line": 0
},
"rule": "structural_drift/SD004",
"observations": 1,
"rule_version": "0.1.0"
}
],
"support": {
"kind": "pattern"
}
}
]Powered by Hypatia Neurosymbolic CI/CD Intelligence |
hyperpolymath
marked this pull request as ready for review
November 17, 2026 17:55
8 tasks done
hyperpolymath
added a commit
that referenced
this pull request
Nov 18, 2026
## Summary Stacked on #233. The base is #233's branch, so the diff also shows the commit of #240 (`0a03ecc`), which is merged in here. Once #233 and #240 are merged, retarget this to `main`; the only new commit is `86ab3fd`. A built-in that failed was an error that unwound through every command around it, to the script or the prompt: - `mkdir x || echo no` never ran `echo no`. - A loop stopped at its first failure. - A function stopped, and its caller stopped with it. - A `-c` script or a script file ended at the first built-in that failed. That last behaviour stood in for `set -e`, which was not implemented. `set -e` only stored a variable, so `set -e; false; echo after` printed `after`. Now every command is a boundary. A command that fails prints `vsh: ` and the reason, and gives status 1. Whatever ran it goes on. `set -e` is honoured. A simple command that fails ends the shell with its status, and the EXIT trap still runs. As in bash, `set -e` does not apply in these places: - the condition of `if`, `elif`, `while` and `until`; - a command of an `&&` or `||` list before its last command; - a function called from either of those. `! cmd` is still not implemented. The owner chose this route after a measured prototype, on the condition that it lands together with a real `set -e`. Without it, scripts would lose the accidental stop at the first failure and become less safe. ## Changes - `impl/rust-cli/src/executable.rs`: - `ExecutableCommand::execute` is the boundary. It turns a failure into status 1 and applies `set -e`. The old body is now `Command::run`. - `as_condition` opens and closes the contexts in which `set -e` does not apply. - Three statuses change to what bash gives: - `return` outside a function: 2. - `eval` of a line that cannot be read: 2. - an `if` with no branch taken: 0, as POSIX requires. With `set -e`, the old 1 would end the shell. - `impl/rust-cli/src/state.rs`: two new fields, `errexit` and `errexit_suppressed`. - `impl/rust-cli/tests/command_boundary_tests.rs` (new): 28 lines run on the built shell, in four groups. Each expected result was recorded from bash 5.2.21. - Changed tests, each because the behaviour changed: - `function_and_script_tests.rs`: five checks of a status instead of an error from `execute`. - `function_frame_tests.rs`: the failure message reads `vsh: …`, not `Error: …`. - `builtin_strictness_tests.rs`: an EXIT trap statement that runs and fails is reported as `vsh: …`. - The battery list: `builtin-05` and `rm-02` now pass and come off `known_failing.txt`. The count in the test goes from 61 to 59. - `CLAUDE.md`, `README.adoc`, `ROADMAP.adoc`: - the battery count is now 69; - `set -e` is off the "does not work" list; - the line saying a failed built-in ends a `-c` script is gone. ## Measured, with bash 5.2.21 as reference | | before (#233 + #240) | this branch | |---|---|---| | 56 failure and `set -e` lines | 13 match bash | 53 match bash | | conformance battery, 128 lines | 67 match | 69 match; no case gets worse | The three lines that still differ are older faults this does not touch: - a redirection from a missing file gives 127; - `shift` is not a built-in; - the built-in `ls` gives 1 for a missing file where `ls` gives 2. dash agrees with bash on every case except the status cases noted in the test file: `return` and `local` outside a function, and `eval 'if'`, which end dash. ## RSR Quality Checklist ### Required - [x] Tests pass (`just test` or equivalent): `cargo test --no-fail-fast` in `impl/rust-cli/`: 889 passed, 0 failed, 15 ignored. That holds with `OLDPWD` and `PWD` set, with both unset, and with `--no-default-features`. `just test` itself was not run. - [x] Code is formatted (`just fmt` or equivalent): `cargo fmt --check`. - [x] Linter is clean (no new warnings or errors): `cargo clippy --all-targets -- -D warnings`, and the same with `--all-features`. - [x] No banned language patterns. - [x] No `unsafe` blocks without `// SAFETY:` comments (none added). - [x] No banned functions. - [x] SPDX license headers present on all new/modified source files. - [x] No secrets, credentials, or `.env` files included. ## Testing - **The four new tests fail without the change.** Against the code before this commit, the same test file fails all four. 21 of its 28 lines differ from bash there. - **The prompt tests**, which drive the shell through a pseudo-terminal, were run 400 times against each of the two prompts (the line editor and the plain prompt), with no failure. - **Not verified:** macOS; the stress suite; CI on this branch. ## Limits - `execute` keeps its `Result` type but no longer returns `Err` for a command that fails. A program using the library sees a status instead, and the message goes to standard error. - The 100,000-iteration safety limit on `while` and `until` now fails only the loop, with status 1, where before it ended the script. - No proof covers this. The evidence is tests only. --- _Generated by [Claude Code](https://claude.ai/code/session_01J4QvDjYffb3HcnwwDyrsF9)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
This branch has not been deployed
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.



Summary
A call to a shell function pushes a frame: the caller's positional parameters, and the old value of each variable the body declares
local. The frame was popped when the body ran to its end or reachedreturn. A command in the body that returned an error left through?before the pop. The caller then kept the function'slocalvalues and had the function's arguments as its own$1,$2, … and$#. The call depth stayed raised too, solocalandreturnwere accepted outside any function.This was noted in the review of #233 (the finding on
main.rsabout refusals inside nested execution) and left out of that pull request: the fault is onmainand is older.One point needs the owner's decision. The
-cline that was given as the reproduction does not print what bash prints, before or after this change, and this change cannot make it. The reason is a different fault. See "Reproduction".Changes
impl/rust-cli/src/executable.rs: the body of a function now runs inrun_function_body.execute_function_callpushes the frame, runs the body, pops the frame, and then returns the result, whichever it is. There is no?between the push and the pop. The body loop and the pop are moved, not rewritten. The error still goes to the caller.impl/rust-cli/tests/function_frame_tests.rs(new): four tests that run the built shell at its prompt, through a pseudo-terminal.impl/rust-cli/tests/function_and_script_tests.rs: one test that calls the library. It runs on the platforms where the four above are not built.impl/rust-cli/Cargo.toml:nix(already a dependency, same version) is also listed under dev-dependencies with itstermfeature, for the pseudo-terminal. The feature is on for test builds only.Cargo.lockdoes not change.Reproduction, and what bash 5.2.21 prints
The
-cline does not show the fault. Onmain, a built-in that fails ends a-cscript, or a script file, at once. Nothing after the failing call runs, so no command is left to show what the caller kept.f() { local x=inner; mkdir /no/such/parent/dir; }; x=outer; f; echo "$x"outervsh -conmainError: Parent directory does not exist (ENOENT)on standard error)vsh -con this branchThat is the fault #233 lists under "Not done here": "A failing built-in still ends a
-cscript". It hides this one in a script. It is not touched here.At the prompt the fault shows. The interactive loop reports an error and reads the next line with the same state. In the table, the functions are in a file that the first line sources. The lines were typed at the prompt for vsh and given on standard input for bash.
mainf() { local x=inner; mkdir /no/such/parent/dir; }x=outer,f,echo "after: x is $x."after: x is outer.after: x is inner.after: x is outer.f() { mkdir /no/such/parent/dir; }set -- outer1 outer2,f inner1,echo "after: first is $1, count is $#."after: first is outer1, count is 2.after: first is inner1, count is 1.after: first is outer1, count is 2.inner() { local x=in; mkdir /no/such/parent/dir; }outer() { local x=mid; inner deep; }x=top,set -- top1,outer mid1 mid2,echo "after: x is $x, first is $1, count is $#."after: x is top, first is top1, count is 1.after: x is in, first is deep, count is 1.after: x is top, first is top1, count is 1.f() { mkdir /no/such/parent/dir; }f,local y=1,return 0localandreturnare refusedBoth prompts were checked: the line editor of the default build, and the plain prompt of a build with
--no-default-features.RSR Quality Checklist
Required
just testor equivalent):cargo test --no-fail-fastinimpl/rust-cli/, 832 passed, 0 failed, 15 ignored.just testitself was not run.just fmtor equivalent):cargo fmt --checkis clean.cargo clippy --all-targets -- -D warningsis clean, and so is the same with--all-features.unsafeblocks without// SAFETY:comments (nounsafeis added).MPL-2.0)..envfiles included.Testing
All of this was run locally, on Linux x86_64, with rustc 1.97.0.
--no-default-features. With the fix: 5 pass on both builds. Without it (a checkout ofmainplus only the test files and theCargo.tomlline): 5 fail on both builds, each showing the leaked value, for exampleafter: x is inner.and a call depth of 2 where 0 is expected.main, 400 runs gave 1600 failures.OLDPWDandPWDset and again with both unset (env -u OLDPWD -u PWD):cargo fmt --checkclean,cargo clippy --all-targets -- -D warningsclean,cargo test --no-fail-fast832 passed, 0 failed, 15 ignored, both times.cargo test --all-features839 passed,cargo test --no-default-features832 passed, no failures.9714586) in a scratch tree, not pushed. It merges without conflict. There,cargo fmt --checkand clippy are clean andcargo test --no-fail-fastgives 873 passed, 0 failed, 15 ignored, with the default features and with--no-default-features. The conformance battery of Truthful baseline (S0): a battery that runs the built shell, and what it drove #233 is unchanged by it.Not done here
-cline above. It prints what bash prints only once a failing built-in stops ending a script. That is a change in what scripts do after a failure, so it is the owner's call whether it belongs here, in Truthful baseline (S0): a battery that runs the built shell, and what it drove #233's follow-up, or in a change of its own.g() { echo one; echo two; }is cut at the;inside the braces, in both prompts. The tests therefore source their functions from a file. Seen while writing the tests; not touched.echo 'echo hi' | vshprints the banner andError reading input: No such device or address (os error 6), and exits with status 0. Seen while reproducing; not touched.Limits
after:lines are from bash 5.2.21. For the fourth test bash's wording differs from the shell's own, so the test checks only that both commands are refused.Generated by Claude Code