Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix copy of structures when passed by value.
  • Loading branch information
zooba committed Sep 8, 2017
commit ea8a0dcea68409d37f3ad9e60e42777c76ad903b
31 changes: 21 additions & 10 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,24 +399,35 @@ static PyCArgObject *
StructUnionType_paramfunc(CDataObject *self)
{
PyCArgObject *parg;
CDataObject *copied_self;
StgDictObject *stgdict;

if (self->b_size > sizeof(void*)) {
void *new_ptr = PyMem_Malloc(self->b_size);
if (new_ptr == NULL)
return NULL;
memcpy(new_ptr, self->b_ptr, self->b_size);
copied_self = (CDataObject *)PyCData_AtAddress(
(PyObject *)Py_TYPE(self), new_ptr);
copied_self->b_needsfree = 1;
} else {
copied_self = self;
Py_INCREF(copied_self);
}

parg = PyCArgObject_new();
if (parg == NULL)
if (parg == NULL) {
Py_DECREF(copied_self);
return NULL;
}

parg->tag = 'V';
stgdict = PyObject_stgdict((PyObject *)self);
stgdict = PyObject_stgdict((PyObject *)copied_self);
assert(stgdict); /* Cannot be NULL for structure/union instances */
parg->pffi_type = &stgdict->ffi_type_pointer;
/* For structure parameters (by value), parg->value doesn't contain the structure
data itself, instead parg->value.p *points* to the structure's data
See also _ctypes.c, function _call_function_pointer().
*/
parg->value.p = self->b_ptr;
parg->size = self->b_size;
Py_INCREF(self);
parg->obj = (PyObject *)self;
parg->value.p = copied_self->b_ptr;
parg->size = copied_self->b_size;
parg->obj = (PyObject *)copied_self;
return parg;
}

Expand Down