Copy value received by MergeOlder/MergeNewer so caller can't trash the merge result by modifying the contents.

This commit is contained in:
Jonathan Moody 2022-05-24 09:17:45 -04:00 committed by Roy Lee
parent 0241e18f42
commit 5f7b1f1b4f

View file

@ -30,13 +30,17 @@ func (a *pooledMerger) Swap(i, j int) {
} }
func (a *pooledMerger) MergeNewer(value []byte) error { func (a *pooledMerger) MergeNewer(value []byte) error {
a.values = append(a.values, value) vc := a.pool.Get().([]byte)[:0]
vc = append(vc, value...)
a.values = append(a.values, vc)
a.index = append(a.index, len(a.values)) a.index = append(a.index, len(a.values))
return nil return nil
} }
func (a *pooledMerger) MergeOlder(value []byte) error { func (a *pooledMerger) MergeOlder(value []byte) error {
a.values = append(a.values, value) vc := a.pool.Get().([]byte)[:0]
vc = append(vc, value...)
a.values = append(a.values, vc)
a.index = append(a.index, -len(a.values)) a.index = append(a.index, -len(a.values))
return nil return nil
} }
@ -53,6 +57,9 @@ func (a *pooledMerger) Finish(includesBase bool) ([]byte, io.Closer, error) {
} }
func (a *pooledMerger) Close() error { func (a *pooledMerger) Close() error {
for i := range a.values {
a.pool.Put(a.values[i])
}
a.pool.Put(a.buffer) a.pool.Put(a.buffer)
return nil return nil
} }