]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Raise an error instead of crashing with a segfault when a NULL
authorThomas Heller <theller@ctypes.org>
Fri, 11 Jan 2008 19:34:06 +0000 (19:34 +0000)
committerThomas Heller <theller@ctypes.org>
Fri, 11 Jan 2008 19:34:06 +0000 (19:34 +0000)
function pointer is called.

Will backport to release25-maint.

Lib/ctypes/test/test_funcptr.py
Modules/_ctypes/_ctypes.c

index 7ea873f0a4df7dd7d6e77579c9b174b32934cf52..92bf89bb603334d9b25e88238f0ec55d7da969cc 100644 (file)
@@ -123,5 +123,11 @@ class CFuncPtrTestCase(unittest.TestCase):
         self.failUnlessEqual(strtok(None, "\n"), "c")
         self.failUnlessEqual(strtok(None, "\n"), None)
 
+    def test_NULL_funcptr(self):
+        tp = CFUNCTYPE(c_int)
+        func = tp() # NULL function pointer
+        # raise a ValueError when we try to call it
+        self.assertRaises(ValueError, func)
+
 if __name__ == '__main__':
     unittest.main()
index 212b8752ae8b8ba26f2588f1306f82f787e5143d..4c4720e4a5f918682c1ce62a7183366cd9671f1e 100644 (file)
@@ -3312,6 +3312,11 @@ CFuncPtr_call(CFuncPtrObject *self, PyObject *inargs, PyObject *kwds)
 
 
        pProc = *(void **)self->b_ptr;
+       if (pProc == NULL) {
+               PyErr_SetString(PyExc_ValueError,
+                               "attempt to call NULL function pointer");
+               return NULL;
+       }
 #ifdef MS_WIN32
        if (self->index) {
                /* It's a COM method */