fix overflow in differenceRunBitmap

This commit is contained in:
Todd Gruben 2017-11-10 12:44:25 -06:00
parent 27a91a6465
commit 50d31a3c83
2 changed files with 11 additions and 3 deletions

View file

@ -2508,7 +2508,7 @@ func differenceRunArray(a, b *container) *container {
// differenceRunBitmap computes the difference of an run from a bitmap.
func differenceRunBitmap(a, b *container) *container {
// If a is full, difference is the flip of b.
if a.runs[0].start == 0 && a.runs[0].last == 65535 {
if len(a.runs) > 0 && a.runs[0].start == 0 && a.runs[0].last == 65535 {
return b.flipBitmap()
}
output := &container{container_type: ContainerRun}
@ -2534,6 +2534,9 @@ func differenceRunBitmap(a, b *container) *container {
break
}
}
if bit == 65535 { //overflow
break
}
}
if run.start <= run.last {
output.runs = append(output.runs, run)

View file

@ -1524,14 +1524,19 @@ func TestDifferenceRunBitmap(t *testing.T) {
bitmap: MakeBitmap([]uint64{0x0, 0x8000000000000000}),
exp: []interval16{{start: 0, last: 65}},
},
{
runs: []interval16{{start: 1, last: 65535}},
bitmap: MakeBitmap([]uint64{0x0000000000000001}),
exp: []interval16{{start: 1, last: 65535}},
},
}
for i, test := range tests {
a.runs = test.runs
a.n = a.runCountRange(0, 100)
a.n = a.runCountRange(0, 65535)
for i, v := range test.bitmap {
b.bitmap[i] = v
}
b.n = b.bitmapCountRange(0, 100)
b.n = b.bitmapCountRange(0, 65535)
ret := differenceRunBitmap(a, b)
if !reflect.DeepEqual(ret.runs, test.exp) {
t.Fatalf("test #%v expected %v, but got %v", i, test.exp, ret.runs)