[Python-checkins] gh-91353: Fix void return type handling in ctypes (GH-32246)

tiran webhook-mailer at python.org
Thu Apr 14 10:27:11 EDT 2022


https://github.com/python/cpython/commit/1b035d9699aaebbe4f1ca096345d538caa07907a
commit: 1b035d9699aaebbe4f1ca096345d538caa07907a
branch: main
author: Hood Chatham <roberthoodchatham at gmail.com>
committer: tiran <christian at python.org>
date: 2022-04-14T16:27:01+02:00
summary:

gh-91353: Fix void return type handling in ctypes (GH-32246)

files:
A Misc/NEWS.d/next/Core and Builtins/2022-04-03-17-21-04.bpo-47197.Ji_c30.rst
M Modules/_ctypes/callbacks.c
M Modules/_ctypes/callproc.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-04-03-17-21-04.bpo-47197.Ji_c30.rst b/Misc/NEWS.d/next/Core and Builtins/2022-04-03-17-21-04.bpo-47197.Ji_c30.rst
new file mode 100644
index 0000000000000..697b07dc95ecc
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-04-03-17-21-04.bpo-47197.Ji_c30.rst	
@@ -0,0 +1,5 @@
+ctypes used to mishandle ``void`` return types, so that for instance a
+function declared like ``ctypes.CFUNCTYPE(None, ctypes.c_int)`` would be
+called with signature ``int f(int)`` instead of ``void f(int)``. Wasm
+targets require function pointers to be called with the correct signatures
+so this led to crashes. The problem is now fixed.
diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c
index bb59456a7321a..7dd1f99021a7f 100644
--- a/Modules/_ctypes/callbacks.c
+++ b/Modules/_ctypes/callbacks.c
@@ -399,7 +399,7 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable,
 #endif
     result = ffi_prep_cif(&p->cif, cc,
                           Py_SAFE_DOWNCAST(nargs, Py_ssize_t, int),
-                          _ctypes_get_ffi_type(restype),
+                          p->ffi_restype,
                           &p->atypes[0]);
     if (result != FFI_OK) {
         PyErr_Format(PyExc_RuntimeError,
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index 4a6b8ec3ee016..720c4c09f538f 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -1209,7 +1209,12 @@ PyObject *_ctypes_callproc(PPROC pProc,
         }
     }
 
-    rtype = _ctypes_get_ffi_type(restype);
+    if (restype == Py_None) {
+        rtype = &ffi_type_void;
+    } else {
+        rtype = _ctypes_get_ffi_type(restype);
+    }
+
     resbuf = alloca(max(rtype->size, sizeof(ffi_arg)));
 
 #ifdef _Py_MEMORY_SANITIZER



More information about the Python-checkins mailing list