summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2016-11-20 07:58:35 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2016-11-20 07:58:35 (GMT)
commit6e723d2d11c9a154c301a231788981a9b087aec5 (patch)
tree614e436f74dcc758e47d9084b23608e453c582ba
parent395733d46bbc23d2f559eba4e5f75783f9bca6f1 (diff)
downloadcpython-6e723d2d11c9a154c301a231788981a9b087aec5.zip
cpython-6e723d2d11c9a154c301a231788981a9b087aec5.tar.gz
cpython-6e723d2d11c9a154c301a231788981a9b087aec5.tar.bz2
Issue #25659: Change assert to TypeError in from_buffer/_copy()
Based on suggestion by Eryk Sun.
-rw-r--r--Lib/ctypes/test/test_frombuffer.py8
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_ctypes/_ctypes.c12
3 files changed, 20 insertions, 3 deletions
diff --git a/Lib/ctypes/test/test_frombuffer.py b/Lib/ctypes/test/test_frombuffer.py
index 29c5a19..7ab38f1 100644
--- a/Lib/ctypes/test/test_frombuffer.py
+++ b/Lib/ctypes/test/test_frombuffer.py
@@ -120,5 +120,13 @@ class Test(unittest.TestCase):
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 --git a/Misc/NEWS b/Misc/NEWS
index 07576b5..785f361 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -121,6 +121,9 @@ Core and Builtins
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 --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 2f3495e..b32a0ce 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -469,7 +469,10 @@ CDataType_from_buffer(PyObject *type, PyObject *args)
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 @@ CDataType_from_buffer_copy(PyObject *type, PyObject *args)
Py_ssize_t offset = 0;
PyObject *result;
StgDictObject *dict = PyType_stgdict(type);
- assert (dict);
+ if (!dict) {
+ PyErr_SetString(PyExc_TypeError, "abstract class");
+ return NULL;
+ }
- if (!PyArg_ParseTuple(args, "y*|n:from_buffer", &buffer, &offset))
+ if (!PyArg_ParseTuple(args, "y*|n:from_buffer_copy", &buffer, &offset))
return NULL;
if (offset < 0) {