Skip to content
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

gh-99240: Reset pointer to NULL when the pointed memory is freed in argument parsing #99890

Merged
merged 9 commits into from
Dec 17, 2022
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
In argument parsing, after deallocating newly allocated memory, reset its
pointer to NULL.
10 changes: 6 additions & 4 deletions Python/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,10 @@ _PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va)
static int
cleanup_ptr(PyObject *self, void *ptr)
{
if (ptr) {
PyMem_Free(ptr);
void **pptr = (void **)ptr;
if (*pptr) {
colorfulappl marked this conversation as resolved.
Show resolved Hide resolved
PyMem_Free(*pptr);
*pptr = NULL;
}
return 0;
}
Expand Down Expand Up @@ -1116,7 +1118,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
PyErr_NoMemory();
RETURN_ERR_OCCURRED;
}
if (addcleanup(*buffer, freelist, cleanup_ptr)) {
if (addcleanup(buffer, freelist, cleanup_ptr)) {
Py_DECREF(s);
return converterr(
"(cleanup problem)",
Expand Down Expand Up @@ -1162,7 +1164,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
PyErr_NoMemory();
RETURN_ERR_OCCURRED;
}
if (addcleanup(*buffer, freelist, cleanup_ptr)) {
if (addcleanup(buffer, freelist, cleanup_ptr)) {
Py_DECREF(s);
return converterr("(cleanup problem)",
arg, msgbuf, bufsize);
Expand Down