Skip to content

fix: prevent hash overflow to NaN in getColorFromString (#651)#689

Open
thairc-dev wants to merge 2 commits into
tscircuit:mainfrom
thairc-dev:fix/prevent-hash-overflow-getcolorfromstring
Open

fix: prevent hash overflow to NaN in getColorFromString (#651)#689
thairc-dev wants to merge 2 commits into
tscircuit:mainfrom
thairc-dev:fix/prevent-hash-overflow-getcolorfromstring

Conversation

@thairc-dev

Copy link
Copy Markdown

Fixes #651

Cause

When passing long strings (e.g. "n".repeat(300)), accumulating acc * 31 + charCodeAt(0) overflows JavaScript's Number.MAX_VALUE to Infinity. Subsequently, Infinity % 360 evaluates to NaN, returning an invalid color string hsl(NaN, 100%, 50%, 1).

Solution

  • Use 32-bit integer bitwise OR (| 0) on each hash accumulation step to keep the hash bounded within signed 32-bit integer range.
  • Use Math.abs(hash) % 360 to guarantee a valid non-negative hue integer between 0 and 359.

Verification

Added tests/getColorFromString.test.ts verifying valid HSL format, alpha parameter handling, and long string handling ("n".repeat(300)). All tests pass cleanly.

@vercel

vercel Bot commented Jul 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
schematic-trace-solver Ready Ready Preview, Comment Jul 22, 2026 10:21am

Request Review

@thairc-dev

Copy link
Copy Markdown
Author

Unit test tests/get-color-from-string.test.ts passes 100% (3/3). Biome format check, type check, and Vercel preview deployment are all green. Ready for review!

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.

getColorFromString returns hsl(NaN, ...) for very long strings

1 participant