1
0
mirror of https://github.com/golang/go synced 2024-11-22 05:54:40 -07:00

crypto/rand: zero length reads shouldn't crash on Windows.

R=golang-dev, dave
CC=golang-dev
https://golang.org/cl/6496099
This commit is contained in:
Adam Langley 2012-09-09 20:07:09 -04:00
parent ae0862c1ec
commit b752ab2e90
2 changed files with 15 additions and 0 deletions

View File

@ -30,3 +30,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)
}
}

View File

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