mirror of
https://github.com/python/cpython.git
synced 2025-10-21 22:22:48 +00:00
Fix formatting memory consumption with very large padding specifications
This commit is contained in:
parent
db6c7f5c33
commit
978b9d2a27
1 changed files with 28 additions and 8 deletions
|
@ -12727,6 +12727,29 @@ formatchar(PyObject *v)
|
||||||
return (Py_UCS4) -1;
|
return (Py_UCS4) -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
repeat_accumulate(_PyAccu *acc, PyObject *obj, Py_ssize_t count)
|
||||||
|
{
|
||||||
|
int r;
|
||||||
|
assert(count > 0);
|
||||||
|
assert(PyUnicode_Check(obj));
|
||||||
|
if (count > 5) {
|
||||||
|
PyObject *repeated = unicode_repeat((PyUnicodeObject *) obj, count);
|
||||||
|
if (repeated == NULL)
|
||||||
|
return -1;
|
||||||
|
r = _PyAccu_Accumulate(acc, repeated);
|
||||||
|
Py_DECREF(repeated);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
do {
|
||||||
|
if (_PyAccu_Accumulate(acc, obj))
|
||||||
|
return -1;
|
||||||
|
} while (--count);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
PyUnicode_Format(PyObject *format, PyObject *args)
|
PyUnicode_Format(PyObject *format, PyObject *args)
|
||||||
{
|
{
|
||||||
|
@ -13145,10 +13168,9 @@ PyUnicode_Format(PyObject *format, PyObject *args)
|
||||||
}
|
}
|
||||||
if (width > len && !(flags & F_LJUST)) {
|
if (width > len && !(flags & F_LJUST)) {
|
||||||
assert(fillobj != NULL);
|
assert(fillobj != NULL);
|
||||||
do {
|
if (repeat_accumulate(&acc, fillobj, width - len))
|
||||||
if (_PyAccu_Accumulate(&acc, fillobj))
|
goto onError;
|
||||||
goto onError;
|
width = len;
|
||||||
} while (--width > len);
|
|
||||||
}
|
}
|
||||||
if (fill == ' ') {
|
if (fill == ' ') {
|
||||||
if (sign) {
|
if (sign) {
|
||||||
|
@ -13186,10 +13208,8 @@ PyUnicode_Format(PyObject *format, PyObject *args)
|
||||||
Py_DECREF(v);
|
Py_DECREF(v);
|
||||||
if (r)
|
if (r)
|
||||||
goto onError;
|
goto onError;
|
||||||
while (--width >= len) {
|
if (width > len && repeat_accumulate(&acc, blank, width - len))
|
||||||
if (_PyAccu_Accumulate(&acc, blank))
|
goto onError;
|
||||||
goto onError;
|
|
||||||
}
|
|
||||||
if (dict && (argidx < arglen) && c != '%') {
|
if (dict && (argidx < arglen) && c != '%') {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"not all arguments converted during string formatting");
|
"not all arguments converted during string formatting");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue