Skip to content

Commit efbb1eb

Browse files
colorfulapplkumaraditya303erlend-aasland
authored
gh-99240: Reset pointer to NULL when the pointed memory is freed in argument parsing (#99890)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
1 parent 9cdd2fa commit efbb1eb

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

Lib/test/test_capi/test_getargs.py

+4
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,10 @@ def test_Z_hash(self):
10851085
with self.assertWarns(DeprecationWarning):
10861086
self.assertIsNone(getargs_Z_hash(None))
10871087

1088+
def test_gh_99240_clear_args(self):
1089+
from _testcapi import gh_99240_clear_args
1090+
self.assertRaises(TypeError, gh_99240_clear_args, 'a', '\0b')
1091+
10881092

10891093
class Object_TestCase(unittest.TestCase):
10901094
def test_S(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In argument parsing, after deallocating newly allocated memory, reset its
2+
pointer to NULL.

Modules/_testcapi/getargs.c

+19
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,24 @@ getargs_s_hash_int2(PyObject *self, PyObject *args, PyObject *kwargs)
854854
Py_RETURN_NONE;
855855
}
856856

857+
static PyObject *
858+
gh_99240_clear_args(PyObject *self, PyObject *args)
859+
{
860+
char *a = NULL;
861+
char *b = NULL;
862+
863+
if (!PyArg_ParseTuple(args, "eses", "idna", &a, "idna", &b)) {
864+
if (a || b) {
865+
PyErr_Clear();
866+
PyErr_SetString(PyExc_AssertionError, "Arguments are not cleared.");
867+
}
868+
return NULL;
869+
}
870+
PyMem_Free(a);
871+
PyMem_Free(b);
872+
Py_RETURN_NONE;
873+
}
874+
857875
static PyMethodDef test_methods[] = {
858876
{"get_args", get_args, METH_VARARGS},
859877
{"get_kwargs", _PyCFunction_CAST(get_kwargs), METH_VARARGS|METH_KEYWORDS},
@@ -906,6 +924,7 @@ static PyMethodDef test_methods[] = {
906924
{"test_empty_argparse", test_empty_argparse, METH_NOARGS},
907925
{"test_k_code", test_k_code, METH_NOARGS},
908926
{"test_s_code", test_s_code, METH_NOARGS},
927+
{"gh_99240_clear_args", gh_99240_clear_args, METH_VARARGS},
909928
{NULL},
910929
};
911930

Python/getargs.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,9 @@ _PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va)
202202
static int
203203
cleanup_ptr(PyObject *self, void *ptr)
204204
{
205-
if (ptr) {
206-
PyMem_Free(ptr);
207-
}
205+
void **pptr = (void **)ptr;
206+
PyMem_Free(*pptr);
207+
*pptr = NULL;
208208
return 0;
209209
}
210210

@@ -1116,7 +1116,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
11161116
PyErr_NoMemory();
11171117
RETURN_ERR_OCCURRED;
11181118
}
1119-
if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1119+
if (addcleanup(buffer, freelist, cleanup_ptr)) {
11201120
Py_DECREF(s);
11211121
return converterr(
11221122
"(cleanup problem)",
@@ -1162,7 +1162,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
11621162
PyErr_NoMemory();
11631163
RETURN_ERR_OCCURRED;
11641164
}
1165-
if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1165+
if (addcleanup(buffer, freelist, cleanup_ptr)) {
11661166
Py_DECREF(s);
11671167
return converterr("(cleanup problem)",
11681168
arg, msgbuf, bufsize);

0 commit comments

Comments
 (0)