Repository navigation
fix(interpreter): pass every positional parameter to for loops, source and scripts - #729
abhiramshaji-cyber wants to merge 10 commits into
Conversation
…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
|
@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 reviewAutomated, advisory triage for
Review panel: 🟢 low highest severity just-bash maintainer code review: 🟢 low
General code review: 🟢 low
Adversarial security: 🟢 low
Adversarial security (second opinion): 🟢 low
Standard Bash and host portability: 🟢 low
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
|
Thanks for the reviews. Two updates:
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.
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.
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.
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.
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.
|
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 |
|
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. |
|
Thanks for the second look! Both points were real, and I checked each one against bash 5.3.
New tests cover both, and typecheck, lint, knip, and the unit and comparison suites pass locally. If the |
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
executeForincontrol-flow.tsrunsforloops, includingfor a; doandfor a do, which loop over the positional parameters. Thesourcebuiltin,executeUserScript(a script run by path) andcallFunctionset$1,$2and 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.callFunctionsets$1toa b,$2to empty and$3toc.executeForsees noinlist and reads$@as one string:a b c..split(" ").filter(Boolean)on it.[a],[b],[c]: three wrong items instead of[a b],[],[c].source s.sh a b c d e f g h i j ksets$#to 11, butsourceandexecuteUserScriptstop at$9, so${10}and${11}are empty and"$@"prints()()for them (Scripts run by path andsourceonly get $1..$9; ${10}+ and "$@" beyond 9 are empty #456).After this PR
Same person, same command.
executeForchecks$#againstmaxArrayElements, the same way theinlist path does.$1through$#one at a time, the same way"$@"expansion reads them.[a b],[],[c].sourceand scripts run by path set every argument, so${10}isjand${11}isk.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:#111827The change
control-flow.ts: the fix for for x; do (no "in") splits the positional parameters on spaces and drops empty ones #485.executeForloops over$1through$#instead of splitting$@, after checking$#againstmaxArrayElements.builtins/source.ts,subshell-group.ts,functions.tsand the newhelpers/positional.ts: the fix for Scripts run by path andsourceonly get $1..$9; ${10}+ and "$@" beyond 9 are empty #456, needed sofor a; doworks in sourced scripts with more than nine arguments.replacePositionalParamssaves 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 as11=outerwith no10is still hidden.restorePositionalParamsclears the callee's parameters before putting the caller's back, soset --inside a function no longer leaks extra ones.set --inbuiltins/set.tsuses the same clearing.builtins/source.tsalso follows bash when a file sourced with arguments runssetoutside a function: those new parameters stay. ApositionalSetBySetflag is turned on byset, cleared by eachsourceand each function return, and saved and restored by subshells, pipeline stages,$(...)and<(...), soset --in any of those still letssourcerestore the caller's parameters.@directly as an environment variable, which only the old split read, so it now usesset --.control-flow.for-positional.test.ts,positional-over-nine.test.tsandbuiltins/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; doandfor a dokeep spaces, tabs, newlines, glob characters and empty arguments, in functions, sourced files and scripts.source file a … kand./script a … ksee${10}and later, and"$@"keeps every argument.set -- x yat top level now leavesx ybehind, as bash does.shiftalone, orsetinside a function, subshell, pipeline or$(...), still gets the caller's parameters back.for a in "$@", functions andsourcewithout 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.