Skip to content

Throw on empty or bigger-than-256 alphabet instead of hanging - #627

Closed
Ultroncn wants to merge 2 commits into
ai:mainfrom
Ultroncn:fix/invalid-alphabet-hang
Closed

Ultroncn wants to merge 2 commits into
ai:mainfrom
Ultroncn:fix/invalid-alphabet-hang

Conversation

@Ultroncn

Copy link
Copy Markdown

Hi, thanks for nanoid. This turns a documented infinite loop into an error.

Bug: customRandom() (and customAlphabet(), 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, so safeByteCutoff is NaN and no byte can ever pass the filter. Over-256 alphabet: 256 % len = 256, so safeByteCutoff is 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 the if (!size) return '' check, throw RangeError('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 both index.js and index.browser.js, with tests for both in test/index.test.js.

Size-limit note: this moves the customAlphabet limit from 220 B to 251 B (+31 B), so I updated the limit in package.json following 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 test run, oxlint clean, size-limit passing.

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.
Comment thread package.json
"name": "customAlphabet",
"import": "{ customAlphabet }",
"limit": "220 B"
"limit": "251 B"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is too big price for only theoretical issue

@ai ai closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants