[Python-checkins] gh-87347: Fix test_pymem_new() reference leak (#94330)

vstinner webhook-mailer at python.org
Mon Jun 27 17:46:46 EDT 2022


https://github.com/python/cpython/commit/edb10cab39748cb5cd5ba12080e410f0e0baff66
commit: edb10cab39748cb5cd5ba12080e410f0e0baff66
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-06-27T23:46:22+02:00
summary:

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().

files:
M Modules/_testcapimodule.c

diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index f8fb6eeec66f5..7d88f4f51f831 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -4189,39 +4189,39 @@ test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored))
 }
 
 static PyObject *
-test_pymem_new(PyObject *self, PyObject *Py_UNUSED(ignored))
+test_pyobject_new(PyObject *self, PyObject *Py_UNUSED(ignored))
 {
-    char *ptr;
+    PyObject *obj;
     PyTypeObject *type = &PyBaseObject_Type;
     PyTypeObject *var_type = &PyLong_Type;
 
     // PyObject_New()
-    ptr = PyObject_New(char, type);
-    if (ptr == NULL) {
+    obj = PyObject_New(PyObject, type);
+    if (obj == NULL) {
         goto alloc_failed;
     }
-    PyObject_Free(ptr);
+    Py_DECREF(obj);
 
     // PyObject_NEW()
-    ptr = PyObject_NEW(char, type);
-    if (ptr == NULL) {
+    obj = PyObject_NEW(PyObject, type);
+    if (obj == NULL) {
         goto alloc_failed;
     }
-    PyObject_Free(ptr);
+    Py_DECREF(obj);
 
     // PyObject_NewVar()
-    ptr = PyObject_NewVar(char, var_type, 3);
-    if (ptr == NULL) {
+    obj = PyObject_NewVar(PyObject, var_type, 3);
+    if (obj == NULL) {
         goto alloc_failed;
     }
-    PyObject_Free(ptr);
+    Py_DECREF(obj);
 
     // PyObject_NEW_VAR()
-    ptr = PyObject_NEW_VAR(char, var_type, 3);
-    if (ptr == NULL) {
+    obj = PyObject_NEW_VAR(PyObject, var_type, 3);
+    if (obj == NULL) {
         goto alloc_failed;
     }
-    PyObject_Free(ptr);
+    Py_DECREF(obj);
 
     Py_RETURN_NONE;
 
@@ -6326,7 +6326,7 @@ static PyMethodDef TestMethods[] = {
     {"with_tp_del",             with_tp_del,                     METH_VARARGS},
     {"create_cfunction",        create_cfunction,                METH_NOARGS},
     {"test_pymem_alloc0",       test_pymem_alloc0,               METH_NOARGS},
-    {"test_pymem_new",          test_pymem_new,                  METH_NOARGS},
+    {"test_pyobject_new",       test_pyobject_new,               METH_NOARGS},
     {"test_pymem_setrawallocators",test_pymem_setrawallocators,  METH_NOARGS},
     {"test_pymem_setallocators",test_pymem_setallocators,        METH_NOARGS},
     {"test_pyobject_setallocators",test_pyobject_setallocators,  METH_NOARGS},



More information about the Python-checkins mailing list