Skip to content

Commit 9c000df

Browse files
committed
fix runtime max validation
1 parent dfcb8c1 commit 9c000df

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

src/runtime.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ export async function runScript(source, opts = {}) {
121121
}
122122
const out = opts.out || ((s) => console.log(s));
123123
const max = Number.isFinite(opts.max) ? opts.max : DEFAULT_MAX;
124+
if (!Number.isInteger(max) || max < 1) {
125+
throw new Error(`moshscript: max must be a positive integer, got ${JSON.stringify(opts.max)}`);
126+
}
124127
const control = makeControl(max, out);
125128

126129
const ctx = {

test/runtime.test.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ test("DEFAULT_MAX bounds an unbounded while when no max is passed", async () =>
112112
assert.equal(calls.length, DEFAULT_MAX);
113113
});
114114

115+
test("runScript rejects invalid max values", async () => {
116+
const { registry } = recorder();
117+
118+
await assert.rejects(
119+
() => runScript(`while (alive) {}`, { commands: registry, max: 0 }),
120+
/max must be a positive integer/,
121+
);
122+
await assert.rejects(
123+
() => runScript(`while (alive) {}`, { commands: registry, max: 1.5 }),
124+
/max must be a positive integer/,
125+
);
126+
});
127+
115128
test("a rejecting fire-and-forget verb does not kill a script that keeps running", async () => {
116129
const calls = [];
117130
const registry = createRegistry([

0 commit comments

Comments
 (0)