From 4104152de6f25b4f44fffd0a6cb2ab02a7ffd172 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Mon, 9 May 2016 18:22:08 -0700 Subject: [PATCH] Use io.ReadFull instead of raw Read An io.Reader does not guarantee that it can read all bytes possible to fill the input buffer. Thus, we should use io.ReadFull here instead. Cherry-picked from ddfd4a2b2d89e20f0a7c63c88420aaa419d4d95c of https://github.com/google/certificate-transparency --- ct/serialization.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ct/serialization.go b/ct/serialization.go index e383ea5..33d5ea9 100644 --- a/ct/serialization.go +++ b/ct/serialization.go @@ -80,13 +80,12 @@ func readVarBytes(r io.Reader, numLenBytes int) ([]byte, error) { return nil, err } data := make([]byte, l) - n, err := r.Read(data) - if err != nil { + if n, err := io.ReadFull(r, data); err != nil { + if err == io.EOF || err == io.ErrUnexpectedEOF { + return nil, fmt.Errorf("short read: expected %d but got %d", l, n) + } return nil, err } - if n != int(l) { - return nil, fmt.Errorf("short read: expected %d but got %d", l, n) - } return data, nil }