Skip to content

Commit 6c78d0d

Browse files
committed
overflow safety
1 parent 1ee1215 commit 6c78d0d

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

iter_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,20 @@ func TestUnset(t *testing.T) {
291291

292292
assert.Equal(t, expected, actual)
293293
})
294+
295+
t.Run("extreme max", func(t *testing.T) {
296+
b := New()
297+
b.AddInt(4294967295)
298+
299+
it := Unset(b, 4294967294, 4294967295)
300+
301+
actual := make([]uint32, 0)
302+
it(func(val uint32) bool {
303+
actual = append(actual, val)
304+
return true
305+
})
306+
expected := []uint32{4294967294}
307+
308+
assert.Equal(t, expected, actual)
309+
})
294310
}

roaring.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ func (ii *manyIntIterator) Initialize(a *Bitmap) {
744744

745745
type unsetIterator struct {
746746
min, max uint32
747-
current uint32
747+
current uint64 // use uint64 to avoid overflow
748748
it IntPeekable
749749
hasNext bool
750750
}
@@ -753,7 +753,7 @@ type unsetIterator struct {
753753
func (ui *unsetIterator) Initialize(b *Bitmap, min, max uint32) {
754754
ui.min = min
755755
ui.max = max
756-
ui.current = min
756+
ui.current = uint64(min)
757757
ui.it = b.Iterator()
758758
// Advance to first value >= min
759759
ui.it.AdvanceIfNeeded(min)
@@ -772,11 +772,11 @@ func (ui *unsetIterator) Next() uint32 {
772772
result := ui.current
773773
ui.current++
774774
ui.updateHasNext()
775-
return result
775+
return uint32(result)
776776
}
777777

778778
func (ui *unsetIterator) updateHasNext() {
779-
for ui.current <= ui.max {
779+
for ui.current <= uint64(ui.max) {
780780
if !ui.it.HasNext() {
781781
// No more set bits, we have values to yield
782782
ui.hasNext = true
@@ -790,15 +790,15 @@ func (ui *unsetIterator) updateHasNext() {
790790
return
791791
}
792792

793-
if ui.current < nextSet {
793+
if ui.current < uint64(nextSet) {
794794
// We have unset values before the next set bit
795795
ui.hasNext = true
796796
return
797797
}
798798

799799
// Skip the set bit
800800
ui.it.Next()
801-
ui.current = nextSet + 1
801+
ui.current = uint64(nextSet) + 1
802802
}
803803

804804
ui.hasNext = false

0 commit comments

Comments
 (0)