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>
This commit is contained in:
colorfulappl 2022-12-17 14:37:44 +08:00 committed by GitHub
parent 9cdd2fa63b
commit efbb1eb9f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 5 deletions

View file

@ -854,6 +854,24 @@ 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 are not cleared.");
}
return NULL;
}
PyMem_Free(a);
PyMem_Free(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 +924,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},
};