Skip to content

Commit 9a42fdd

Browse files
authored
Validate moshscript max loop count (#1)
1 parent 2b536c2 commit 9a42fdd

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

bin/moshcode.mjs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ function readScript(arg) {
2323
return fs.readFileSync(arg, "utf8");
2424
}
2525

26+
function parseMax(value) {
27+
if (value === undefined) throw new Error("moshcode run: --max requires a positive integer");
28+
const max = Number(value);
29+
if (!Number.isInteger(max) || max < 1) {
30+
throw new Error(`moshcode run: --max must be a positive integer, got ${JSON.stringify(value)}`);
31+
}
32+
return max;
33+
}
34+
2635
function help() {
2736
console.log(`moshcode — metal scripting toolkit 🤘
2837
@@ -80,7 +89,10 @@ async function main() {
8089
let max = 3, dryRun = false, file = null;
8190
for (let k = 0; k < rest.length; k++) {
8291
const a = rest[k];
83-
if (a === "--max" || a === "-n") max = Number(rest[++k]);
92+
if (a === "--max" || a === "-n") {
93+
try { max = parseMax(rest[++k]); }
94+
catch (e) { console.error(String(e.message || e)); process.exit(1); }
95+
}
8496
else if (a === "--dry-run") dryRun = true;
8597
else file = a;
8698
}
@@ -92,7 +104,7 @@ async function main() {
92104
const ctx = {
93105
vars: { alive: true },
94106
iter: 0,
95-
maxIterations: Number.isFinite(max) && max > 0 ? max : 3,
107+
maxIterations: max,
96108
dryRun,
97109
out: (s) => console.log(s),
98110
commands: defaultCommands(),

0 commit comments

Comments
 (0)