Skip to content

Commit a902058

Browse files
[3.13] gh-71810: Fix _PyLong_AsByteArray() undefined behavior (GH-138873) (#138884)
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 8b5ce31) Co-authored-by: Victor Stinner <[email protected]>
1 parent a5f7e02 commit a902058

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Objects/longobject.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,8 +1046,14 @@ _PyLong_AsByteArray(PyLongObject* v,
10461046
just above didn't get to ensure there's a sign bit, and the
10471047
loop below wouldn't add one either. Make sure a sign bit
10481048
exists. */
1049-
unsigned char msb = *(p - pincr);
1050-
int sign_bit_set = msb >= 0x80;
1049+
int sign_bit_set;
1050+
if (n > 0) {
1051+
unsigned char msb = *(p - pincr);
1052+
sign_bit_set = msb >= 0x80;
1053+
}
1054+
else {
1055+
sign_bit_set = 0;
1056+
}
10511057
assert(accumbits == 0);
10521058
if (sign_bit_set == do_twos_comp)
10531059
return 0;

0 commit comments

Comments
 (0)