Repository navigation
Fix hang on size below 1 in customRandom() - #623
Closed
nk-batman21 wants to merge 1 commit into
Closed
nk-batman21 wants to merge 1 commit into
nk-batman21 wants to merge 1 commit into
Conversation
Owner
|
2% is too much for absolutely theoretical issue. Please don't open based on LLM ideas. Open source maintainers prefer spend time for real issue. |
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.
Since #616 (released in 6.0.2), the power-of-two branch of
customRandom()never returns when the size is between 0 and 1.if (!size)lets0.5through, thensize |= 0turns it into0, so the inner loop does nothing andwhile (true)spins forever. The browser tab or Node process freezes.It is reachable through the public API. In the browser, React Native and edge builds
customAlphabet()iscustomRandom(), so a computed size likecustomAlphabet('0123456789abcdef')(length / 2)withlength = 1hangs. In Node,customRandom(urlAlphabet, 10, random)(0.5)and multi-byte power-of-two alphabets such ascustomAlphabet('абвг')(0.5)hang the same way. Before #616 these calls returned one character, andnanoid(0.5)and the NodecustomAlphabet()fast path already return''.This change moves the existing
size |= 0into the empty-size check, so the check sees the integer. Sizes that truncate to 0, like0.5or-0.5, now return''likenanoid().Infinityalso returns''now (it looped in Node and threw in the browser build), the same asnanoid(Infinity). Normal integer and larger fractional sizes behave as before, negative ones still throw, andgetRandomalways gets an integer.customAlphabetgrows by 6 B (220 B to 226 B), so I bumped the limit. A micro-benchmark of the power-of-two path in both builds showed no difference beyond run-to-run noise.Ran:
bnt --coverage 100(90 tests pass, 100% coverage),oxlint,size-limitandtest/check-prebuild.json Node 24.Checked: the new test hangs on
mainand passes with the change.