From c25d9718b7d8c93737b04a10463379c5be671c60 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 22 Nov 2018 13:52:44 +0100 Subject: [PATCH 1/2] bpo-18407: win32_urandom() uses PY_DWORD_MAX CryptGenRandom() maximum size is PY_DWORD_MAX, not INT_MAX. Co-Authored-By: Jeremy Kloth --- Python/bootstrap_hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/bootstrap_hash.c b/Python/bootstrap_hash.c index 793c646f12ba6c..fda2940ab0ccc6 100644 --- a/Python/bootstrap_hash.c +++ b/Python/bootstrap_hash.c @@ -66,7 +66,7 @@ win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise) while (size > 0) { - chunk = size > INT_MAX ? INT_MAX : size; + chunk = Py_MIN(size, PY_DWORD_MAX); if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer)) { /* CryptGenRandom() failed */ From c3d2be49816f7eae8d0003ffd8ccf6c54ff21ac1 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 22 Nov 2018 14:23:26 +0100 Subject: [PATCH 2/2] Use DWORD type for chunk variable --- Python/bootstrap_hash.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Python/bootstrap_hash.c b/Python/bootstrap_hash.c index fda2940ab0ccc6..eb848c8ff6e365 100644 --- a/Python/bootstrap_hash.c +++ b/Python/bootstrap_hash.c @@ -55,8 +55,6 @@ win32_urandom_init(int raise) static int win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise) { - Py_ssize_t chunk; - if (hCryptProv == 0) { if (win32_urandom_init(raise) == -1) { @@ -66,8 +64,8 @@ win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise) while (size > 0) { - chunk = Py_MIN(size, PY_DWORD_MAX); - if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer)) + DWORD chunk = (DWORD)Py_MIN(size, PY_DWORD_MAX); + if (!CryptGenRandom(hCryptProv, chunk, buffer)) { /* CryptGenRandom() failed */ if (raise) {