Skip to content

fix(shell): a function pops its frame when a command in it fails - #240

Open
hyperpolymath wants to merge 1 commit into
mainfrom
claude/peaceful-johnson-49bumq
Open

hyperpolymath wants to merge 1 commit into
mainfrom
claude/peaceful-johnson-49bumq

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

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 reached return. A command in the body that returned an error left through ? 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 too, so local and return were accepted outside any function.

This was noted in the review of #233 (the finding on main.rs about refusals inside nested execution) and left out of that pull request: the fault is on main and is older.

One point needs the owner's decision. The -c line 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 in run_function_body. execute_function_call pushes 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 its term feature, for the pseudo-terminal. The feature is on for test builds only. Cargo.lock does not change.

Reproduction, and what bash 5.2.21 prints

The -c line does not show the fault. On main, a built-in that fails ends a -c script, 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" standard output exit status
bash 5.2.21 outer 0
vsh -c on main nothing (Error: Parent directory does not exist (ENOENT) on standard error) 1
vsh -c on this branch nothing (the same error) 1

That is the fault #233 lists under "Not done here": "A failing built-in still ends a -c script". 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.

functions, then lines bash 5.2.21 vsh on main vsh on this branch
f() { 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 0
local and return are refused both are accepted, with no message both are refused

Both 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

  • Tests pass (just test or equivalent): cargo test --no-fail-fast in impl/rust-cli/, 832 passed, 0 failed, 15 ignored. just test itself was not run.
  • Code is formatted (just fmt or equivalent): cargo fmt --check is clean.
  • Linter is clean (no new warnings or errors): cargo clippy --all-targets -- -D warnings is clean, and so is the same with --all-features.
  • No banned language patterns (Rust only).
  • No unsafe blocks without // SAFETY: comments (no unsafe is added).
  • No banned functions.
  • SPDX license headers present on all new/modified source files (the new test file carries MPL-2.0).
  • No secrets, credentials, or .env files included.

Testing

All of this was run locally, on Linux x86_64, with rustc 1.97.0.

  • Each new test fails without the fix. The five tests were run on four builds: the source with and without the fix, each with the default features and with --no-default-features. With the fix: 5 pass on both builds. Without it (a checkout of main plus only the test files and the Cargo.toml line): 5 fail on both builds, each showing the leaked value, for example after: x is inner. and a call depth of 2 where 0 is expected.
  • Stability of the pseudo-terminal tests. The file was run 400 times, eight at a time on four processors: 1600 passes, no failure. Against the shell built from main, 400 runs gave 1600 failures.
  • The checks asked for, with OLDPWD and PWD set and again with both unset (env -u OLDPWD -u PWD): cargo fmt --check clean, cargo clippy --all-targets -- -D warnings clean, cargo test --no-fail-fast 832 passed, 0 failed, 15 ignored, both times.
  • As the CI matrix runs it: cargo test --all-features 839 passed, cargo test --no-default-features 832 passed, no failures.
  • Together with Truthful baseline (S0): a battery that runs the built shell, and what it drove #233. The commit was merged into the tip of Truthful baseline (S0): a battery that runs the built shell, and what it drove #233 (9714586) in a scratch tree, not pushed. It merges without conflict. There, cargo fmt --check and clippy are clean and cargo test --no-fail-fast gives 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 verified: anything on macOS; the stress suite; CI on this branch.

Not done here

  • The -c line 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.
  • A function with two commands cannot be typed on one line at the prompt. 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.
  • Commands piped into the default build are not run. echo 'echo hi' | vsh prints the banner and Error reading input: No such device or address (os error 6), and exits with status 0. Seen while reproducing; not touched.
  • No changelog entry.

Limits

  • The four tests that run the built shell are built on Linux only. The pseudo-terminal handling has been run nowhere else. The one library test runs everywhere.
  • The expected 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.
  • No proof is added or changed, and no proof covers function frames. The evidence for this change is tests only.

Generated by Claude Code

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
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: e819ec82-d023-4e14-a443-3bccc04be104
Commits

Reviewing files that changed from the base of the PR and between 63c14a0 and 0a03ecc.

Files selected for processing: 4

  • impl/rust-cli/Cargo.toml
  • impl/rust-cli/src/executable.rs
  • impl/rust-cli/tests/function_and_script_tests.rs
  • impl/rust-cli/tests/function_frame_tests.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

🔍 Hypatia Security Scan

Findings: 18 issues detected

Severity Count
🔴 Critical 0
🟠 High 2
🟡 Medium 16
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
hyperpolymath marked this pull request as ready for review November 17, 2026 17:55
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

No deployments
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.

3 participants