[Python-checkins] r59925 - in python/trunk: Lib/ctypes/test/test_funcptr.py Modules/_ctypes/_ctypes.c

thomas.heller python-checkins at python.org
Fri Jan 11 20:34:06 CET 2008


Author: thomas.heller
Date: Fri Jan 11 20:34:06 2008
New Revision: 59925

Modified:
   python/trunk/Lib/ctypes/test/test_funcptr.py
   python/trunk/Modules/_ctypes/_ctypes.c
Log:
Raise an error instead of crashing with a segfault when a NULL
function pointer is called.

Will backport to release25-maint.


Modified: python/trunk/Lib/ctypes/test/test_funcptr.py
==============================================================================
--- python/trunk/Lib/ctypes/test/test_funcptr.py	(original)
+++ python/trunk/Lib/ctypes/test/test_funcptr.py	Fri Jan 11 20:34:06 2008
@@ -123,5 +123,11 @@
         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()

Modified: python/trunk/Modules/_ctypes/_ctypes.c
==============================================================================
--- python/trunk/Modules/_ctypes/_ctypes.c	(original)
+++ python/trunk/Modules/_ctypes/_ctypes.c	Fri Jan 11 20:34:06 2008
@@ -3312,6 +3312,11 @@
 
 
 	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 */


More information about the Python-checkins mailing list