Skip to content

Commit 9aadcec

Browse files
committed
fix: Safety guard on random(negative)
1 parent ccc263c commit 9aadcec

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

src/engine/script/handlers/NumberOps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ const NumberOps: CommandHandlers = {
3030
},
3131

3232
[ScriptOpcode.RANDOM]: state => {
33-
const a = state.popInt();
33+
const a = Math.max(0, state.popInt());
3434
state.pushInt(JavaRandom.nextInt(a));
3535
},
3636

3737
[ScriptOpcode.RANDOMINC]: state => {
38-
const a = state.popInt();
38+
const a = Math.max(0, state.popInt());
3939
state.pushInt(JavaRandom.nextInt(a + 1));
4040
},
4141

src/util/JavaRandom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function next(bits: number) {
5757

5858
function checkIsPositiveInt(n: number, r = Number.MAX_SAFE_INTEGER) {
5959
if (n < 0 || n > r) {
60-
throw RangeError();
60+
throw RangeError('number must be > 0');
6161
}
6262
}
6363

0 commit comments

Comments
 (0)