Skip to content

Commit 29ba688

Browse files
bpo-31619: Fixed integer overflow in converting huge strings to int. (#3884)
1 parent 1fb72d2 commit 29ba688

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Objects/longobject.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,7 +2024,7 @@ long_from_binary_base(const char **str, int base, PyLongObject **res)
20242024
const char *p = *str;
20252025
const char *start = p;
20262026
char prev = 0;
2027-
int digits = 0;
2027+
Py_ssize_t digits = 0;
20282028
int bits_per_char;
20292029
Py_ssize_t n;
20302030
PyLongObject *z;
@@ -2267,8 +2267,9 @@ just 1 digit at the start, so that the copying code was exercised for every
22672267
digit beyond the first.
22682268
***/
22692269
twodigits c; /* current input character */
2270+
double fsize_z;
22702271
Py_ssize_t size_z;
2271-
int digits = 0;
2272+
Py_ssize_t digits = 0;
22722273
int i;
22732274
int convwidth;
22742275
twodigits convmultmax, convmult;
@@ -2330,7 +2331,14 @@ digit beyond the first.
23302331
* need to initialize z->ob_digit -- no slot is read up before
23312332
* being stored into.
23322333
*/
2333-
size_z = (Py_ssize_t)(digits * log_base_BASE[base]) + 1;
2334+
fsize_z = digits * log_base_BASE[base] + 1;
2335+
if (fsize_z > MAX_LONG_DIGITS) {
2336+
/* The same exception as in _PyLong_New(). */
2337+
PyErr_SetString(PyExc_OverflowError,
2338+
"too many digits in integer");
2339+
return NULL;
2340+
}
2341+
size_z = (Py_ssize_t)fsize_z;
23342342
/* Uncomment next line to test exceedingly rare copy code */
23352343
/* size_z = 1; */
23362344
assert(size_z > 0);

0 commit comments

Comments
 (0)