Repository navigation
Fix shuffle hanging on empty or invalid ranges - #304
Open
draarivpatel-ui wants to merge 1 commit into
Open
draarivpatel-ui wants to merge 1 commit into
draarivpatel-ui wants to merge 1 commit into
Conversation
When start is greater than stop, or the range bounds are non-integer or out of range, m = i1 - i0 starts negative or fractional and while (m) never reaches zero, so shuffle loops forever writing garbage properties. Normalize the bounds with Math.floor and clamp them to the array so the range is treated as empty instead. Fixes d3#302.
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.
Fixes #302.
Problem
shuffle(array, start, end)computesm = i1 - i0and loopswhile (m). When the range is empty or invalid,mstarts negative (or fractional) and decrements away from zero forever, so the loop never ends. In Node it eventually throwsRangeError: Too many properties to enumerateafter writing garbage properties at negative/fractional indices.Reproduction:
Fix
Normalize the bounds before computing
m: floor both to integers, clampi0below at 0, and clampi1to[i0, array.length]. An empty or invalid range now yieldsm = 0and returns the array unchanged, matching the behavior suggested in the issue. Out-of-range ends can no longer write past the array.Integer in-range bounds are unaffected; all existing tests still pass.
Test
Added four cases to
test/shuffle-test.jscovering start > end, start past the end of the array, non-integer bounds, and an end past the array length. They timed out / crashed before the fix and pass after. Ranpnpm vitest run test/shuffle-test.js: 8/8 pass.