Skip to content

Commit bed1d14

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

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

Lib/test/test_capi/test_getargs.py

+4
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,10 @@ def test_Z_hash(self):
10981098
warnings.simplefilter('error', DeprecationWarning)
10991099
self.assertRaises(DeprecationWarning, getargs_Z_hash, 'abc\xe9')
11001100

1101+
def test_gh_99240_clear_args(self):
1102+
from _testcapi import gh_99240_clear_args
1103+
self.assertRaises(TypeError, gh_99240_clear_args, 'a', '\0b')
1104+
11011105

11021106
class Object_TestCase(unittest.TestCase):
11031107
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/_testcapimodule.c

+20
Original file line numberDiff line numberDiff line change
@@ -6192,6 +6192,7 @@ function_get_module(PyObject *self, PyObject *func)
61926192
static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
61936193
static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*);
61946194
static PyObject *getargs_s_hash_int2(PyObject *, PyObject *, PyObject*);
6195+
static PyObject *gh_99240_clear_args(PyObject *, PyObject *);
61956196

61966197
static PyMethodDef TestMethods[] = {
61976198
{"raise_exception", raise_exception, METH_VARARGS},
@@ -6308,6 +6309,7 @@ static PyMethodDef TestMethods[] = {
63086309
METH_VARARGS|METH_KEYWORDS},
63096310
{"getargs_s_hash_int2", _PyCFunction_CAST(getargs_s_hash_int2),
63106311
METH_VARARGS|METH_KEYWORDS},
6312+
{"gh_99240_clear_args", gh_99240_clear_args, METH_VARARGS},
63116313
{"getargs_z", getargs_z, METH_VARARGS},
63126314
{"getargs_z_star", getargs_z_star, METH_VARARGS},
63136315
{"getargs_z_hash", getargs_z_hash, METH_VARARGS},
@@ -8089,3 +8091,21 @@ getargs_s_hash_int2(PyObject *self, PyObject *args, PyObject *kwargs)
80898091
PyBuffer_Release(&buf);
80908092
Py_RETURN_NONE;
80918093
}
8094+
8095+
static PyObject *
8096+
gh_99240_clear_args(PyObject *self, PyObject *args)
8097+
{
8098+
char *a = NULL;
8099+
char *b = NULL;
8100+
8101+
if (!PyArg_ParseTuple(args, "eses", "idna", &a, "idna", &b)) {
8102+
if (a || b) {
8103+
PyErr_Clear();
8104+
PyErr_SetString(PyExc_AssertionError, "Arguments are not cleared.");
8105+
}
8106+
return NULL;
8107+
}
8108+
PyMem_Free(a);
8109+
PyMem_Free(b);
8110+
Py_RETURN_NONE;
8111+
}

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

@@ -1169,7 +1169,7 @@ _Py_COMP_DIAG_POP
11691169
PyErr_NoMemory();
11701170
RETURN_ERR_OCCURRED;
11711171
}
1172-
if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1172+
if (addcleanup(buffer, freelist, cleanup_ptr)) {
11731173
Py_DECREF(s);
11741174
return converterr(
11751175
"(cleanup problem)",
@@ -1215,7 +1215,7 @@ _Py_COMP_DIAG_POP
12151215
PyErr_NoMemory();
12161216
RETURN_ERR_OCCURRED;
12171217
}
1218-
if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1218+
if (addcleanup(buffer, freelist, cleanup_ptr)) {
12191219
Py_DECREF(s);
12201220
return converterr("(cleanup problem)",
12211221
arg, msgbuf, bufsize);

0 commit comments

Comments
 (0)