diff --git a/Lib/ctypes/test/test_funcptr.py b/Lib/ctypes/test/test_funcptr.py index 7ea873f0a4d..92bf89bb603 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/Misc/NEWS b/Misc/NEWS index 47b3c5a9a09..1e742fb3740 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,8 @@ What's New in Python 2.5.2c1? Core and builtins ----------------- +- Prevent a segfault when a ctypes NULL function pointer is called. + - Bug #1517: Possible segfault in lookup(). - Issue #1638: %zd configure test fails on Linux. diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 73b5cbdc2e9..47fab8a00a5 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -3305,6 +3305,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 */