1
0
mirror of https://github.com/golang/go synced 2024-11-14 17:20:21 -07:00

internal/syscall/unix: allow calling getrandom(..., 0, ...)

Calling getrandom() with a zero length is actually valid and useful:

- Calling getrandom(..., 0, 0) will block until the RNG is initialized.
- Calling getrandom(..., 0, GRND_NONBLOCK) will query whether the RNG
  is initialized.

So instead of short circuiting execution for these circumstances, pass
this through to the syscall.

Change-Id: I15178f087908a2d8be6c020a1ef800cc0a074742
Reviewed-on: https://go-review.googlesource.com/c/go/+/615315
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
Auto-Submit: Jason Donenfeld <Jason@zx2c4.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Jason A. Donenfeld 2024-09-24 03:06:38 +02:00 committed by Gopher Robot
parent 3d6173aa12
commit 80143607f0

View File

@ -19,14 +19,11 @@ type GetRandomFlag uintptr
// GetRandom calls the getrandom system call.
func GetRandom(p []byte, flags GetRandomFlag) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
if getrandomUnsupported.Load() {
return 0, syscall.ENOSYS
}
r1, _, errno := syscall.Syscall(getrandomTrap,
uintptr(unsafe.Pointer(&p[0])),
uintptr(unsafe.Pointer(unsafe.SliceData(p))),
uintptr(len(p)),
uintptr(flags))
if errno != 0 {