]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-71810: Fix _PyLong_AsByteArray() undefined behavior (GH-138873) (#138884)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 14 Sep 2025 09:49:23 +0000 (11:49 +0200)
committerGitHub <noreply@github.com>
Sun, 14 Sep 2025 09:49:23 +0000 (09:49 +0000)
gh-71810: Fix _PyLong_AsByteArray() undefined behavior (GH-138873)

Don't read p[-1] when p is an empty string: when n==0.
(cherry picked from commit 8b5ce31c2b44d9bf82e6119e90a52dd530bfd1db)

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

index 98bf50d01cc07921c0dad96d95bb08b39d00ed4d..23f50c7ed28abb606845d807b1323fa8b455abdd 100644 (file)
@@ -1046,8 +1046,14 @@ _PyLong_AsByteArray(PyLongObject* v,
            just above didn't get to ensure there's a sign bit, and the
            loop below wouldn't add one either.  Make sure a sign bit
            exists. */
-        unsigned char msb = *(p - pincr);
-        int sign_bit_set = msb >= 0x80;
+        int sign_bit_set;
+        if (n > 0) {
+            unsigned char msb = *(p - pincr);
+            sign_bit_set = msb >= 0x80;
+        }
+        else {
+            sign_bit_set = 0;
+        }
         assert(accumbits == 0);
         if (sign_bit_set == do_twos_comp)
             return 0;