Skip to content

Commit 5967c0e

Browse files
Fix OverflowError in _PyLong_FromByteArray.
1 parent 24cbd8d commit 5967c0e

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

Objects/longobject.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -971,16 +971,9 @@ _PyLong_FromByteArray(const unsigned char* bytes, size_t n,
971971
++numsignificantbytes;
972972
}
973973

974-
/* How many Python int digits do we need? We have
975-
8*numsignificantbytes bits, and each Python int digit has
976-
PyLong_SHIFT bits, so it's the ceiling of the quotient. */
977-
/* catch overflow before it happens */
978-
if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) {
979-
PyErr_SetString(PyExc_OverflowError,
980-
"byte array too long to convert to int");
981-
return NULL;
982-
}
983-
ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
974+
/* avoid integer overflow */
975+
ndigits = numsignificantbytes / PyLong_SHIFT * 8
976+
+ (numsignificantbytes % PyLong_SHIFT * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
984977
v = long_alloc(ndigits);
985978
if (v == NULL)
986979
return NULL;

0 commit comments

Comments
 (0)