aboutsummaryrefslogtreecommitdiffstats
path: root/rlp/encode.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-09-03 07:37:00 +0800
committerFelix Lange <fjl@twurst.com>2015-09-11 01:12:32 +0800
commitac32f52ca6e620556e7a875f0d52ddef72215532 (patch)
treea4a7ba1514e5a6570b85b27d49e14b3b00996f6c /rlp/encode.go
parente2d7c1a52303ebdd8c2956badad5e600bf93ff33 (diff)
downloaddexon-ac32f52ca6e620556e7a875f0d52ddef72215532.tar
dexon-ac32f52ca6e620556e7a875f0d52ddef72215532.tar.gz
dexon-ac32f52ca6e620556e7a875f0d52ddef72215532.tar.bz2
dexon-ac32f52ca6e620556e7a875f0d52ddef72215532.tar.lz
dexon-ac32f52ca6e620556e7a875f0d52ddef72215532.tar.xz
dexon-ac32f52ca6e620556e7a875f0d52ddef72215532.tar.zst
dexon-ac32f52ca6e620556e7a875f0d52ddef72215532.zip
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.
Diffstat (limited to 'rlp/encode.go')
-rw-r--r--rlp/encode.go13
1 files changed, 9 insertions, 4 deletions
diff --git a/rlp/encode.go b/rlp/encode.go
index b525ae4e7..a0531af01 100644
--- a/rlp/encode.go
+++ b/rlp/encode.go
@@ -90,8 +90,8 @@ func Encode(w io.Writer, val interface{}) error {
return outer.encode(val)
}
eb := encbufPool.Get().(*encbuf)
- eb.reset()
defer encbufPool.Put(eb)
+ eb.reset()
if err := eb.encode(val); err != nil {
return err
}
@@ -102,8 +102,8 @@ func Encode(w io.Writer, val interface{}) error {
// Please see the documentation of Encode for the encoding rules.
func EncodeToBytes(val interface{}) ([]byte, error) {
eb := encbufPool.Get().(*encbuf)
- eb.reset()
defer encbufPool.Put(eb)
+ eb.reset()
if err := eb.encode(val); err != nil {
return nil, err
}
@@ -288,8 +288,13 @@ type encReader struct {
func (r *encReader) Read(b []byte) (n int, err error) {
for {
if r.piece = r.next(); r.piece == nil {
- encbufPool.Put(r.buf)
- r.buf = nil
+ // Put the encode buffer back into the pool at EOF when it
+ // is first encountered. Subsequent calls still return EOF
+ // as the error but the buffer is no longer valid.
+ if r.buf != nil {
+ encbufPool.Put(r.buf)
+ r.buf = nil
+ }
return n, io.EOF
}
nn := copy(b[n:], r.piece)