Skip to content

fix(interpreter): pass every positional parameter to for loops, source and scripts - #729

Open
abhiramshaji-cyber wants to merge 10 commits into
vercel-labs:mainfrom
abhiramshaji-cyber:fix/for-positional-params
Open

abhiramshaji-cyber wants to merge 10 commits into
vercel-labs:mainfrom
abhiramshaji-cyber:fix/for-positional-params

Conversation

@abhiramshaji-cyber

@abhiramshaji-cyber abhiramshaji-cyber commented Oct 30, 2026 •

Copy link
Copy Markdown

Loops over a script's arguments were splitting and dropping them, and sourced scripts lost every argument after the ninth, so this PR keeps every argument as given.

What this code is for

executeFor in control-flow.ts runs for loops, including for a; do and for a do, which loop over the positional parameters. The source builtin, executeUserScript (a script run by path) and callFunction set $1, $2 and onward for the code they run, then put the caller's parameters back afterwards.

Before this PR

Someone runs f () { for a; do echo "[$a]"; done; }; f "a b" "" c.

  1. callFunction sets $1 to a b, $2 to empty and $3 to c.
  2. executeFor sees no in list and reads $@ as one string: a b c.
  3. It runs .split(" ").filter(Boolean) on it.
  4. The loop prints [a], [b], [c]: three wrong items instead of [a b], [], [c].
  5. Separately, source s.sh a b c d e f g h i j k sets $# to 11, but source and executeUserScript stop at $9, so ${10} and ${11} are empty and "$@" prints ()() for them (Scripts run by path and source only get $1..$9; ${10}+ and "$@" beyond 9 are empty #456).

After this PR

Same person, same command.

  1. Same as above through step 1.
  2. executeFor checks $# against maxArrayElements, the same way the in list path does.
  3. It loops over $1 through $# one at a time, the same way "$@" expansion reads them.
  4. The loop prints [a b], [], [c].
  5. source and scripts run by path set every argument, so ${10} is j and ${11} is k.

The diagram

flowchart LR

classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827
classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px
classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px

subgraph BEFORE["BEFORE: 3 arguments print [a] [b] [c]"]
direction TB
  B1["f 'a b' '' c runs for a; do"] --> B2["executeFor"]
  B2 --> B3["reads $@ as one string"]
  B3 --> B4{"split on spaces, drop empty"}
  B4 -->|"spaces and empties lost"| B5["prints [a] [b] [c]"]
  B4 -->|"source s.sh with 11 args"| B6["${10} and ${11} empty"]
end

subgraph AFTER["AFTER: 3 arguments print [a b] [] [c]"]
direction TB
  A1["f 'a b' '' c runs for a; do"] --> A2["executeFor"]
  A2 --> A3["checks $# against maxArrayElements"]
  A3 --> A4{"THE CHANGE: loop $1 through $#"}
  A4 -->|"each argument kept whole"| A5["prints [a b] [] [c]"]
  A4 -->|"source s.sh with 11 args"| A6["${10} is j, ${11} is k"]
end

B1 ~~~ A1

class B1,B2,B3 box
class A1,A2,A3,A6 box
class B4,B5,B6 bad
class A4,A5 good

linkStyle 3 stroke:#cc0000,stroke-width:5px
linkStyle 8 stroke:#008800,stroke-width:5px

style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827
style AFTER fill:#f3fff3,stroke:#008800,color:#111827
Loading

The change

  • control-flow.ts: the fix for for x; do (no "in") splits the positional parameters on spaces and drops empty ones #485. executeFor loops over $1 through $# instead of splitting $@, after checking $# against maxArrayElements.
  • builtins/source.ts, subshell-group.ts, functions.ts and the new helpers/positional.ts: the fix for Scripts run by path and source only get $1..$9; ${10}+ and "$@" beyond 9 are empty #456, needed so for a; do works in sourced scripts with more than nine arguments. replacePositionalParams saves and clears every numbered parameter that exists, found by name, so a seeded $# that is huge, not a number or wrong can't drive the work, and a gap such as 11=outer with no 10 is still hidden. restorePositionalParams clears the callee's parameters before putting the caller's back, so set -- inside a function no longer leaks extra ones. set -- in builtins/set.ts uses the same clearing.
  • builtins/source.ts also follows bash when a file sourced with arguments runs set outside a function: those new parameters stay. A positionalSetBySet flag is turned on by set, cleared by each source and each function return, and saved and restored by subshells, pipeline stages, $(...) and <(...), so set -- in any of those still lets source restore the caller's parameters.
  • One existing test seeded @ directly as an environment variable, which only the old split read, so it now uses set --.
  • Tests: control-flow.for-positional.test.ts, positional-over-nine.test.ts and builtins/source.set-positional.test.ts. Expected outputs were checked against bash 5.3. Typecheck, lint, knip and the full test suite pass locally.

