]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-132470: Prevent crash in ctypes.CField when `byte_size` is incorrect (#132475)
authordura0ok <slpmcf@gmail.com>
Tue, 22 Apr 2025 13:13:00 +0000 (20:13 +0700)
committerGitHub <noreply@github.com>
Tue, 22 Apr 2025 13:13:00 +0000 (16:13 +0300)
commit3b4b56f46dbfc0c336a1f70704f127593ec1f4ce
tree8ced9e54b13dbd8f5534fefd8510af626ad7e672
parentf663b2c56a2eecc258d2abd54ed33836d070e6f5
gh-132470: Prevent crash in ctypes.CField when `byte_size` is incorrect (#132475)

Fix: Prevent crash in ctypes.CField when byte_size does not match type size  (gh-132470)

When creating a ctypes.CField with an incorrect byte_size (e.g., using `byte_size=2` for `ctypes.c_byte`), the code would previously abort due to the failed assertion `byte_size == info->size`.

This commit replaces the assertion with a proper error handling mechanism that raises a `ValueError` when `byte_size` does not match the expected type size. This prevents the crash and provides a more informative error message to the us

Co-authored-by: sobolevn <mail@sobolevn.me>
Lib/test/test_ctypes/test_struct_fields.py
Misc/NEWS.d/next/C_API/2025-04-13-20-52-39.gh-issue-132470.UqBQjN.rst [new file with mode: 0644]
Modules/_ctypes/cfield.c