rlp: fix encReader returning nil buffers to the pool
The bug can cause crashes if Read is called after EOF has been returned. No code performs such calls right now, but hitting the bug gets more likely as rlp.EncodeToReader gets used in more places.
This commit is contained in:
@ -23,6 +23,7 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/big"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -306,3 +307,25 @@ func TestEncodeToReaderPiecewise(t *testing.T) {
|
||||
return output, nil
|
||||
})
|
||||
}
|
||||
|
||||
// This is a regression test verifying that encReader
|
||||
// returns its encbuf to the pool only once.
|
||||
func TestEncodeToReaderReturnToPool(t *testing.T) {
|
||||
buf := make([]byte, 50)
|
||||
wg := new(sync.WaitGroup)
|
||||
for i := 0; i < 5; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
for i := 0; i < 1000; i++ {
|
||||
_, r, _ := EncodeToReader("foo")
|
||||
ioutil.ReadAll(r)
|
||||
r.Read(buf)
|
||||
r.Read(buf)
|
||||
r.Read(buf)
|
||||
r.Read(buf)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
Reference in New Issue
Block a user