| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -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, | ||
| Expand All | @@ -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; | ||
| } | ||
| } | ||
|
|
||
|
Comment thread
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIf 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.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Qualitycast(void *ptr, PyObject *src, *ctype does this: 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 id currently works, in the test above: cast(pp, POINTER(POINTER(c_int16)))._objects[id(pp)] is ppso it feels like the safe solution would be to call cast() in the
Sorry, something went wrong.
All reactions
|
||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityDo you think it might be useful to call PyErr_SetString here?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI did what other code in _ctypes.c did for this usage, which wasn't to set the error string.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityOh, yes, PyObject_IsInstance will do it if there's an error
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.