]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] Fix a compiler warning in _randommodule.c (GH-141058) (#141063)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 5 Nov 2025 19:10:32 +0000 (20:10 +0100)
committerGitHub <noreply@github.com>
Wed, 5 Nov 2025 19:10:32 +0000 (19:10 +0000)
Fix a compiler warning in _randommodule.c (GH-141058)

The test just before the cast ensures that the cast cannot overflow.

Fix the warning on 32-bit Windows:

    Modules\_randommodule.c(525,28): warning C4244: '=': conversion
    from 'uint64_t' to 'Py_ssize_t', possible loss of data
(cherry picked from commit 4ac16dd10950fad2d3e58e8b0ba5f2e621af3cc1)

Co-authored-by: Victor Stinner <vstinner@python.org>
Modules/_randommodule.c

index 2f4f388ce1161ab6e282438f251b122dafd52c70..3ffeff32c0dac1040958f5693e3ed2d4547b9dbc 100644 (file)
@@ -522,7 +522,7 @@ _random_Random_getrandbits_impl(RandomObject *self, uint64_t k)
         PyErr_NoMemory();
         return NULL;
     }
-    words = (k - 1u) / 32u + 1u;
+    words = (Py_ssize_t)((k - 1u) / 32u + 1u);
     wordarray = (uint32_t *)PyMem_Malloc(words * 4);
     if (wordarray == NULL) {
         PyErr_NoMemory();