From 5a2c275be125d935440ddad3042bfc7bb2ce5027 Mon Sep 17 00:00:00 2001 From: Shenghou Ma Date: Tue, 18 Dec 2012 01:26:48 +0800 Subject: [PATCH] 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 --- src/pkg/encoding/gob/decode.go | 6 +++--- src/pkg/fmt/scan.go | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/pkg/encoding/gob/decode.go b/src/pkg/encoding/gob/decode.go index 900c69ddb4..a80d9f9195 100644 --- a/src/pkg/encoding/gob/decode.go +++ b/src/pkg/encoding/gob/decode.go @@ -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 diff --git a/src/pkg/fmt/scan.go b/src/pkg/fmt/scan.go index 62de3a2efa..6a282c81f1 100644 --- a/src/pkg/fmt/scan.go +++ b/src/pkg/fmt/scan.go @@ -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 }