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

[3.10] gh-99240: Reset pointer to NULL when the pointed memory is freed in argument parsing (GH-99890) #100386

Merged
merged 1 commit into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions Lib/test/test_capi/test_getargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,10 @@ def test_Z_hash(self):
warnings.simplefilter('error', DeprecationWarning)
self.assertRaises(DeprecationWarning, getargs_Z_hash, 'abc\xe9')

def test_gh_99240_clear_args(self):
from _testcapi import gh_99240_clear_args
self.assertRaises(TypeError, gh_99240_clear_args, 'a', '\0b')


class Object_TestCase(unittest.TestCase):
def test_S(self):
Expand Down
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.
20 changes: 20 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5671,6 +5671,7 @@ test_fatal_error(PyObject *self, PyObject *args)
static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*);
static PyObject *getargs_s_hash_int2(PyObject *, PyObject *, PyObject*);
static PyObject *gh_99240_clear_args(PyObject *, PyObject *);

static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS},
Expand Down Expand Up @@ -5784,6 +5785,7 @@ static PyMethodDef TestMethods[] = {
METH_VARARGS|METH_KEYWORDS},
{"getargs_s_hash_int2", (PyCFunction)(void(*)(void))getargs_s_hash_int2,
METH_VARARGS|METH_KEYWORDS},
{"gh_99240_clear_args", gh_99240_clear_args, METH_VARARGS},
{"getargs_z", getargs_z, METH_VARARGS},
{"getargs_z_star", getargs_z_star, METH_VARARGS},
{"getargs_z_hash", getargs_z_hash, METH_VARARGS},
Expand Down Expand Up @@ -7500,3 +7502,21 @@ getargs_s_hash_int2(PyObject *self, PyObject *args, PyObject *kwargs)
PyBuffer_Release(&buf);
Py_RETURN_NONE;
}

static PyObject *
gh_99240_clear_args(PyObject *self, PyObject *args)
{
char *a = NULL;
char *b = NULL;

if (!PyArg_ParseTuple(args, "eses", "idna", &a, "idna", &b)) {
if (a || b) {
PyErr_Clear();
PyErr_SetString(PyExc_AssertionError, "Arguments are not cleared.");
}
return NULL;
}
PyMem_Free(a);
PyMem_Free(b);
Py_RETURN_NONE;
}
10 changes: 5 additions & 5 deletions Python/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ _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;
PyMem_Free(*pptr);
*pptr = NULL;
return 0;
}

Expand Down Expand Up @@ -1168,7 +1168,7 @@ _Py_COMP_DIAG_POP
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 @@ -1214,7 +1214,7 @@ _Py_COMP_DIAG_POP
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