Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto/rand: returns wrong number of bytes! #8362

Closed
gopherbot opened this issue Jul 12, 2014 · 2 comments
Closed

crypto/rand: returns wrong number of bytes! #8362

gopherbot opened this issue Jul 12, 2014 · 2 comments

Comments

@gopherbot
Copy link

by byron.rakitzis:

Go version 1.3 (and 1.2)

The documentation states that crypto/rand.Read() returns a byte count equal to the
length of the incoming slice whenever err==nil. This is contradicted with our code and
is reproducible on the playground:

http://play.golang.org/p/xd3W7l4Q-l

Source:

package main

import (
    "crypto/rand"
    "fmt"
)

func foo(i int) error {
    buf := make([]byte, 20)
    n, err := rand.Reader.Read(buf)
    switch {
    case err != nil:
        return fmt.Errorf("(%dth iteration) Rand reader error: %s", i, err)
    case n != len(buf):
        // err is nil!!!
        return fmt.Errorf("(%dth iteration) didn't get the bytes we requested (%d), got %d", i, len(buf), n)
    default:
        return nil
    }
}

func main() {
    for i := 0; i < 1000000; i++ {
        if err := foo(i); err != nil {
            panic(err)
        }
    }
}
@ianlancetaylor
Copy link
Contributor

Comment 1:

The guarantee is made for rand.Read.  You are calling rand.Reader.Read, which has no
such guarantee.

Status changed to Invalid.

@gopherbot
Copy link
Author

Comment 2 by byron.rakitzis:

Whoops! Thanks for the clarification.

@mikioh mikioh changed the title crypto/rand returns wrong number of bytes! crypto/rand: returns wrong number of bytes! Jan 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants