]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-18407: win32_urandom() uses PY_DWORD_MAX (GH-10656)
authorVictor Stinner <vstinner@redhat.com>
Thu, 22 Nov 2018 13:43:07 +0000 (14:43 +0100)
committerGitHub <noreply@github.com>
Thu, 22 Nov 2018 13:43:07 +0000 (14:43 +0100)
CryptGenRandom() maximum size is PY_DWORD_MAX, not INT_MAX.
Use DWORD type for the 'chunk' variable

Co-Authored-By: Jeremy Kloth <jeremy.kloth@gmail.com>
Python/bootstrap_hash.c

index 793c646f12ba6cb0d39556df778b6d8d16416854..eb848c8ff6e3659cf647cce3f4991f146873c7d7 100644 (file)
@@ -55,8 +55,6 @@ error:
 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 = size > INT_MAX ? INT_MAX : size;
-        if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer))
+        DWORD chunk = (DWORD)Py_MIN(size, PY_DWORD_MAX);
+        if (!CryptGenRandom(hCryptProv, chunk, buffer))
         {
             /* CryptGenRandom() failed */
             if (raise) {