-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
gh-144475: Fix a heap buffer overflow in partial_repr #145362
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Calling :func:`repr` on :func:`functools.partial` will no longer | ||
| use a mutated version of the arguments ``fn``, ``args``, or ``kw`` | ||
| when generating the string representation of the partial object. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -688,65 +688,73 @@ partial_repr(PyObject *self) | |
| { | ||
| partialobject *pto = partialobject_CAST(self); | ||
| PyObject *result = NULL; | ||
| PyObject *arglist; | ||
| PyObject *mod; | ||
| PyObject *name; | ||
| PyObject *fn, *args, *kw; | ||
| PyObject *arglist = NULL; | ||
| PyObject *mod = NULL; | ||
| PyObject *name = NULL; | ||
| Py_ssize_t i, n; | ||
| PyObject *key, *value; | ||
| int status; | ||
|
|
||
| status = Py_ReprEnter(self); | ||
| if (status != 0) { | ||
| if (status < 0) | ||
| if (status < 0) { | ||
| return NULL; | ||
| } | ||
| return PyUnicode_FromString("..."); | ||
| } | ||
| /* Reference arguments in case they change */ | ||
| fn = Py_NewRef(pto->fn); | ||
| args = Py_NewRef(pto->args); | ||
| kw = Py_NewRef(pto->kw); | ||
| assert(PyTuple_Check(args)); | ||
| assert(PyDict_Check(kw)); | ||
|
|
||
| arglist = Py_GetConstant(Py_CONSTANT_EMPTY_STR); | ||
| if (arglist == NULL) | ||
| if (arglist == NULL) { | ||
| goto done; | ||
| } | ||
| /* Pack positional arguments */ | ||
| assert(PyTuple_Check(pto->args)); | ||
| n = PyTuple_GET_SIZE(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) { | ||
| goto done; | ||
| } | ||
| } | ||
| /* Pack keyword arguments */ | ||
| assert (PyDict_Check(pto->kw)); | ||
| for (i = 0; PyDict_Next(pto->kw, &i, &key, &value);) { | ||
| for (i = 0; PyDict_Next(kw, &i, &key, &value);) { | ||
| /* Prevent key.__str__ from deleting the value. */ | ||
| Py_INCREF(value); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're covering all the bases: Here, Also, the iteration should have a critical section around it -- see PyDict_Next docs. But perhaps the best way to solve that would be switching to always use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think that adding a critical section here is best for a future PR. This PR is more about fixing a mutation during The one change we could make now is to replace
I agree, enforcing that |
||
| Py_SETREF(arglist, PyUnicode_FromFormat("%U, %S=%R", arglist, | ||
| key, value)); | ||
| Py_DECREF(value); | ||
| if (arglist == NULL) | ||
| if (arglist == NULL) { | ||
| goto done; | ||
| } | ||
| } | ||
|
|
||
| mod = PyType_GetModuleName(Py_TYPE(pto)); | ||
| if (mod == NULL) { | ||
| goto error; | ||
| goto done; | ||
| } | ||
|
|
||
| name = PyType_GetQualName(Py_TYPE(pto)); | ||
| if (name == NULL) { | ||
| Py_DECREF(mod); | ||
| goto error; | ||
| goto done; | ||
| } | ||
| result = PyUnicode_FromFormat("%S.%S(%R%U)", mod, name, pto->fn, arglist); | ||
| Py_DECREF(mod); | ||
| Py_DECREF(name); | ||
| Py_DECREF(arglist); | ||
|
|
||
| done: | ||
| result = PyUnicode_FromFormat("%S.%S(%R%U)", mod, name, fn, arglist); | ||
| done: | ||
| Py_XDECREF(name); | ||
| Py_XDECREF(mod); | ||
| Py_XDECREF(arglist); | ||
| Py_DECREF(fn); | ||
| Py_DECREF(args); | ||
| Py_DECREF(kw); | ||
| Py_ReprLeave(self); | ||
| return result; | ||
| error: | ||
| Py_DECREF(arglist); | ||
| Py_ReprLeave(self); | ||
| return NULL; | ||
| } | ||
|
|
||
| /* Pickle strategy: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: if you're not setting these to NULL at the start & clearing with
XDECREF, could you declare them when they're set?