Skip to content

Commit 54aaaad

Browse files
ambvcode-of-kpp
andauthored
[3.12] gh-46376: Return existing pointer when possible in ctypes (GH-107131) (#107487)
(cherry picked from commit 08447b5) Co-authored-by: Konstantin <[email protected]>
1 parent 04bd8c7 commit 54aaaad

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

Lib/test/test_ctypes/test_keeprefs.py

+27
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,33 @@ def test_p_cint(self):
9393
x = pointer(i)
9494
self.assertEqual(x._objects, {'1': i})
9595

96+
def test_pp_ownership(self):
97+
d = c_int(123)
98+
n = c_int(456)
99+
100+
p = pointer(d)
101+
pp = pointer(p)
102+
103+
self.assertIs(pp._objects['1'], p)
104+
self.assertIs(pp._objects['0']['1'], d)
105+
106+
pp.contents.contents = n
107+
108+
self.assertIs(pp._objects['1'], p)
109+
self.assertIs(pp._objects['0']['1'], n)
110+
111+
self.assertIs(p._objects['1'], n)
112+
self.assertEqual(len(p._objects), 1)
113+
114+
del d
115+
del p
116+
117+
self.assertIs(pp._objects['0']['1'], n)
118+
self.assertEqual(len(pp._objects), 2)
119+
120+
del n
121+
122+
self.assertEqual(len(pp._objects), 2)
96123

97124
class PointerToStructure(unittest.TestCase):
98125
def test(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prevent memory leak and use-after-free when using pointers to pointers with ctypes

Modules/_ctypes/_ctypes.c

+29
Original file line numberDiff line numberDiff line change
@@ -5122,6 +5122,8 @@ static PyObject *
51225122
Pointer_get_contents(CDataObject *self, void *closure)
51235123
{
51245124
StgDictObject *stgdict;
5125+
PyObject *keep, *ptr_probe;
5126+
CDataObject *ptr2ptr;
51255127

51265128
if (*(void **)self->b_ptr == NULL) {
51275129
PyErr_SetString(PyExc_ValueError,
@@ -5131,6 +5133,33 @@ Pointer_get_contents(CDataObject *self, void *closure)
51315133

51325134
stgdict = PyObject_stgdict((PyObject *)self);
51335135
assert(stgdict); /* Cannot be NULL for pointer instances */
5136+
5137+
keep = GetKeepedObjects(self);
5138+
if (keep != NULL) {
5139+
// check if it's a pointer to a pointer:
5140+
// pointers will have '0' key in the _objects
5141+
ptr_probe = PyDict_GetItemString(keep, "0");
5142+
5143+
if (ptr_probe != NULL) {
5144+
ptr2ptr = (CDataObject*) PyDict_GetItemString(keep, "1");
5145+
if (ptr2ptr == NULL) {
5146+
PyErr_SetString(PyExc_ValueError,
5147+
"Unexpected NULL pointer in _objects");
5148+
return NULL;
5149+
}
5150+
// don't construct a new object,
5151+
// return existing one instead to preserve refcount
5152+
assert(
5153+
*(void**) self->b_ptr == ptr2ptr->b_ptr ||
5154+
*(void**) self->b_value.c == ptr2ptr->b_ptr ||
5155+
*(void**) self->b_ptr == ptr2ptr->b_value.c ||
5156+
*(void**) self->b_value.c == ptr2ptr->b_value.c
5157+
); // double-check that we are returning the same thing
5158+
Py_INCREF(ptr2ptr);
5159+
return (PyObject *) ptr2ptr;
5160+
}
5161+
}
5162+
51345163
return PyCData_FromBaseObj(stgdict->proto,
51355164
(PyObject *)self, 0,
51365165
*(void **)self->b_ptr);

0 commit comments

Comments
 (0)