fix: stop distribute() from looping forever on a non-positive count#506
Open
spokodev wants to merge 1 commit into
Open
fix: stop distribute() from looping forever on a non-positive count#506spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
distribute(count) used `for (; count !== 0; count--)`, which only reaches 0 for a positive integer count. A negative or fractional count decrements past 0 forever, allocating currency objects until the process runs out of memory: currency(1).distribute(-1); // never returns -> heap out of memory currency(1).distribute(2.5); // never returns -> heap out of memory `count` comes straight from the public, number-typed API, so a caller-supplied count can crash the process. Bound the loop with `for (let i = 0; i < count; i++)` so it always terminates; a non-positive count yields an empty distribution, consistent with the existing distribute(0) behaviour. Positive-integer counts are unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
distribute(count)loops withfor (; count !== 0; count--), which only reaches0for a positive integercount. A negative or fractional count decrements past0forever, pushingcurrencyobjects into the result array until the process runs out of memory:countis part of the public,number-typed API (distribute(count: number)), so a caller-supplied (e.g. user-controlled) value can hang and crash the process.distribute(0)already returns[], so the positive-integer assumption is only implicit.Fix
Bound the loop with
for (let i = 0; i < count; i++)so it always terminates. A non-positive count now yields an empty distribution (consistent withdistribute(0)), and positive-integer counts behave exactly as before (the loop body never referencedcount).Test
Added a test asserting
distribute(0)anddistribute(-1)return[]instead of looping forever. The full suite passes (63 tests).