https://github.com/python/cpython/commit/b343009377b632cd07863475e33238b592d984f5
commit: b343009377b632cd07863475e33238b592d984f5
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: vstinner <[email protected]>
date: 2025-10-07T19:38:40+02:00
summary:

[3.14] gh-71810: Fix _PyLong_AsByteArray() undefined behavior (GH-138873) 
(#138883)

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 <[email protected]>

files:
M Objects/longobject.c

diff --git a/Objects/longobject.c b/Objects/longobject.c
index cc061ffbe6b24e..ec2b8fc4182643 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -1143,8 +1143,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;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to