What changes for a user

  • for a; do and for a do keep spaces, tabs, newlines, glob characters and empty arguments, in functions, sourced files and scripts.
  • source file a … k and ./script a … k see ${10} and later, and "$@" keeps every argument.
  • A file sourced with arguments that runs set -- x y at top level now leaves x y behind, as bash does. shift alone, or set inside a function, subshell, pipeline or $(...), still gets the caller's parameters back.
  • for a in "$@", functions and source without extra arguments work as before.

Fixes #485, fixes #456

Thanks @trieloff for the clear report. Happy to change anything if you'd like it done differently.

…hout an in list

for a; do and for a do read $@ as one string and split it on spaces, so parameters with spaces were split and empty ones dropped. Iterate $1 through $# instead, as "$@" expansion already does.

Fixes vercel-labs#485
@vercel

vercel Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

@abhiramshaji-cyber is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

@auto-maintain

auto-maintain Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

🤖 auto-maintain review

Automated, advisory triage for @abhiramshaji-cyber's PR. Facts below are read from the GitHub API.

Check Result
Author's merged PRs (this repo) 0 — ⚠️ first-time contributor
Account established ✅ (age 443d · 30 followers · 144 public repos)
Commits signed/verified ✅ 10/10
Changeset included ✅ (.changeset/for-positional-params.md, .changeset/positional-over-nine.md)

Review panel: 🟢 low highest severity

just-bash maintainer code review: 🟢 low

No actionable correctness or maintainability issues found in the complete diff.

General code review: 🟢 low

No actionable defects found in the complete base-to-head diff.

Adversarial security: 🟢 low

No actionable adversarial security issues found in the complete diff.

Adversarial security (second opinion): 🟢 low

Focused interpreter fix for positional-parameter handling in `for` loops, `source`, and scripts run by path. The diff touches only interpreter source, tests, and changesets — no dependency, lockfile, CI, install-script, network, process-execution, or filesystem-boundary changes, and no obfuscated or hidden content. The new `helpers/positional.ts` only matches `^[1-9]\d*$` keys, so `$0` and special parameters are untouched; the new `for`-without-`in` path bounds work with the same `maxArrayElements` check `"$@"` expansion already uses and tolerates a non-numeric or oversized seeded `$#`; the new `positionalSetBySet` flag is saved/restored at all five env-snapshot sites (pipeline, command substitution, pattern expansion, process substitution, isolated shell state), and function/source restore runs in a `finally`/cleanup on every path. No security-relevant defects found.

Standard Bash and host portability: 🟢 low

No actionable Bash or host-portability issues found.

Posted by auto-maintain. This automated code review is advisory; a human maintainer makes the call.

…arameters in a for loop

A seeded $# could make a for loop without an in list allocate before any limit applied. Check it against maxArrayElements first, as the in list path does.
…run by path

source and the script runner only set $1 through $9, so ${10} and later were empty and "$@" and for a; do lost arguments after the ninth. Set every positional parameter the way function calls do, and restore all of the caller's parameters after source.

