Skip to content

gh-156933: Widen narrow integer results in ctypes callbacks - #157045

Open
lazerg wants to merge 1 commit into
python:mainfrom
lazerg:fix/issue-156933-ctypes-callback-widen
Open

gh-156933: Widen narrow integer results in ctypes callbacks#157045
lazerg wants to merge 1 commit into
python:mainfrom
lazerg:fix/issue-156933-ctypes-callback-widen

Conversation

@lazerg

@lazerg lazerg commented Sep 6, 2026

Copy link
Copy Markdown

_CallPythonObject() only wrote restype->size bytes into the closure's result buffer, leaving the unused high-order bits of the ffi_arg-sized register untouched. libffi's ffi_prep_closure_loc() documents that integral types narrower than a machine register must be widened to fill it, sign-extending signed types. On architectures that always read the full register for narrow return values (s390x), this leaves garbage in the high bits, which broke libclang callbacks used by cindex.py.

The fix widens narrow integer results into a register-sized buffer before writing them back, sign- or zero-extending depending on the type, replacing the old big-endian-only pointer offset that didn't actually widen anything.

Fixes #156933.

@python-cla-bot

python-cla-bot Bot commented Sep 6, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@lazerg

lazerg commented Sep 6, 2026

Copy link
Copy Markdown
Author

This failure is a 10 minute timeout in test_subprocess's test_check on the macOS Intel runner, unrelated to this change (which only touches _ctypes/callbacks.c).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_ctypes fails to extend the return value per libffi contract

1 participant