Skip to content

Commit 30a6bc8

Browse files
miss-islingtonserhiy-storchaka
authored andcommitted
bpo-31619: Fixed integer overflow in converting huge strings to int. (GH-3884) (#4690)
(cherry picked from commit 29ba688)
1 parent c8f32aa commit 30a6bc8

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

‎Objects/longobject.c‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,7 @@ long_from_binary_base(const char **str, int base, PyLongObject **res)
20162016
const char *p = *str;
20172017
const char *start = p;
20182018
char prev = 0;
2019-
int digits = 0;
2019+
Py_ssize_t digits = 0;
20202020
int bits_per_char;
20212021
Py_ssize_t n;
20222022
PyLongObject *z;
@@ -2259,8 +2259,9 @@ just 1 digit at the start, so that the copying code was exercised for every
22592259
digit beyond the first.
22602260
***/
22612261
twodigits c; /* current input character */
2262+
double fsize_z;
22622263
Py_ssize_t size_z;
2263-
int digits = 0;
2264+
Py_ssize_t digits = 0;
22642265
int i;
22652266
int convwidth;
22662267
twodigits convmultmax, convmult;
@@ -2322,7 +2323,14 @@ digit beyond the first.
23222323
* need to initialize z->ob_digit -- no slot is read up before
23232324
* being stored into.
23242325
*/
2325-
size_z = (Py_ssize_t)(digits * log_base_BASE[base]) + 1;
2326+
fsize_z = digits * log_base_BASE[base] + 1;
2327+
if (fsize_z > MAX_LONG_DIGITS) {
2328+
/* The same exception as in _PyLong_New(). */
2329+
PyErr_SetString(PyExc_OverflowError,
2330+
"too many digits in integer");
2331+
return NULL;
2332+
}
2333+
size_z = (Py_ssize_t)fsize_z;
23262334
/* Uncomment next line to test exceedingly rare copy code */
23272335
/* size_z = 1; */
23282336
assert(size_z > 0);

0 commit comments

Comments
 (0)