Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
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` is now safer | ||
| when the partial object's internal attributes are replaced while | ||
| the string representation is being generated. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -688,65 +688,72 @@ partial_repr(PyObject *self) | ||
| { | ||
| partialobject *pto = partialobject_CAST(self); | ||
| PyObject *result = NULL; | ||
| 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) { | ||
| return NULL; | ||
| } | ||
| return PyUnicode_FromString("..."); | ||
| } | ||
| /* Reference arguments in case they change */ | ||
| PyObject *fn = Py_NewRef(pto->fn); | ||
| PyObject *args = Py_NewRef(pto->args); | ||
| PyObject *kw = Py_NewRef(pto->kw); | ||
| assert(PyTuple_Check(args)); | ||
| assert(PyDict_Check(kw)); | ||
| arglist = Py_GetConstant(Py_CONSTANT_EMPTY_STR); | ||
| if (arglist == NULL) { | ||
| goto done; | ||
| } | ||
| /* Pack positional arguments */ | ||
| n = PyTuple_GET_SIZE(args); | ||
| for (i = 0; i < n; i++) { | ||
| Py_SETREF(arglist, PyUnicode_FromFormat("%U, %R", arglist, | ||
| PyTuple_GET_ITEM(args, i))); | ||
| if (arglist == NULL) { | ||
| goto done; | ||
| } | ||
| } | ||
| /* Pack keyword arguments */ | ||
| 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 -- seePyDict_Next docs. But perhaps the best way to solve that would be switching to always use ContributorAuthor 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 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.
OK!
The former seems worth looking into. As you said, there's a lot of
Yes. | ||
| Py_SETREF(arglist, PyUnicode_FromFormat("%U, %S=%R", arglist, | ||
| key, value)); | ||
| Py_DECREF(value); | ||
| if (arglist == NULL) { | ||
| goto done; | ||
| } | ||
| } | ||
| mod = PyType_GetModuleName(Py_TYPE(pto)); | ||
| if (mod == NULL) { | ||
| gotodone; | ||
| } | ||
| name = PyType_GetQualName(Py_TYPE(pto)); | ||
| if (name == NULL) { | ||
| goto 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; | ||
| } | ||
| /* Pickle strategy: | ||
Uh oh!
There was an error while loading.Please reload this page.