4
4
"bytes"
5
5
"context"
6
6
"fmt"
7
+ "math"
7
8
"sort"
8
9
"sync"
9
10
@@ -735,7 +736,11 @@ func findGaps(ownershipRange v1.FingerprintBounds, metas []v1.FingerprintBounds)
735
736
736
737
searchRange := ownershipRange .Slice (leftBound , clippedMeta .Max )
737
738
// update the left bound for the next iteration
738
- leftBound = min (clippedMeta .Max + 1 , ownershipRange .Max + 1 )
739
+ // We do the max to prevent the max bound to overflow from MaxUInt64 to 0
740
+ leftBound = min (
741
+ max (clippedMeta .Max + 1 , clippedMeta .Max ),
742
+ max (ownershipRange .Max + 1 , ownershipRange .Max ),
743
+ )
739
744
740
745
// since we've already ensured that the meta is within the ownership range,
741
746
// we know the xor will be of length zero (when the meta is equal to the ownership range)
@@ -750,8 +755,11 @@ func findGaps(ownershipRange v1.FingerprintBounds, metas []v1.FingerprintBounds)
750
755
gaps = append (gaps , xors [0 ])
751
756
}
752
757
753
- if leftBound <= ownershipRange .Max {
754
- // There is a gap between the last meta and the end of the ownership range.
758
+ // If the leftBound is less than the ownership range max, and it's smaller than MaxUInt64,
759
+ // There is a gap between the last meta and the end of the ownership range.
760
+ // Note: we check `leftBound < math.MaxUint64` since in the loop above we clamp the
761
+ // leftBound to MaxUint64 to prevent an overflow to 0: `max(clippedMeta.Max+1, clippedMeta.Max)`
762
+ if leftBound < math .MaxUint64 && leftBound <= ownershipRange .Max {
755
763
gaps = append (gaps , v1 .NewBounds (leftBound , ownershipRange .Max ))
756
764
}
757
765
0 commit comments