From: Adam Langley Date: Fri, 21 Sep 2012 19:55:05 +0000 (+1000) Subject: [release-branch.go1] crypto/rand: zero length reads shouldn't crash on Windows. X-Git-Tag: go1.0.3~32 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a580f888eb52acf22cd652b05643655fdd388529;p=gostls13.git [release-branch.go1] crypto/rand: zero length reads shouldn't crash on Windows. ««« backport a69e30463bf4 crypto/rand: zero length reads shouldn't crash on Windows. R=golang-dev, dave CC=golang-dev https://golang.org/cl/6496099 »»» --- diff --git a/src/pkg/crypto/rand/rand_test.go b/src/pkg/crypto/rand/rand_test.go index be3a5a221d..c5dbdb6cb8 100644 --- a/src/pkg/crypto/rand/rand_test.go +++ b/src/pkg/crypto/rand/rand_test.go @@ -29,3 +29,14 @@ func TestRead(t *testing.T) { t.Fatalf("Compressed %d -> %d", len(b), z.Len()) } } + +func TestReadEmpty(t *testing.T) { + n, err := Reader.Read(make([]byte, 0)) + if n != 0 || err != nil { + t.Fatalf("Read(make([]byte, 0)) = %d, %v", n, err) + } + n, err = Reader.Read(nil) + if n != 0 || err != nil { + t.Fatalf("Read(make(nil) = %d, %v", n, err) + } +} diff --git a/src/pkg/crypto/rand/rand_windows.go b/src/pkg/crypto/rand/rand_windows.go index 2b2bd4bba6..82b39b64a3 100644 --- a/src/pkg/crypto/rand/rand_windows.go +++ b/src/pkg/crypto/rand/rand_windows.go @@ -35,6 +35,10 @@ func (r *rngReader) Read(b []byte) (n int, err error) { } } r.mu.Unlock() + + if len(b) == 0 { + return 0, nil + } err = syscall.CryptGenRandom(r.prov, uint32(len(b)), &b[0]) if err != nil { return 0, os.NewSyscallError("CryptGenRandom", err)