Skip to content

Commit

Permalink
intervalstore: fix uint64 overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
acud committed Apr 28, 2021
1 parent dda4d16 commit 0eb06e9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
9 changes: 7 additions & 2 deletions pkg/intervalstore/intervals.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package intervalstore
import (
"bytes"
"fmt"
"math"
"strconv"
"sync"
)
Expand Down Expand Up @@ -68,19 +69,23 @@ func (i *Intervals) add(start, end uint64) {
if end < i.start {
return
}
endCheck := end + 1
if end == math.MaxUint64 {
endCheck = end
}
minStartJ := -1
maxEndJ := -1
j := 0
for ; j < len(i.ranges); j++ {
if minStartJ < 0 {
if (start <= i.ranges[j][0] && end+1 >= i.ranges[j][0]) || (start <= i.ranges[j][1]+1 && end+1 >= i.ranges[j][1]) {
if (start <= i.ranges[j][0] && endCheck >= i.ranges[j][0]) || (start <= i.ranges[j][1]+1 && endCheck >= i.ranges[j][1]) {
if i.ranges[j][0] < start {
start = i.ranges[j][0]
}
minStartJ = j
}
}
if (start <= i.ranges[j][1] && end+1 >= i.ranges[j][1]) || (start <= i.ranges[j][0] && end+1 >= i.ranges[j][0]) {
if (start <= i.ranges[j][1] && endCheck >= i.ranges[j][1]) || (start <= i.ranges[j][0] && endCheck >= i.ranges[j][0]) {
if i.ranges[j][1] > end {
end = i.ranges[j][1]
}
Expand Down
19 changes: 18 additions & 1 deletion pkg/intervalstore/intervals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package intervalstore

import "testing"
import (
"math"
"testing"
)

// Test tests Interval methods Add, Next and Last for various
// initial state.
Expand Down Expand Up @@ -471,3 +474,17 @@ func TestMerge(t *testing.T) {
}
}
}

// TestMaxUint64 is a regression test to verify that interval
// is handled correctly at the edges.
func TestMaxUint64(t *testing.T) {
intervals := NewIntervals(1)
intervals.Add(math.MaxUint64-1, math.MaxUint64)
intervals.Add(1, math.MaxUint64)
intervals.Add(2, math.MaxUint64)
intervals.Add(math.MaxUint64, math.MaxUint64)
wantstr := "[[1 18446744073709551615]]"
if s := intervals.String(); s != wantstr {
t.Fatalf("got interval string '%s' want '%s'", s, wantstr)
}
}

0 comments on commit 0eb06e9

Please sign in to comment.