fixed overflow on read

This commit is contained in:
Todd Gruben 2017-08-02 10:05:01 -05:00
parent 091e982263
commit 6dacd287b4
2 changed files with 30 additions and 12 deletions

View file

@ -615,7 +615,7 @@ func (b *Bitmap) UnmarshalBinary(data []byte) error {
b.keys[i] = binary.LittleEndian.Uint64(buf[0:8])
b.containers[i] = &container{
container_type: byte(binary.LittleEndian.Uint16(buf[8:10])),
n: int(binary.LittleEndian.Uint16(buf[10:12]) + 1),
n: int(binary.LittleEndian.Uint16(buf[10:12])) + 1,
mapped: true,
}
}
@ -1236,7 +1236,6 @@ func (c *container) Optimize() {
}
runs := c.countRuns()
// First decide which type to use.
var newType byte
if runs <= RunMaxSize && runs <= c.n/2 {
newType = ContainerRun
@ -1259,22 +1258,12 @@ func (c *container) Optimize() {
} else if newType == ContainerRun {
c.bitmapToRun()
}
e := c.check()
if e != nil {
fmt.Printf("Bitmap %v\n", c)
panic(e)
}
} else if c.isRun() {
if newType == ContainerBitmap {
c.runToBitmap()
} else if newType == ContainerArray {
c.runToArray()
}
e := c.check()
if e != nil {
fmt.Printf("Run %v\n", c)
panic(e)
}
}
}

View file

@ -1634,6 +1634,35 @@ func TestWriteReadBitmap(t *testing.T) {
}
}
func TestWriteReadFullBitmap(t *testing.T) {
// create bitmap containing > 4096 bits
cb := &container{bitmap: make([]uint64, bitmapN), n: 65536, container_type: ContainerBitmap}
for i := 0; i < bitmapN; i++ {
cb.bitmap[i] = 0xffffffffffffffff
}
bb := &Bitmap{keys: []uint64{0}, containers: []*container{cb}}
bb2 := &Bitmap{}
var buf bytes.Buffer
_, err := bb.WriteTo(&buf)
if err != nil {
t.Fatalf("error writing: %v", err)
}
err = bb2.UnmarshalBinary(buf.Bytes())
if err != nil {
t.Fatalf("error unmarshaling: %v", err)
}
if !reflect.DeepEqual(bb2.containers[0].bitmap, cb.bitmap) {
t.Fatalf("bitmap test expected %x, but got %x", cb.bitmap, bb2.containers[0].bitmap)
}
if bb2.containers[0].n != cb.n {
t.Fatalf("bitmap test expected count %x, but got %x", cb.n, bb2.containers[0].n)
}
if bb2.containers[0].count() != cb.count() {
t.Fatalf("bitmap test expected count %x, but got %x", cb.n, bb2.containers[0].n)
}
}
func TestWriteReadRun(t *testing.T) {
cr := &container{runs: []interval16{{start: 3, last: 13}, {start: 100, last: 109}}, n: 21, container_type: ContainerRun}
br := &Bitmap{keys: []uint64{0}, containers: []*container{cr}}