Repository navigation
Conversation
customRandom() looped forever when the alphabet was empty (`safeByteCutoff` is NaN) or bigger than 256 symbols (`safeByteCutoff` is 0), because no random byte could ever pass the rejection filter. customAlphabet() inherits the hang, since it falls back to customRandom() for those alphabets. Throw a RangeError when a non-zero size is requested instead. Zero size still returns an empty string, as before. The check lives in the non-power-of-two branch only: invalid cutoffs never equal 256, so they always reach it. The customAlphabet size limit grows 220 B to 251 B.
The code fix in the previous commit turns the documented infinite loop into a RangeError, so the README sentence saying the generator 'can loop forever' is no longer true.
ai
reviewed
Nov 19, 2026
| "name": "customAlphabet", | ||
| "import": "{ customAlphabet }", | ||
| "limit": "220 B" | ||
| "limit": "251 B" |
Owner
There was a problem hiding this comment.
It is too big price for only theoretical 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.
Hi, thanks for nanoid. This turns a documented infinite loop into an error.
Bug:
customRandom()(andcustomAlphabet(), which falls back to it for these alphabets) loops forever when the alphabet is empty or has more than 256 symbols, for any non-zero size. Empty alphabet:256 % 0 = NaN, sosafeByteCutoffis NaN and no byte can ever pass the filter. Over-256 alphabet:256 % len = 256, sosafeByteCutoffis 0 and again no byte passes. Verified on current main with a 5s timeout:customAlphabet('', 5)()never returns (exit 124). The README already documents this ("can loop forever instead of returning an ID") — #609 documented it but only changed the docs, the code still hangs.Fix: in the non-power-of-two branch of
customRandom(), right after theif (!size) return ''check, throwRangeError('Wrong alphabet size')when!safeByteCutoff. Invalid cutoffs (NaN/0) can never equal 256, so they always reach this check; size 0 still returns''as before. Applied to bothindex.jsandindex.browser.js, with tests for both intest/index.test.js.Size-limit note: this moves the
customAlphabetlimit from 220 B to 251 B (+31 B), so I updated the limit inpackage.jsonfollowing the repo's convention of limits matching current size. I tried reusing the existing'Wrong ID size'message first, but that came out larger (259 B), so I kept the clearer'Wrong alphabet size'.Docs: also updated the README sentence that said the generator "can loop forever" — it now says it throws a
RangeError.Tests: full suite 92/92 passing (node --test on all test files), coverage 100% per the subagent's
pnpm testrun, oxlint clean, size-limit passing.