Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-144475: Fix use-after-free in functools.partial.__repr__()#145395

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
Fix a heap-buffer-overflow in :func:`functools.partial` ``__repr__`` where a
user-defined :meth:`~object.__repr__` on an argument could mutate the
:class:`~functools.partial` object via ``__setstate__()``, freeing the args
tuple while iteration was still in progress. The fix holds strong references
to the ``args`` tuple, ``kw`` dict, and ``fn`` callable during formatting.
32 changes: 24 additions & 8 deletionsModules/_functoolsmodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -705,26 +705,39 @@ partial_repr(PyObject *self)
arglist = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
if (arglist == NULL)
goto done;
/* Pack positional arguments */
/* Pack positional arguments.
* Hold a strong reference to pto->args across the loop, because
* a user-defined __repr__ called via %R could mutate 'pto' (e.g.
* via __setstate__), freeing the original args tuple while we're
* still iterating over it. See gh-144475. */
assert(PyTuple_Check(pto->args));
n = PyTuple_GET_SIZE(pto->args);
PyObject *args = Py_NewRef(pto->args);
n = PyTuple_GET_SIZE(args);
for (i = 0; i < n; i++) {
Py_SETREF(arglist, PyUnicode_FromFormat("%U, %R", arglist,
PyTuple_GET_ITEM(pto->args, i)));
if (arglist == NULL)
PyTuple_GET_ITEM(args, i)));
if (arglist == NULL) {
Py_DECREF(args);
goto done;
}
}
/* Pack keyword arguments */
Py_DECREF(args);
/* Pack keyword arguments.
* Similarly, hold a strong reference to pto->kw. See gh-144475. */
assert (PyDict_Check(pto->kw));
for (i = 0; PyDict_Next(pto->kw, &i, &key, &value);) {
PyObject *kw = Py_NewRef(pto->kw);
for (i = 0; PyDict_Next(kw, &i, &key, &value);) {
/* Prevent key.__str__ from deleting the value. */
Py_INCREF(value);
Py_SETREF(arglist, PyUnicode_FromFormat("%U, %S=%R", arglist,
key, value));
Py_DECREF(value);
if (arglist == NULL)
if (arglist == NULL) {
Py_DECREF(kw);
goto done;
}
}
Py_DECREF(kw);

mod = PyType_GetModuleName(Py_TYPE(pto));
if (mod == NULL) {
Expand All@@ -735,7 +748,10 @@ partial_repr(PyObject *self)
Py_DECREF(mod);
goto error;
}
result = PyUnicode_FromFormat("%S.%S(%R%U)", mod, name, pto->fn, arglist);
/* Hold a strong reference to pto->fn for the same reason as args/kw. */
PyObject *fn = Py_NewRef(pto->fn);
result = PyUnicode_FromFormat("%S.%S(%R%U)", mod, name, fn, arglist);
Py_DECREF(fn);
Py_DECREF(mod);
Py_DECREF(name);
Py_DECREF(arglist);
Expand Down
Loading

[8]ページ先頭

©2009-2026 Movatter.jp