From b579f6f4d4390997573af7d05bf83dc16d533479 Mon Sep 17 00:00:00 2001 From: Yawning Angel Date: Mon, 26 May 2014 04:43:46 +0000 Subject: [PATCH] Use io.ReadFull in places where it is appropriate. --- csrand/csrand.go | 9 +++------ framing/framing.go | 11 ++--------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/csrand/csrand.go b/csrand/csrand.go index 5a86da0..721fb1e 100644 --- a/csrand/csrand.go +++ b/csrand/csrand.go @@ -37,6 +37,7 @@ import ( cryptRand "crypto/rand" "encoding/binary" "fmt" + "io" "math/rand" ) @@ -44,7 +45,7 @@ var ( csRandSourceInstance csRandSource // CsRand is a math/rand instance backed by crypto/rand CSPRNG. - CsRand = rand.New(csRandSourceInstance) + CsRand = rand.New(csRandSourceInstance) ) type csRandSource struct { @@ -85,13 +86,9 @@ func IntRange(min, max int) int { // Bytes fills the slice with random data. func Bytes(buf []byte) error { - n, err := cryptRand.Read(buf) + _, err := io.ReadFull(cryptRand.Reader, buf) if err != nil { - // Yes, the go idiom is to check the length, but we panic() when it - // does not match because the system is screwed at that point. return err - } else if n != len(buf) { - panic(fmt.Sprintf("Bytes: truncated rand.Read (%d, %d)", n, len(buf))) } return nil diff --git a/framing/framing.go b/framing/framing.go index b51a7af..66eeff5 100644 --- a/framing/framing.go +++ b/framing/framing.go @@ -240,12 +240,9 @@ func (decoder *Decoder) Decode(data []byte, frames *bytes.Buffer) (int, error) { // Remove the length field from the buffer. var obfsLen [lengthLength]byte - n, err := frames.Read(obfsLen[:]) + _, err := io.ReadFull(frames, obfsLen[:]) if err != nil { return 0, err - } else if n != lengthLength { - // Should *NEVER* happen, since at least 2 bytes exist. - panic(fmt.Sprintf("BUG: Failed to read obfuscated length: %d", n)) } // Derive the nonce the peer used. @@ -284,13 +281,9 @@ func (decoder *Decoder) Decode(data []byte, frames *bytes.Buffer) (int, error) { // Unseal the frame. var box [maxFrameLength]byte - n, err := frames.Read(box[:decoder.nextLength]) + n, err := io.ReadFull(frames, box[:decoder.nextLength]) if err != nil { return 0, err - } else if n != int(decoder.nextLength) { - // Should *NEVER* happen, since the length is checked. - panic(fmt.Sprintf("BUG: Failed to read secretbox, got %d, should have %d", - n, decoder.nextLength)) } out, ok := secretbox.Open(data[:0], box[:n], &decoder.nextNonce, &decoder.key) if !ok || decoder.nextLengthInvalid {