binary: prevent runaway alloc

This commit is contained in:
Ethan Buchman
2015-04-25 13:25:44 -07:00
parent b02f088cc7
commit b3b6bfb312
3 changed files with 52 additions and 8 deletions

View File

@ -1,21 +1,35 @@
package binary
import (
. "github.com/tendermint/tendermint/common"
"io"
)
const (
ByteSliceChunk = 1024
)
func WriteByteSlice(bz []byte, w io.Writer, n *int64, err *error) {
WriteUvarint(uint(len(bz)), w, n, err)
WriteTo(bz, w, n, err)
}
func ReadByteSlice(r io.Reader, n *int64, err *error) []byte {
length := ReadUvarint(r, n, err)
length := int(ReadUvarint(r, n, err))
if *err != nil {
return nil
}
buf := make([]byte, int(length))
ReadFull(buf, r, n, err)
var buf, tmpBuf []byte
// read one ByteSliceChunk at a time and append
for i := 0; i*ByteSliceChunk < length; i++ {
tmpBuf = make([]byte, MinInt(ByteSliceChunk, length-i*ByteSliceChunk))
ReadFull(tmpBuf, r, n, err)
if *err != nil {
return nil
}
buf = append(buf, tmpBuf...)
}
return buf
}