Skip to content

fix(utils): prevent numeric overflow to NaN in getColorFromString for long strings - #818

Open
rudra496 wants to merge 3 commits into
tscircuit:mainfrom
rudra496:fix/get-color-from-string-overflow
Open

fix(utils): prevent numeric overflow to NaN in getColorFromString for long strings#818
rudra496 wants to merge 3 commits into
tscircuit:mainfrom
rudra496:fix/get-color-from-string-overflow

Conversation

@rudra496

Copy link
Copy Markdown

Summary

Fixes #651.

Previously, `getColorFromString` computed its hash accumulator with floating-point arithmetic without bitwise integer coercion (`acc * 31 + char.charCodeAt(0)`). For long net/chip strings (e.g. `"n".repeat(300)`), this overflowed to `Infinity`, resulting in `hsl(NaN, 100%, 50%, 1)`.

Changes

  1. 32-Bit Integer Arithmetic: Coerced hash accumulator step with `| 0` to stay within 32-bit signed integer space.
  2. Non-Negative Hue Normalization: Evaluated hue with `Math.abs(hash) % 360` to guarantee valid hue values between `0` and `359`.
  3. Unit Tests: Added `tests/utils/getColorFromString.test.ts` verifying long strings produce valid HSL colors without `NaN`.

Verification

  • `bun test tests/utils/getColorFromString.test.ts` (2/2 passing).

… long strings

Fixes tscircuit#651 by coercing hash calculation to 32-bit integer arithmetic and using Math.abs(hash) % 360 to guarantee valid HSL color strings.
Copilot AI lite review requested due to automatic review settings August 15, 2026 18:46
@vercel

vercel Bot commented Aug 15, 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 Aug 15, 2026 10:33pm

Request Review

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d979b004f8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lib/utils/getColorFromString.ts Outdated
// 32-bit integer pseudo-random hash from string to prevent overflow to Infinity
const hash = string.split("").reduce((acc, char) => {
return acc * 31 + char.charCodeAt(0)
return (acc * 31 + char.charCodeAt(0)) | 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep finite hashes compatible with SVG snapshots

When the existing SVG snapshot tests run, this truncates every intermediate hash to 32 bits, so ordinary finite IDs change color even though they did not hit the long-string overflow path; for example connectivity_net1 in the committed example snapshots goes from hsl(40, ...) with the previous reducer to hsl(196, ...) here. The solver snapshot tests compare the generated SVG colors exactly via toMatchSolverSnapshot, and no snapshot fixtures were updated in this commit, so the visual test suite will fail for snapshots containing these IDs unless the finite-string hues are preserved or the affected snapshots are regenerated.

Useful? React with 👍 / 👎.

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

2 participants