Skip to content

bpo-31619: Fixed integer overflow in converting huge strings to int. #3884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2024,7 +2024,7 @@ long_from_binary_base(const char **str, int base, PyLongObject **res)
const char *p = *str;
const char *start = p;
char prev = 0;
int digits = 0;
Py_ssize_t digits = 0;
int bits_per_char;
Py_ssize_t n;
PyLongObject *z;
Expand Down Expand Up @@ -2267,8 +2267,9 @@ just 1 digit at the start, so that the copying code was exercised for every
digit beyond the first.
***/
twodigits c; /* current input character */
double fsize_z;
Py_ssize_t size_z;
int digits = 0;
Py_ssize_t digits = 0;
int i;
int convwidth;
twodigits convmultmax, convmult;
Expand Down Expand Up @@ -2330,7 +2331,14 @@ digit beyond the first.
* need to initialize z->ob_digit -- no slot is read up before
* being stored into.
*/
size_z = (Py_ssize_t)(digits * log_base_BASE[base]) + 1;
fsize_z = digits * log_base_BASE[base] + 1;
if (fsize_z > MAX_LONG_DIGITS) {
/* The same exception as in _PyLong_New(). */
PyErr_SetString(PyExc_OverflowError,
"too many digits in integer");
return NULL;
}
size_z = (Py_ssize_t)fsize_z;
/* Uncomment next line to test exceedingly rare copy code */
/* size_z = 1; */
assert(size_z > 0);
Expand Down