Fixes vercel-labs#456
@abhiramshaji-cyber abhiramshaji-cyber changed the title fix(interpreter): iterate each positional parameter in a for loop without an in list fix(interpreter): pass every positional parameter to for loops, source and scripts Oct 30, 2026
@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Thanks for the reviews. Two updates:

  1. The $# allocation point was fair: a seeded $# could make for a; do allocate before any limit applied. It now checks maxArrayElements first, the same way the in list path does (9f4f687).
  2. The more than nine arguments point was also right: source and scripts run by path only set $1 through $9 (Scripts run by path and source only get $1..$9; ${10}+ and "$@" beyond 9 are empty #456), so this PR would have emptied for a; do there. Since nobody had picked up Scripts run by path and source only get $1..$9; ${10}+ and "$@" beyond 9 are empty #456, I fixed it here (59fc669) and added tests for source and a script run by path with 11 arguments. source s.sh 1 … 11 now prints (10)(11) for "$@" where main printed ()().

Happy to split #456 into its own PR if you'd prefer.

…ist, not from $#

Script, source and function setup cleared numbered parameters up to the caller's $#, so a seeded $# such as `env '#=1000000000' /s.sh x` ran an unbounded synchronous loop, a non numeric $# skipped setting the arguments at all, and a parameter set beyond $# leaked into the callee. Walk the numbered parameters actually present in the environment instead, so the work is bounded by real state and every $# value behaves the same.
…each call

Scanning every environment entry made each function call cost time in proportion to the number of shell variables. Walk numbered keys from $1 while they exist, past the new arguments, as `set --` already does, so each call stays bounded by its arguments and the caller's real parameters.
…e and functions return

Restore only put back the saved keys, so `set --` inside a sourced file or a function left extra parameters behind with a smaller $#. Clear the current parameters before restoring, and always cover $1 through $9 as before so a seeded gap there cannot leak into a script.
…h does

At top level bash keeps parameters a sourced file sets with set, and restores them only when source runs inside a function, so assert the case bash defines.
abhiramshaji-cyber added a commit to abhiramshaji-cyber/just-bash that referenced this pull request Nov 8, 2026
Gaps now stay unset after shift, so a set -u script that read one of
them (source with more than nine arguments before vercel-labs#729, or a host seeded
$# above the stored keys) died with "unbound variable" where main printed
an empty line. getVariable now raises the nounset error for $N only when
N is above $#, which is what bash means by $#. The sourced file test also
checks the moved values.
abhiramshaji-cyber added a commit to abhiramshaji-cyber/just-bash that referenced this pull request Nov 8, 2026
The parser and the stored key move now live in helpers/stored-params.ts.
shift checks the $# bound before the range check, so every shift other
than shift 0 on a $# above 2 ** 63 - 1 gives the same error. set -u reads
$# with the same bounded parser instead of parseInt, which rounded above
2 ** 53 and reparsed a long $# on every reference. set -- now removes the
stored keys up to the old $# before its existing walk, so a gap left by
shift no longer keeps later keys alive. The host limit tests no longer
set maxArrayElements, which shift does not read, and the sourced file
test checks exact values with and without vercel-labs#729.
abhiramshaji-cyber added a commit to abhiramshaji-cyber/just-bash that referenced this pull request Nov 8, 2026
After moveStoredParams removed $1 to $#, the existing walk still started
at 1, found nothing and stopped, so keys above the old $# (the caller's
$10 and $11 inside a sourced file, or a stored key past a seeded $#)
survived where main cleared them. The walk now starts at $# + 1. The
sourced set -u test pins the output without vercel-labs#729 instead of probing for
it.
abhiramshaji-cyber added a commit to abhiramshaji-cyber/just-bash that referenced this pull request Nov 8, 2026
The sourced set -u test pinned output that changes once vercel-labs#729 stores $10
and later, so it now uses a seeded gap that does not depend on vercel-labs#729. New
tests cover set -u with a non numeric or too large $#, where every $N
past the stored keys still errors, and set -- with a seeded $# above
env.size, which takes the scan path. The changeset now says shift 0
still returns 0 above 2 ** 63 - 1 and describes the set -- clearing.
@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hey @jamalkamaladdin, hope you're doing well! I know you have a lot on your plate, so no rush at all. Just a gentle check in on this one: the $# thread is addressed in 13c0ad6 and 6ce40bb, and the shift part you suggested splitting out is up separately in #734. The only failing checks are the two Vercel deploys, which need a maintainer to authorize them since this comes from a fork. Is there anything else that would help get this ready?

@privatenumber privatenumber added awaiting-review Maintainer review or re-review is next. Does not imply approval or merge readiness. bug Something isn't working p2 Normal backlog action. Priority is not acceptance or a delivery promise. shell Parsing, quoting, expansion, pipelines, redirection, and shell execution labels Nov 20, 2026
@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hey @privatenumber, hope you're doing well! Thanks for triaging this one, and I know the review queue is long right now. I merged the latest main into the branch (no conflicts, and typecheck, lint plus the unit and comparison suites all pass locally), so the new commit's workflow runs need approving again. Whenever you get a moment, could you approve them? And if anything would make this easier to review, or you'd like it scoped differently, I'm happy to change it.

@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Thanks for the second look! Both points were real, and I checked each one against bash 5.3.

  1. source and set --: bash keeps the new parameters when a file sourced with arguments replaces them with set outside a function, and restores the caller's otherwise. So set -- a; source s x, with s running set -- 1 2, now leaves $# at 2. shift alone, set -- inside a function, a subshell, a pipeline or $(...), and any source run from a function still restore a, the same as bash. Fixed in 63969da.
  2. Gaps above $9: the cleanup now finds every numbered parameter instead of stopping at the first missing one, so a seeded 11=outer stays hidden inside functions, source and scripts, and comes back afterwards. set -- uses the same helper now, so it clears it too. Fixed in b699980.

New tests cover both, and typecheck, lint, knip, and the unit and comparison suites pass locally. If the set part would be easier to review on its own, I'm happy to split it out.

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

awaiting-review Maintainer review or re-review is next. Does not imply approval or merge readiness. bug Something isn't working p2 Normal backlog action. Priority is not acceptance or a delivery promise. shell Parsing, quoting, expansion, pipelines, redirection, and shell execution

Projects

None yet

3 participants