mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2943] [Issue]: {{random:X:Y}} with a reversed range and {{roll:Xd0}} produce wrong numbers #1519
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1519
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @kolacheee on GitHub (Jun 28, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2943
Originally assigned to: @SpicyMarinara on GitHub.
What's wrong
{{random:X:Y}}and{{roll:XdY}}produce wrong numbers for a couple of common mistakes, silently, instead of handling them sensibly:{{random:5:1}}, you get a number outside the range you asked for (somewhere around 2–5), not a number between 1 and 5.{{roll:Xd0}}(zero-sided dice) just returns the dice count instead of 0 or an error — e.g.{{roll:2d0}}→2.These are small, but for a dice/random feature they give quietly incorrect results.
How to reproduce (try it yourself)
Put these in a message or character field and look at the output:
{{random:5:1}}→ You'll see a number in roughly 2–5. Expected: a number between 1 and 5 (order shouldn't matter).{{roll:2d0}}→ You'll see2. Expected:0(or treat 0 sides as invalid).Where it is
packages/shared/src/utils/macro-engine.ts—{{random:X:Y}}at line 919 computesMath.floor(Math.random() * (hi - lo + 1)) + lowith no check thathi >= lo;{{roll}}at line 926 computesMath.floor(Math.random() * s) + 1per die, which is1whensis 0.What's happening
The handlers don't normalize/validate their inputs: a reversed range makes
(hi - lo + 1)negative, ands = 0makes each die a guaranteed1.Suggested fix
In
{{random:X:Y}}, swap the bounds ifhi < lo(usemin/max). In{{roll}}, treatsides < 1as invalid (return0or leave the macro unresolved). (Clamping the upper bounds is also covered by the separate DoS issue.)Found by a code review of v2.0.6; please verify the reproductions before fixing.
@SpicyMarinara commented on GitHub (Jun 28, 2026):
Addressed in the staging fixes: {{random:X:Y}} now normalizes reversed bounds before rolling, {{roll:Xd0}} returns 0 instead of adding one per die, and dice count/sides are capped to avoid the obvious runaway loop case. Regression coverage was added for the reversed range and zero-sided dice behavior.