mirror of
https://github.com/python/cpython.git
synced 2025-08-21 17:25:34 +00:00
Added checks for integer overflows, contributed by Google. Some are
only available if asserts are left in the code, in cases where they can't be triggered from Python code.
This commit is contained in:
parent
abcb59a1d8
commit
73c01d4101
25 changed files with 464 additions and 59 deletions
|
@ -2496,11 +2496,43 @@ filterstring(PyObject *func, PyObject *strobj)
|
|||
PyString_AS_STRING(item)[0];
|
||||
} else {
|
||||
/* do we need more space? */
|
||||
Py_ssize_t need = j + reslen + len-i-1;
|
||||
Py_ssize_t need = j;
|
||||
|
||||
/* calculate space requirements while checking for overflow */
|
||||
if (need > PY_SSIZE_T_MAX - reslen) {
|
||||
Py_DECREF(item);
|
||||
goto Fail_1;
|
||||
}
|
||||
|
||||
need += reslen;
|
||||
|
||||
if (need > PY_SSIZE_T_MAX - len) {
|
||||
Py_DECREF(item);
|
||||
goto Fail_1;
|
||||
}
|
||||
|
||||
need += len;
|
||||
|
||||
if (need <= i) {
|
||||
Py_DECREF(item);
|
||||
goto Fail_1;
|
||||
}
|
||||
|
||||
need = need - i - 1;
|
||||
|
||||
assert(need >= 0);
|
||||
assert(outlen >= 0);
|
||||
|
||||
if (need > outlen) {
|
||||
/* overallocate, to avoid reallocations */
|
||||
if (need<2*outlen)
|
||||
if (outlen > PY_SSIZE_T_MAX / 2) {
|
||||
Py_DECREF(item);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (need<2*outlen) {
|
||||
need = 2*outlen;
|
||||
}
|
||||
if (_PyString_Resize(&result, need)) {
|
||||
Py_DECREF(item);
|
||||
return NULL;
|
||||
|
@ -2592,11 +2624,31 @@ filterunicode(PyObject *func, PyObject *strobj)
|
|||
else {
|
||||
/* do we need more space? */
|
||||
Py_ssize_t need = j + reslen + len - i - 1;
|
||||
|
||||
/* check that didnt overflow */
|
||||
if ((j > PY_SSIZE_T_MAX - reslen) ||
|
||||
((j + reslen) > PY_SSIZE_T_MAX - len) ||
|
||||
((j + reslen + len) < i) ||
|
||||
((j + reslen + len - i) <= 0)) {
|
||||
Py_DECREF(item);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
assert(need >= 0);
|
||||
assert(outlen >= 0);
|
||||
|
||||
if (need > outlen) {
|
||||
/* overallocate,
|
||||
to avoid reallocations */
|
||||
if (need < 2 * outlen)
|
||||
need = 2 * outlen;
|
||||
if (need < 2 * outlen) {
|
||||
if (outlen > PY_SSIZE_T_MAX / 2) {
|
||||
Py_DECREF(item);
|
||||
return NULL;
|
||||
} else {
|
||||
need = 2 * outlen;
|
||||
}
|
||||
}
|
||||
|
||||
if (PyUnicode_Resize(
|
||||
&result, need) < 0) {
|
||||
Py_DECREF(item);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue