From c682614df098700c689eabb47c6857f244e85bed Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Fri, 11 Jan 2008 19:34:06 +0000 Subject: [PATCH] Raise an error instead of crashing with a segfault when a NULL function pointer is called. Will backport to release25-maint. --- Lib/ctypes/test/test_funcptr.py | 6 ++++++ Modules/_ctypes/_ctypes.c | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/Lib/ctypes/test/test_funcptr.py b/Lib/ctypes/test/test_funcptr.py index 7ea873f0a4..92bf89bb60 100644 --- a/Lib/ctypes/test/test_funcptr.py +++ b/Lib/ctypes/test/test_funcptr.py @@ -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() diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 212b8752ae..4c4720e4a5 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -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 */ -- 2.50.1