-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-107940: Only reuse the pointer object when it matches the _type_
of the container
#108222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5139,6 +5139,8 @@ static PyObject * | |
Pointer_get_contents(CDataObject *self, void *closure) | ||
{ | ||
StgDictObject *stgdict; | ||
PyObject *ptr2ptr; | ||
CDataObject *p2p; | ||
|
||
if (*(void **)self->b_ptr == NULL) { | ||
PyErr_SetString(PyExc_ValueError, | ||
|
@@ -5148,38 +5150,40 @@ Pointer_get_contents(CDataObject *self, void *closure) | |
|
||
stgdict = PyObject_stgdict((PyObject *)self); | ||
assert(stgdict); /* Cannot be NULL for pointer instances */ | ||
assert(stgdict->proto); | ||
|
||
PyObject *keep = GetKeepedObjects(self); | ||
if (keep != NULL) { | ||
// check if it's a pointer to a pointer: | ||
// pointers will have '0' key in the _objects | ||
int ptr_probe = PyDict_ContainsString(keep, "0"); | ||
if (ptr_probe < 0) { | ||
if (self->b_objects != NULL && PyDict_CheckExact(self->b_objects)) { | ||
// Pointer_set_contents uses KeepRef(self, 1, value); we retrieve that | ||
ptr2ptr = PyDict_GetItemString(self->b_objects, "1"); | ||
if (ptr2ptr == NULL) { | ||
PyErr_SetString(PyExc_ValueError, | ||
"Unexpected NULL pointer in _objects"); | ||
return NULL; | ||
} | ||
if (ptr_probe) { | ||
PyObject *item; | ||
if (PyDict_GetItemStringRef(keep, "1", &item) < 0) { | ||
return NULL; | ||
} | ||
if (item == NULL) { | ||
PyErr_SetString(PyExc_ValueError, | ||
"Unexpected NULL pointer in _objects"); | ||
return NULL; | ||
} | ||
#ifndef NDEBUG | ||
CDataObject *ptr2ptr = (CDataObject *)item; | ||
// Don't construct a new object, | ||
// return existing one instead to preserve refcount. | ||
// Double-check that we are returning the same thing. | ||
// if our base pointer is cast from another type, | ||
// its `_type_` proto will be incompatible with the | ||
// type of the object stored in `b_objects["1"]` because | ||
// `_objects` is shared between casts and the original. | ||
int res = PyObject_IsInstance(ptr2ptr, stgdict->proto); | ||
if (res == -1) { | ||
return NULL; | ||
} | ||
if (res) { | ||
// It's not a cast: don't construct a new object, | ||
// return existing one instead to preserve refcount | ||
p2p = (CDataObject*) ptr2ptr; | ||
printf("self->b_ptr=%lu\n", *(void**) self->b_ptr); | ||
printf("p2p->b_ptr=%lu\n", *(void**) p2p->b_ptr); | ||
printf("self->b_value.c=%lu\n", *(void**) self->b_value.c); | ||
printf("p2p->b_value.c=%lu\n", *(void**) p2p->b_value.c); | ||
assert( | ||
*(void**) self->b_ptr == ptr2ptr->b_ptr || | ||
*(void**) self->b_value.c == ptr2ptr->b_ptr || | ||
*(void**) self->b_ptr == ptr2ptr->b_value.c || | ||
*(void**) self->b_value.c == ptr2ptr->b_value.c | ||
); | ||
#endif | ||
return item; | ||
*(void**) self->b_ptr == *(void**) p2p->b_ptr || | ||
*(void**) self->b_value.c == *(void**) p2p->b_ptr || | ||
*(void**) self->b_ptr == *(void**) p2p->b_value.c || | ||
*(void**) self->b_value.c == *(void**) p2p->b_value.c | ||
); // double-check that we are returning the same thing | ||
Py_INCREF(ptr2ptr); | ||
return ptr2ptr; | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand correctly, we will fall down here if types are different, meaning that if someone will try to set contents of the contents, we'll have the same bug as we had originally - garbage in the memory. Let me check that/play with that a bit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
result = (CDataObject *)_PyObject_CallNoArgs(ctype);
index = PyLong_FromVoidPtr((void *)src);
rc = PyDict_SetItem(result->b_objects, index, src);
memcpy(result->b_ptr, &ptr, sizeof(void *));
return (PyObject *)result; e.g., given how cast(pp, POINTER(POINTER(c_int16)))._objects[id(pp)] is pp so it feels like the safe solution would be to call |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it might be useful to call
PyErr_SetString
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did what other code in
_ctypes.c
did for this usage, which wasn't to set the error string.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yes,
PyObject_IsInstance
will do it if there's an error