Commit fd08fdc7 authored by Martin Panter's avatar Martin Panter

Issue #25659: Change assert to TypeError in from_buffer/_copy()

Based on suggestion by Eryk Sun.
parent f75a2ebb
...@@ -77,5 +77,13 @@ class Test(unittest.TestCase): ...@@ -77,5 +77,13 @@ class Test(unittest.TestCase):
self.assertRaises(ValueError, self.assertRaises(ValueError,
(c_int * 1).from_buffer_copy, a, 16 * sizeof(c_int)) (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__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -63,6 +63,9 @@ Core and Builtins ...@@ -63,6 +63,9 @@ Core and Builtins
Library Library
------- -------
- Issue #25659: In ctypes, prevent a crash calling the from_buffer() and
from_buffer_copy() methods on abstract classes like Array.
- Issue #28563: Fixed possible DoS and arbitrary code execution when handle - Issue #28563: Fixed possible DoS and arbitrary code execution when handle
plural form selections in the gettext module. The expression parser now plural form selections in the gettext module. The expression parser now
supports exact syntax supported by GNU gettext. supports exact syntax supported by GNU gettext.
......
...@@ -501,7 +501,10 @@ CDataType_from_buffer(PyObject *type, PyObject *args) ...@@ -501,7 +501,10 @@ CDataType_from_buffer(PyObject *type, PyObject *args)
Py_ssize_t offset = 0; Py_ssize_t offset = 0;
PyObject *obj, *result; PyObject *obj, *result;
StgDictObject *dict = PyType_stgdict(type); StgDictObject *dict = PyType_stgdict(type);
assert (dict); if (!dict) {
PyErr_SetString(PyExc_TypeError, "abstract class");
return NULL;
}
if (!PyArg_ParseTuple(args, if (!PyArg_ParseTuple(args,
#if (PY_VERSION_HEX < 0x02050000) #if (PY_VERSION_HEX < 0x02050000)
...@@ -557,13 +560,16 @@ CDataType_from_buffer_copy(PyObject *type, PyObject *args) ...@@ -557,13 +560,16 @@ CDataType_from_buffer_copy(PyObject *type, PyObject *args)
Py_ssize_t offset = 0; Py_ssize_t offset = 0;
PyObject *obj, *result; PyObject *obj, *result;
StgDictObject *dict = PyType_stgdict(type); StgDictObject *dict = PyType_stgdict(type);
assert (dict); if (!dict) {
PyErr_SetString(PyExc_TypeError, "abstract class");
return NULL;
}
if (!PyArg_ParseTuple(args, if (!PyArg_ParseTuple(args,
#if (PY_VERSION_HEX < 0x02050000) #if (PY_VERSION_HEX < 0x02050000)
"O|i:from_buffer", "O|i:from_buffer_copy",
#else #else
"O|n:from_buffer", "O|n:from_buffer_copy",
#endif #endif
&obj, &offset)) &obj, &offset))
return NULL; return NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment