@@ -119,14 +119,7 @@ PyCStgDict_clone(StgDictObject *dst, StgDictObject *src)
119119
120120 if (src -> ffi_type_pointer .elements == NULL )
121121 return 0 ;
122- /* src->ffi_type_pointer.elements is an */
123- /* array of pointers to ffi_type that */
124- /* contains a trailing NULL pointer. */
125- /* src->size is the size of the array */
126- /* in bytes excluding the trailing NULL */
127- /* pointer, so we have to add it in to */
128- /* to compute how many bytes to copy. */
129- size = src -> size + max (sizeof (ffi_type * ), src -> align );
122+ size = sizeof (ffi_type * ) * (src -> length + 1 );
130123 dst -> ffi_type_pointer .elements = PyMem_Malloc (size );
131124 if (dst -> ffi_type_pointer .elements == NULL ) {
132125 PyErr_NoMemory ();
380373PyCStructUnionType_update_stgdict (PyObject * type , PyObject * fields , int isStruct )
381374{
382375 StgDictObject * stgdict , * basedict ;
383- Py_ssize_t len , offset , size , align , i , base_len ;
376+ Py_ssize_t len , offset , size , align , i ;
384377 Py_ssize_t union_size , total_align , aligned_size ;
385378 Py_ssize_t field_size = 0 ;
386379 int bitofs ;
@@ -472,22 +465,19 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct
472465 union_size = 0 ;
473466 total_align = align ? align : 1 ;
474467 stgdict -> ffi_type_pointer .type = FFI_TYPE_STRUCT ;
475- /* calculate the cumulative number of elements in the base class */
476- /* so that all elements get copied to the child class */
477- base_len = size / max (sizeof (ffi_type * ), align );
478- stgdict -> ffi_type_pointer .elements = PyMem_New (ffi_type * , base_len + len + 1 );
468+ stgdict -> ffi_type_pointer .elements = PyMem_New (ffi_type * , basedict -> length + len + 1 );
479469 if (stgdict -> ffi_type_pointer .elements == NULL ) {
480470 PyErr_NoMemory ();
481471 return -1 ;
482472 }
483473 memset (stgdict -> ffi_type_pointer .elements , 0 ,
484- sizeof (ffi_type * ) * (base_len + len + 1 ));
485- if (size > 0 ) {
474+ sizeof (ffi_type * ) * (basedict -> length + len + 1 ));
475+ if (basedict -> length > 0 ) {
486476 memcpy (stgdict -> ffi_type_pointer .elements ,
487477 basedict -> ffi_type_pointer .elements ,
488- size );
478+ sizeof ( ffi_type * ) * ( basedict -> length ) );
489479 }
490- ffi_ofs = base_len ; /* index of the child class's first element */
480+ ffi_ofs = basedict -> length ;
491481 } else {
492482 offset = 0 ;
493483 size = 0 ;
@@ -705,7 +695,7 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct
705695
706696 stgdict -> size = aligned_size ;
707697 stgdict -> align = total_align ;
708- stgdict -> length = len ; /* the number of elements in the child class */
698+ stgdict -> length = ffi_ofs + len ;
709699
710700/*
711701 * The value of MAX_STRUCT_SIZE depends on the platform Python is running on.
0 commit comments