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
This commit is contained in:
Joe Tsai 2016-05-09 18:22:08 -07:00 committed by Andrew Ayer
parent c36452f67a
commit 4104152de6
1 changed files with 4 additions and 5 deletions

View File

@ -80,13 +80,12 @@ func readVarBytes(r io.Reader, numLenBytes int) ([]byte, error) {
return nil, err return nil, err
} }
data := make([]byte, l) data := make([]byte, l)
n, err := r.Read(data) if n, err := io.ReadFull(r, data); err != nil {
if 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 return nil, err
} }
if n != int(l) {
return nil, fmt.Errorf("short read: expected %d but got %d", l, n)
}
return data, nil return data, nil
} }