You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(types): reject float values of 2^63 when converting to int - #9887
When converting a FloatID value to IntID, types.Convert guards against
overflow with vc > math.MaxInt64. math.MaxInt64 is 2^63-1, which is not
representable as a float64, so in that comparison the constant rounds up to 2^63. A float holding exactly 2^63 therefore passes the guard and then
overflows in int64(vc).
The resulting value is wrong and platform dependent:
on arm64, int64(2^63) saturates to math.MaxInt64
on amd64, it wraps to math.MinInt64
Either way the conversion returns a value instead of the expected Float out of int64 range error.
The fix uses >= for the upper bound so 2^63 is rejected. The lower bound
stays < because math.MinInt64 (-2^63) is representable exactly and is a
valid conversion target, so float64(math.MinInt64) must still convert.
The new 2^63 case fails on main (no error returned) and passes with this change.
Checklist
The PR title follows the Conventional Commits syntax, leading
with fix:, feat:, chore:, ci:, etc.
Code compiles correctly and linting (via trunk) passes locally
Tests added for new functionality, or regression tests for bug fixes added as applicable
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.
Summary by CodeRabbit
Bug Fixes
Float-to-integer conversion now rejects values at or above the signed 64-bit upper limit, including the float value rounded to 2^63. Boundary cases below that limit and at the lower limit remain valid.
Float values of exactly 2^63 are now rejected with a range error instead of being converted to a platform-dependent integer. The change is small, tested at its boundaries, and has no identified remaining risk.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title names the primary change: Convert now rejects float values of 2^63 when converting to int. The raw summary confirms the upper-bound check changed from greater-than to greater-than-or-equal, …
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title names the primary change: Convert now rejects float values of 2^63 when converting to int. The raw summary confirms the upper-bound check changed from greater-than to greater-than-or-equal, and the tests add a 2^63 rejection case. The title is specific and matches the changeset.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: defaults
Review profile: CHILL
Plan: Advanced
Run ID: 27c54d75-3c3e-4927-bedd-34754f84eb52
Commits
Reviewing files that changed from the base of the PR and between c587cba and 9311ee2.
Files selected for processing: 2
types/conversion.go
types/conversion_test.go
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Converting a float64 to int64 guarded against overflow with
vc > math.MaxInt64. Because math.MaxInt64 (2^63-1) is not representable
as a float64, the constant rounds up to 2^63 in that comparison, so a
float holding exactly 2^63 passed the guard and then overflowed in
int64(vc). The result was platform dependent: int64(2^63) saturates to
MaxInt64 on arm64 and wraps to MinInt64 on amd64, either way returning a
wrong value instead of an error.
Use >= for the upper bound so 2^63 is rejected. The lower bound stays <
because math.MinInt64 (-2^63) is representable exactly and is a valid
target. Add boundary cases to TestConvertFloatToInt.
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
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.
Description
When converting a
FloatIDvalue toIntID,types.Convertguards againstoverflow with
vc > math.MaxInt64.math.MaxInt64is2^63-1, which is notrepresentable as a
float64, so in that comparison the constant rounds up to2^63. A float holding exactly2^63therefore passes the guard and thenoverflows in
int64(vc).The resulting value is wrong and platform dependent:
int64(2^63)saturates tomath.MaxInt64math.MinInt64Either way the conversion returns a value instead of the expected
Float out of int64 rangeerror.The fix uses
>=for the upper bound so2^63is rejected. The lower boundstays
<becausemath.MinInt64(-2^63) is representable exactly and is avalid conversion target, so
float64(math.MinInt64)must still convert.Added boundary cases to
TestConvertFloatToInt:2^63 - 1024(largest float below2^63) converts correctlymath.MinInt64still converts2^63is now rejectedThe new
2^63case fails onmain(no error returned) and passes with this change.Checklist
Conventional Commits syntax, leading
with
fix:,feat:,chore:,ci:, etc.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.Summary by CodeRabbit