1
0
mirror of https://github.com/golang/go synced 2024-10-03 06:11:21 -06:00

fmt, encoding/gob: fix misuse of Read

reader.Read() can return both 0,nil and len(buf),err.
To be safe, we use io.ReadFull instead of doing reader.Read directly.

Fixes #3472.

R=bradfitz, rsc, ality
CC=golang-dev
https://golang.org/cl/6285050
This commit is contained in:
Shenghou Ma 2012-12-18 01:26:48 +08:00
parent 2ad6714793
commit 5a2c275be1
2 changed files with 7 additions and 4 deletions

View File

@ -62,15 +62,15 @@ func overflow(name string) error {
// Used only by the Decoder to read the message length.
func decodeUintReader(r io.Reader, buf []byte) (x uint64, width int, err error) {
width = 1
_, err = r.Read(buf[0:width])
if err != nil {
n, err := io.ReadFull(r, buf[0:width])
if n == 0 {
return
}
b := buf[0]
if b <= 0x7f {
return uint64(b), width, nil
}
n := -int(int8(b))
n = -int(int8(b))
if n > uint64Size {
err = errBadUint
return

View File

@ -337,7 +337,10 @@ func (r *readRune) readByte() (b byte, err error) {
r.pending--
return
}
_, err = r.reader.Read(r.pendBuf[0:1])
n, err := io.ReadFull(r.reader, r.pendBuf[0:1])
if n != 1 {
return 0, err
}
return r.pendBuf[0], err
}