Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed TypeError when a Structure's field's size is >65535 bytes
Copy link
Member

@ZeroIntensity ZeroIntensity Nov 18, 2024

Choose a reason for hiding this comment

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

Suggested change
Fixed TypeError when a Structure's field's size is >65535 bytes
Fix :exc:`TypeError` when a :class:`ctypes.Structure` has a field size that doesn't fit into an unsigned 16-bit integer.

17 changes: 15 additions & 2 deletions Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,21 @@
goto error;
}

Py_ssize_t bit_size = NUM_BITS(size);
if (bit_size) {
if (bit_size_obj != Py_None) {

Copy link
Member

Choose a reason for hiding this comment

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

No need for a newline

Suggested change

Py_ssize_t bit_size;

Check warning on line 115 in Modules/_ctypes/cfield.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

variable ‘bit_size’ set but not used [-Wunused-but-set-variable]
Copy link
Member

Choose a reason for hiding this comment

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

Lint is complaining about this. I'm assuming this just needs to get moved to before the if


if (PyLong_Check(bit_size_obj)) {
bit_size = PyLong_AsSsize_t(bit_size_obj);
Copy link
Member

Choose a reason for hiding this comment

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

PyLong_AsSsize_t can fail, you need to check for < 0 and then goto error.

} else {
PyErr_Format(
PyExc_ValueError,
"bit size of field %R must be an integer size for bit fields",
self->name
);
goto error;
}

assert(bit_size > 0);
assert(bit_size <= info->size * 8);
switch(info->ffi_type_pointer.type) {
Expand Down
Loading