From 9271e851ef23f9288bdbbcf578dbc723ebae7dd5 Mon Sep 17 00:00:00 2001 From: colorfulappl Date: Wed, 30 Nov 2022 16:42:56 +0800 Subject: [PATCH 1/8] Reset pointer to NULL when memory is freed --- Python/getargs.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Python/getargs.c b/Python/getargs.c index 0167dd753d88f7..9b02e111c896a2 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -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) { + PyMem_Free(*pptr); + *pptr = NULL; } return 0; } @@ -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)", @@ -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); From eba62a3d8c21fa2699b02b72bdbef97c49fb1c6b Mon Sep 17 00:00:00 2001 From: colorfulappl Date: Wed, 30 Nov 2022 16:43:33 +0800 Subject: [PATCH 2/8] Add news --- .../next/C API/2022-11-30-16-39-22.gh-issue-99240.67nAX-.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/C API/2022-11-30-16-39-22.gh-issue-99240.67nAX-.rst diff --git a/Misc/NEWS.d/next/C API/2022-11-30-16-39-22.gh-issue-99240.67nAX-.rst b/Misc/NEWS.d/next/C API/2022-11-30-16-39-22.gh-issue-99240.67nAX-.rst new file mode 100644 index 00000000000000..9a1bb3cf5a73f0 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2022-11-30-16-39-22.gh-issue-99240.67nAX-.rst @@ -0,0 +1,2 @@ +In argument parsing, after deallocating newly allocated memory, reset its +pointer to NULL. From 39895eeb7fc7fb0d67ee696e9e2379ce6577a134 Mon Sep 17 00:00:00 2001 From: colorfulappl Date: Wed, 14 Dec 2022 10:21:59 +0800 Subject: [PATCH 3/8] Remove unnecessary null-check --- Python/getargs.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Python/getargs.c b/Python/getargs.c index 9b02e111c896a2..66dd90877fe6ff 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -203,10 +203,8 @@ static int cleanup_ptr(PyObject *self, void *ptr) { void **pptr = (void **)ptr; - if (*pptr) { - PyMem_Free(*pptr); - *pptr = NULL; - } + PyMem_Free(*pptr); + *pptr = NULL; return 0; } From ef866b2e1531135b709f162bf5dae6633e3990f6 Mon Sep 17 00:00:00 2001 From: colorfulappl Date: Thu, 15 Dec 2022 17:01:02 +0800 Subject: [PATCH 4/8] Add test --- Lib/test/test_capi/test_getargs.py | 4 ++++ Modules/_testcapi/getargs.c | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Lib/test/test_capi/test_getargs.py b/Lib/test/test_capi/test_getargs.py index 1d5c7fbaa1510a..3792d1a6515b44 100644 --- a/Lib/test/test_capi/test_getargs.py +++ b/Lib/test/test_capi/test_getargs.py @@ -1085,6 +1085,10 @@ def test_Z_hash(self): with self.assertWarns(DeprecationWarning): self.assertIsNone(getargs_Z_hash(None)) + 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): diff --git a/Modules/_testcapi/getargs.c b/Modules/_testcapi/getargs.c index 25a8e5fe8b605b..d5374a5034ae3b 100644 --- a/Modules/_testcapi/getargs.c +++ b/Modules/_testcapi/getargs.c @@ -854,6 +854,23 @@ getargs_s_hash_int2(PyObject *self, PyObject *args, PyObject *kwargs) 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 is not cleared."); + } + return NULL; + } + Py_DECREF(a); + Py_DECREF(b); + Py_RETURN_NONE; +} + static PyMethodDef test_methods[] = { {"get_args", get_args, METH_VARARGS}, {"get_kwargs", _PyCFunction_CAST(get_kwargs), METH_VARARGS|METH_KEYWORDS}, @@ -906,6 +923,7 @@ static PyMethodDef test_methods[] = { {"test_empty_argparse", test_empty_argparse, METH_NOARGS}, {"test_k_code", test_k_code, METH_NOARGS}, {"test_s_code", test_s_code, METH_NOARGS}, + {"gh_99240_clear_args", gh_99240_clear_args, METH_VARARGS}, {NULL}, }; From 357576b66ae56e8fbadc528bd6550df8941abadd Mon Sep 17 00:00:00 2001 From: colorfulappl Date: Fri, 16 Dec 2022 10:28:47 +0800 Subject: [PATCH 5/8] Fix an error --- Modules/_testcapi/getargs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_testcapi/getargs.c b/Modules/_testcapi/getargs.c index d5374a5034ae3b..4f47467d59a87e 100644 --- a/Modules/_testcapi/getargs.c +++ b/Modules/_testcapi/getargs.c @@ -866,8 +866,8 @@ gh_99240_clear_args(PyObject *self, PyObject *args) { } return NULL; } - Py_DECREF(a); - Py_DECREF(b); + PyMem_FREE(a); + PyMem_FREE(b); Py_RETURN_NONE; } From 0424701d3dc79b480aafecae59c10b3005258bd7 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Fri, 16 Dec 2022 16:14:09 +0530 Subject: [PATCH 6/8] PyMem_FREE -> PyMem_Free --- Modules/_testcapi/getargs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_testcapi/getargs.c b/Modules/_testcapi/getargs.c index 4f47467d59a87e..b285e548141866 100644 --- a/Modules/_testcapi/getargs.c +++ b/Modules/_testcapi/getargs.c @@ -866,8 +866,8 @@ gh_99240_clear_args(PyObject *self, PyObject *args) { } return NULL; } - PyMem_FREE(a); - PyMem_FREE(b); + PyMem_Free(a); + PyMem_Free(b); Py_RETURN_NONE; } From 4a7d386170eac52e509160c613ed94c1e288a510 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Fri, 16 Dec 2022 16:26:56 +0530 Subject: [PATCH 7/8] Update Modules/_testcapi/getargs.c --- Modules/_testcapi/getargs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_testcapi/getargs.c b/Modules/_testcapi/getargs.c index b285e548141866..a3be47a1132c8a 100644 --- a/Modules/_testcapi/getargs.c +++ b/Modules/_testcapi/getargs.c @@ -862,7 +862,7 @@ gh_99240_clear_args(PyObject *self, PyObject *args) { if (!PyArg_ParseTuple(args, "eses", "idna", &a, "idna", &b)) { if (a || b) { PyErr_Clear(); - PyErr_SetString(PyExc_AssertionError, "Arguments is not cleared."); + PyErr_SetString(PyExc_AssertionError, "Arguments are not cleared."); } return NULL; } From 6e9744cde1a521d05df40f89c0b0c031523a027f Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 16 Dec 2022 23:24:08 +0100 Subject: [PATCH 8/8] Update Modules/_testcapi/getargs.c --- Modules/_testcapi/getargs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/_testcapi/getargs.c b/Modules/_testcapi/getargs.c index a3be47a1132c8a..aa201319950de7 100644 --- a/Modules/_testcapi/getargs.c +++ b/Modules/_testcapi/getargs.c @@ -855,7 +855,8 @@ getargs_s_hash_int2(PyObject *self, PyObject *args, PyObject *kwargs) } static PyObject * -gh_99240_clear_args(PyObject *self, PyObject *args) { +gh_99240_clear_args(PyObject *self, PyObject *args) +{ char *a = NULL; char *b = NULL;