Skip to content

crypto/rand: rand.Reader.Read is buffered on some systems #45217

Closed
@zored

Description

@zored

What version of Go are you using (go version)?

$ go version
go version go1.16.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/user/Library/Caches/go-build"
GOENV="/Users/user/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/user/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/user/go/"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.16.2/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.16.2/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/user/git/gitlab.corp.mail.ru/mayday/payday/worker-service/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/85/w4vtlcwn7qvccncnft_b1z1w0000gp/T/go-build752161639=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Go Playground:

package main

import (
	"crypto/rand"
	"fmt"
)

func main() {
	for i := 1; i <= 10000; i++ {
		n, err := rand.Reader.Read(make([]byte, 10))
		if n != 10 && err == nil {
			fmt.Printf("expected n=10, actial n=%d on i=%d", n, i)
			return
		}
	}
	fmt.Printf("ok")
}

What did you expect to see?

ok

What did you see instead?

expected n=10, actial n=6 on i=410

More info

Docs say nothing about this.

On Unix machine I get ok.

Seems that macOS uses some buffer limited with 4096 bytes. Is it an expected behaviour?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions