mirror of
https://github.com/python/cpython.git
synced 2025-07-29 14:15:07 +00:00
Merge in release25-maint r60793:
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
73baefd7fc
commit
9d53457e59
24 changed files with 438 additions and 54 deletions
|
@ -2792,11 +2792,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;
|
||||
|
@ -2888,11 +2920,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