Skip to content

flags(bash): \local\ declared in a subshell narrows the parent shell's later writes #700

Done

Description

@jflowers

In f() { (local X=1); X=2; }; f; : "$X", the local runs in a child shell and ends with it, so X=2 writes the global X and the final read is satisfied. The scanner folds isLocal per (function, name) with a sticky OR, so the child-shell declaration narrows X=2 to the function's end. The final read then shows up as a false caller requirement.

Fix direction: record the declaration's execution scope (its child-shell range) as well as its function. A local should narrow only writes in the same shell environment. Ordinary local X; X=2 must stay function-scoped.

Repro (not yet run on a main build): the one-liner above in a .sh file, run with ripwire <dir> --flags. X should not be reported as a requirement.

Raised by CodeRabbit on #685 (thread) and deferred when #685 landed through #688.

Activity

  1. joyful-ii-V-I commented on Nov 22, 2026

    @joyful-ii-V-I
    Collaborator

    Fixed in #720 (open; it closes this issue when it merges). Reported by jflowers.

    The per-(function, name) fold no longer applies a sticky OR. A local now binds the name only from where it takes effect to where its own shell ends: the function's }, or sooner when it ran in a child shell. The scan already records that end on the write: the subshell's ), or the command end for a pipeline component or a background job. So f() { (local X=1); X=2; }; f; : "$X" no longer reports X. Neither do the $( local X ) and local X | cat forms. A plain local X; X=2 is still function-scoped.

    Gate: test/bashenvcheck.sh arm 14 (test/bashenvfix/scope.sh):

    • SC_SUB, SC_CMD and SC_PIPE are red on main e267e24 and green on the lane.
    • Negatives: SC_PLAIN, SC_TWICE (a real local after a child-shell one), and SC_INNER / SC_NOLOC (writes inside a subshell) stay requirements. SC_PLAIN and SC_TWICE go red under a guard-off build.
  2. added
    help wanted
    on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions