Skip to content

fix(bisector): avoid uint32 midpoint overflow above 2^31 - #305

Open
draarivpatel-ui wants to merge 1 commit into
d3:mainfrom
draarivpatel-ui:fix/bisector-midpoint-overflow
Open

draarivpatel-ui wants to merge 1 commit into
d3:mainfrom
draarivpatel-ui:fix/bisector-midpoint-overflow

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Fixes #261.

bisector.left and bisector.right computed the search midpoint as (lo + hi) >>> 1. The unsigned right shift coerces the sum to a 32-bit unsigned integer, so when the search bounds exceed 2^31 the midpoint is wrong (or points outside the search range), producing incorrect insertion points or non-termination.

This replaces the shift with Math.floor(lo + (hi - lo) / 2), which avoids both the truncation and overflow while preserving the existing behavior for small indices.

Added regression tests for bisectLeft and bisectRight with bounds above 2^31. Full suite: 533 passed, 1 skipped.

Copilot AI balanced review requested due to automatic review settings November 25, 2026 23:14

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 insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect results for binary search on large arrays due to miscomputation of midpoint

2 participants