Skip to content

Commit b343009

Browse files
[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 8b5ce31) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 52e8858 commit b343009

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
@@ -1143,8 +1143,14 @@ _PyLong_AsByteArray(PyLongObject* v,
11431143
just above didn't get to ensure there's a sign bit, and the
11441144
loop below wouldn't add one either. Make sure a sign bit
11451145
exists. */
1146-
unsigned char msb = *(p - pincr);
1147-
int sign_bit_set = msb >= 0x80;
1146+
int sign_bit_set;
1147+
if (n > 0) {
1148+
unsigned char msb = *(p - pincr);
1149+
sign_bit_set = msb >= 0x80;
1150+
}
1151+
else {
1152+
sign_bit_set = 0;
1153+
}
11481154
assert(accumbits == 0);
11491155
if (sign_bit_set == do_twos_comp)
11501156
return 0;

0 commit comments

Comments
 (0)