crypto/rand: properly handle large Read on windows

Use the batched reader to chunk large Read calls on windows to a max of
1 << 31 - 1 bytes. This prevents an infinite loop when trying to read
more than 1 << 32 -1 bytes, due to how RtlGenRandom works.

This change moves the batched function from rand_unix.go to rand.go,
since it is now needed for both windows and unix implementations.

Fixes #52561

Change-Id: Id98fc4b1427e5cb2132762a445b2aed646a37473
Run-TryBot: Roland Shoemaker <>
Reviewed-by: Filippo Valsorda <>
Reviewed-by: Filippo Valsorda <>
TryBot-Result: Gopher Robot <>
4 files changed
tree: d7adc9404c0ccab25c7f3683e5dbe045400522c8
  1. .github/
  2. api/
  3. doc/
  4. lib/
  5. misc/
  6. src/
  7. test/
  8. .gitattributes
  9. .gitignore
  11. codereview.cfg

The Go Programming Language

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

Gopher image Gopher image by Renee French, licensed under Creative Commons 3.0 Attributions license.

Our canonical Git repository is located at There is a mirror of the repository at

Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file.

Download and Install

Binary Distributions

Official binary distributions are available at

After downloading a binary release, visit for installation instructions.

Install From Source

If a binary distribution is not available for your combination of operating system and architecture, visit for source installation instructions.


Go is the work of thousands of contributors. We appreciate your help!

To contribute, please read the contribution guidelines at

Note that the Go project uses the issue tracker for bug reports and proposals only. See for a list of places to ask questions about the Go language.