Skip to content

Commit edb10ca

Browse files
authored
gh-87347: Fix test_pymem_new() reference leak (#94330)
Delete the allocated object with Py_DECREF() rather than PyObject_Free(). Rename also test_pymem_new() to test_pyobject_new().
1 parent acc6468 commit edb10ca

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

Modules/_testcapimodule.c

+15-15
Original file line numberDiff line numberDiff line change
@@ -4189,39 +4189,39 @@ test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored))
41894189
}
41904190

41914191
static PyObject *
4192-
test_pymem_new(PyObject *self, PyObject *Py_UNUSED(ignored))
4192+
test_pyobject_new(PyObject *self, PyObject *Py_UNUSED(ignored))
41934193
{
4194-
char *ptr;
4194+
PyObject *obj;
41954195
PyTypeObject *type = &PyBaseObject_Type;
41964196
PyTypeObject *var_type = &PyLong_Type;
41974197

41984198
// PyObject_New()
4199-
ptr = PyObject_New(char, type);
4200-
if (ptr == NULL) {
4199+
obj = PyObject_New(PyObject, type);
4200+
if (obj == NULL) {
42014201
goto alloc_failed;
42024202
}
4203-
PyObject_Free(ptr);
4203+
Py_DECREF(obj);
42044204

42054205
// PyObject_NEW()
4206-
ptr = PyObject_NEW(char, type);
4207-
if (ptr == NULL) {
4206+
obj = PyObject_NEW(PyObject, type);
4207+
if (obj == NULL) {
42084208
goto alloc_failed;
42094209
}
4210-
PyObject_Free(ptr);
4210+
Py_DECREF(obj);
42114211

42124212
// PyObject_NewVar()
4213-
ptr = PyObject_NewVar(char, var_type, 3);
4214-
if (ptr == NULL) {
4213+
obj = PyObject_NewVar(PyObject, var_type, 3);
4214+
if (obj == NULL) {
42154215
goto alloc_failed;
42164216
}
4217-
PyObject_Free(ptr);
4217+
Py_DECREF(obj);
42184218

42194219
// PyObject_NEW_VAR()
4220-
ptr = PyObject_NEW_VAR(char, var_type, 3);
4221-
if (ptr == NULL) {
4220+
obj = PyObject_NEW_VAR(PyObject, var_type, 3);
4221+
if (obj == NULL) {
42224222
goto alloc_failed;
42234223
}
4224-
PyObject_Free(ptr);
4224+
Py_DECREF(obj);
42254225

42264226
Py_RETURN_NONE;
42274227

@@ -6326,7 +6326,7 @@ static PyMethodDef TestMethods[] = {
63266326
{"with_tp_del", with_tp_del, METH_VARARGS},
63276327
{"create_cfunction", create_cfunction, METH_NOARGS},
63286328
{"test_pymem_alloc0", test_pymem_alloc0, METH_NOARGS},
6329-
{"test_pymem_new", test_pymem_new, METH_NOARGS},
6329+
{"test_pyobject_new", test_pyobject_new, METH_NOARGS},
63306330
{"test_pymem_setrawallocators",test_pymem_setrawallocators, METH_NOARGS},
63316331
{"test_pymem_setallocators",test_pymem_setallocators, METH_NOARGS},
63326332
{"test_pyobject_setallocators",test_pyobject_setallocators, METH_NOARGS},

0 commit comments

Comments
 (0)