io: allow to restrict slice size in ReadVarBytes
This commit is contained in:
parent
7e2e5e1879
commit
02d0874200
2 changed files with 38 additions and 1 deletions
|
@ -168,8 +168,16 @@ func (r *BinReader) ReadVarUint() uint64 {
|
|||
|
||||
// ReadVarBytes reads the next set of bytes from the underlying reader.
|
||||
// ReadVarUInt() is used to determine how large that slice is
|
||||
func (r *BinReader) ReadVarBytes() []byte {
|
||||
func (r *BinReader) ReadVarBytes(maxSize ...int) []byte {
|
||||
n := r.ReadVarUint()
|
||||
ms := maxArraySize
|
||||
if len(maxSize) != 0 {
|
||||
ms = maxSize[0]
|
||||
}
|
||||
if n > uint64(ms) {
|
||||
r.Err = fmt.Errorf("byte-slice is too big (%d)", n)
|
||||
return nil
|
||||
}
|
||||
b := make([]byte, n)
|
||||
r.ReadBytes(b)
|
||||
return b
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue