Skip to content
Closed
Changes from 2 commits
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
3 changes: 2 additions & 1 deletion Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2367,7 +2367,8 @@ PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds)
stginfo->ffi_type_pointer = *fmt->pffi_type;
}
else {
const size_t els_size = sizeof(fmt->pffi_type->elements);
assert(fmt->pffi_type->type == FFI_TYPE_COMPLEX);
Comment on lines +2370 to +2372
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! There's one more check we can do here.

Suggested change
/* From primitive types - only complex types have the elements
struct field as non-NULL (two element array). */
assert(fmt->pffi_type->type == FFI_TYPE_COMPLEX);
/* From primitive types - only complex types have the elements struct
field as non-NULL (two element array, including final NULL). */
assert(fmt->pffi_type->type == FFI_TYPE_COMPLEX);
assert(fmt->pffi_type->elements[1] == NULL);

Copy link
Copy Markdown
Member

@sunmy2019 sunmy2019 Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's needed, how about using the same check in libffi, i.e.

a->elements != NULL && a->elements[0] != NULL && a->elements[1] == NULL

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this. Actual FFI-invariants are irrelevant for us. The only assumption from our side is that the possible type here is FFI_TYPE_COMPLEX.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it is okay without additional checks, since the behavior here is well documented.
https://github.com/libffi/libffi/blob/10056e7e6a0d40d2a21af63484b99f08898dde9e/doc/libffi.texi#L694

const size_t els_size = 2 * sizeof(ffi_type *);
stginfo->ffi_type_pointer.size = fmt->pffi_type->size;
stginfo->ffi_type_pointer.alignment = fmt->pffi_type->alignment;
stginfo->ffi_type_pointer.type = fmt->pffi_type->type;
Expand Down
Loading