changeset: 105240:5f061870d49c branch: 3.5 user: Martin Panter date: Sun Nov 20 07:58:35 2016 +0000 files: Lib/ctypes/test/test_frombuffer.py Misc/NEWS Modules/_ctypes/_ctypes.c description: Issue #25659: Change assert to TypeError in from_buffer/_copy() Based on suggestion by Eryk Sun. diff -r 48526666321a -r 5f061870d49c Lib/ctypes/test/test_frombuffer.py --- a/Lib/ctypes/test/test_frombuffer.py Sun Nov 20 07:56:37 2016 +0000 +++ b/Lib/ctypes/test/test_frombuffer.py Sun Nov 20 07:58:35 2016 +0000 @@ -120,5 +120,13 @@ with self.assertRaises(ValueError): (c_int * 1).from_buffer_copy(a, 16 * sizeof(c_int)) + def test_abstract(self): + self.assertRaises(TypeError, Array.from_buffer, bytearray(10)) + self.assertRaises(TypeError, Structure.from_buffer, bytearray(10)) + self.assertRaises(TypeError, Union.from_buffer, bytearray(10)) + self.assertRaises(TypeError, Array.from_buffer_copy, b"123") + self.assertRaises(TypeError, Structure.from_buffer_copy, b"123") + self.assertRaises(TypeError, Union.from_buffer_copy, b"123") + if __name__ == '__main__': unittest.main() diff -r 48526666321a -r 5f061870d49c Misc/NEWS --- a/Misc/NEWS Sun Nov 20 07:56:37 2016 +0000 +++ b/Misc/NEWS Sun Nov 20 07:58:35 2016 +0000 @@ -121,6 +121,9 @@ Library ------- +- Issue #25659: In ctypes, prevent a crash calling the from_buffer() and + from_buffer_copy() methods on abstract classes like Array. + - Issue #28732: Fix crash in os.spawnv() with no elements in args - Issue #28485: Always raise ValueError for negative diff -r 48526666321a -r 5f061870d49c Modules/_ctypes/_ctypes.c --- a/Modules/_ctypes/_ctypes.c Sun Nov 20 07:56:37 2016 +0000 +++ b/Modules/_ctypes/_ctypes.c Sun Nov 20 07:58:35 2016 +0000 @@ -469,7 +469,10 @@ Py_ssize_t offset = 0; StgDictObject *dict = PyType_stgdict(type); - assert (dict); + if (!dict) { + PyErr_SetString(PyExc_TypeError, "abstract class"); + return NULL; + } if (!PyArg_ParseTuple(args, "O|n:from_buffer", &obj, &offset)) return NULL; @@ -537,9 +540,12 @@ Py_ssize_t offset = 0; PyObject *result; StgDictObject *dict = PyType_stgdict(type); - assert (dict); - - if (!PyArg_ParseTuple(args, "y*|n:from_buffer", &buffer, &offset)) + if (!dict) { + PyErr_SetString(PyExc_TypeError, "abstract class"); + return NULL; + } + + if (!PyArg_ParseTuple(args, "y*|n:from_buffer_copy", &buffer, &offset)) return NULL; if (offset < 0) {