[pypy-commit] cffi default: Give a RuntimeError when we try to call a null function pointer, similar to how

arigo pypy.commits at gmail.com
Sat Aug 17 02:36:13 EDT 2019


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r3285:74f57a76ed53
Date: 2019-08-17 08:35 +0200
http://bitbucket.org/cffi/cffi/changeset/74f57a76ed53/

Log:	Give a RuntimeError when we try to call a null function pointer,
	similar to how we get a RuntimeError when trying to read or write
	through a null pointer.

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -2997,6 +2997,12 @@
                      cd->c_type->ct_name);
         return NULL;
     }
+    if (cd->c_data == NULL) {
+        PyErr_Format(PyExc_RuntimeError,
+                     "cannot call null pointer pointer from cdata '%s'",
+                     cd->c_type->ct_name);
+        return NULL;
+    }
     if (kwds != NULL && PyDict_Size(kwds) != 0) {
         PyErr_SetString(PyExc_TypeError,
                 "a cdata function cannot be called with keyword arguments");
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -4438,3 +4438,10 @@
         float(cast(BBool, 42))
     with pytest.raises(TypeError):
         complex(cast(BBool, 42))
+
+def test_cannot_call_null_function_pointer():
+    BInt = new_primitive_type("int")
+    BFunc = new_function_type((BInt, BInt), BInt, False)
+    f = cast(BFunc, 0)
+    with pytest.raises(RuntimeError):
+        f(40, 2)


More information about the pypy-commit mailing list