Avoid allocating large amounts of memory for corrupt input.
No test case because the problem can only happen for invalid data.
Let the fuzzer find cases like this.
Fixes #53369
Change-Id: I67c5e75bf181ad84988d6d6da12507df0e6df8e8
Reviewed-on: https://go-review.googlesource.com/c/go/+/413979
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
d.offset += n
}
-// Size grows the buffer to exactly n bytes, so d.Bytes() will
-// return a slice of length n. Existing data is first discarded.
-func (d *decBuffer) Size(n int) {
- d.Reset()
- if cap(d.data) < n {
- d.data = make([]byte, n)
- } else {
- d.data = d.data[0:n]
- }
-}
-
func (d *decBuffer) ReadByte() (byte, error) {
if d.offset >= len(d.data) {
return 0, io.EOF
return d.data[d.offset:]
}
+// SetBytes sets the buffer to the bytes, discarding any existing data.
+func (d *decBuffer) SetBytes(data []byte) {
+ d.data = data
+ d.offset = 0
+}
+
func (d *decBuffer) Reset() {
d.data = d.data[0:0]
d.offset = 0
import (
"bufio"
"errors"
+ "internal/saferio"
"io"
"reflect"
"sync"
panic("non-empty decoder buffer")
}
// Read the data
- dec.buf.Size(nbytes)
- _, dec.err = io.ReadFull(dec.r, dec.buf.Bytes())
+ var buf []byte
+ buf, dec.err = saferio.ReadData(dec.r, uint64(nbytes))
+ dec.buf.SetBytes(buf)
if dec.err == io.EOF {
dec.err = io.ErrUnexpectedEOF
}
text/scanner,
text/tabwriter;
+ io, reflect
+ < internal/saferio;
+
# encodings
# core ones do not use fmt.
io, strconv
fmt !< encoding/base32, encoding/base64;
- FMT, encoding/base32, encoding/base64
+ FMT, encoding/base32, encoding/base64, internal/saferio
< encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
encoding/json, encoding/pem, encoding/xml, mime;
encoding/binary, regexp
< index/suffixarray;
- io, reflect
- < internal/saferio;
-
# executable parsing
FMT, encoding/binary, compress/zlib, internal/saferio
< runtime/debug