Skip to content

pkg/adt: Int64Comparable.Compare overflows for extreme int64 values #22357

Description

@dfengliu

pkg/adt.Int64Comparable.Compare computes cmp := v - vc and takes the sign. The subtraction overflows for extreme values, breaking the trichotomy contract:

Int64Comparable(math.MaxInt64).Compare(Int64Comparable(math.MinInt64)) // returns -1, expected 1
Int64Comparable(math.MaxInt64).Compare(Int64Comparable(-1))            // returns -1, expected 1

On top of that, NewInt64Point(math.MaxInt64) builds [MaxInt64, MaxInt64+1) where a+1 wraps to MinInt64, producing an interval with Begin > End and violating the tree's Begin <= End invariant.

There are no in-tree production callers of the int64 interval constructors, but pkg/adt is an exported package used by downstream consumers, so the API should not return wrong orderings or invalid intervals for extreme input.

Proposed fix: compare v and vc directly without subtracting, and return a degenerate (empty) [a, a) interval for NewInt64Point(math.MaxInt64), preserving the invariant. A regression test will assert the comparator trichotomy at extremes and the point invariant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions