Skip to content

Commit 1b035d9

Browse files
authored
pythongh-91353: Fix void return type handling in ctypes (pythonGH-32246)
1 parent 17dbb6b commit 1b035d9

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ctypes used to mishandle ``void`` return types, so that for instance a
2+
function declared like ``ctypes.CFUNCTYPE(None, ctypes.c_int)`` would be
3+
called with signature ``int f(int)`` instead of ``void f(int)``. Wasm
4+
targets require function pointers to be called with the correct signatures
5+
so this led to crashes. The problem is now fixed.

Modules/_ctypes/callbacks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable,
399399
#endif
400400
result = ffi_prep_cif(&p->cif, cc,
401401
Py_SAFE_DOWNCAST(nargs, Py_ssize_t, int),
402-
_ctypes_get_ffi_type(restype),
402+
p->ffi_restype,
403403
&p->atypes[0]);
404404
if (result != FFI_OK) {
405405
PyErr_Format(PyExc_RuntimeError,

Modules/_ctypes/callproc.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,12 @@ PyObject *_ctypes_callproc(PPROC pProc,
12091209
}
12101210
}
12111211

1212-
rtype = _ctypes_get_ffi_type(restype);
1212+
if (restype == Py_None) {
1213+
rtype = &ffi_type_void;
1214+
} else {
1215+
rtype = _ctypes_get_ffi_type(restype);
1216+
}
1217+
12131218
resbuf = alloca(max(rtype->size, sizeof(ffi_arg)));
12141219

12151220
#ifdef _Py_MEMORY_SANITIZER

0 commit comments

Comments
 (0)