Skip to content

fix(types): reject float values of 2^63 when converting to int - #9887

Merged
shiva-istari merged 1 commit into
dgraph-io:mainfrom
amalsaidov:fix/float-to-int-overflow-boundary
Nov 20, 2026
Merged

shiva-istari merged 1 commit into
dgraph-io:mainfrom
amalsaidov:fix/float-to-int-overflow-boundary

Conversation

@amalsaidov

@amalsaidov amalsaidov commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

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.

Added boundary cases to TestConvertFloatToInt:

  • 2^63 - 1024 (largest float below 2^63) converts correctly
  • math.MinInt64 still converts
  • 2^63 is now rejected

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

Autofix with [code]smith
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.

@amalsaidov
amalsaidov requested a review from a team as a code owner November 13, 2026 08:30
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a4bfde25-2a42-44c2-9d70-cd684debaaf1
Commits

Reviewing files that changed from the base of the PR and between 387bd66 and 8f57d88.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

Convert now rejects FloatID values at or above math.MaxInt64 when converting to IntID. Tests cover the upper and lower boundaries.

Changes: FloatID to IntID conversion

Layer / File(s) Summary
Conversion boundary and tests
types/conversion.go, types/conversion_test.go
The upper-bound check rejects values greater than or equal to math.MaxInt64. Tests cover the largest float64 below 2^63, math.MinInt64, and 2^63.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: mlwelles, matthewmcneely

Merge Risk: Minimal · previous review 9311e

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: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • 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.

@shiva-istari
shiva-istari force-pushed the fix/float-to-int-overflow-boundary branch from 9311ee2 to 387bd66 Compare November 18, 2026 17:03
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.
@shiva-istari
shiva-istari force-pushed the fix/float-to-int-overflow-boundary branch from 387bd66 to 8f57d88 Compare November 19, 2026 10:05
@shiva-istari
shiva-istari merged commit e95fc26 into dgraph-io:main Nov 20, 2026
19 of 20 checks passed
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.

2 participants