Use io.ReadFull in places where it is appropriate.

merge-requests/3/head
Yawning Angel 10 years ago
parent 49d3f6e8bb
commit b579f6f4d4

@ -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

@ -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 {

Loading…
Cancel
Save