[Python-checkins] cpython (merge 3.5 -> 3.6): Issue #25659: Merge ctypes fix from 3.5

martin.panter python-checkins at python.org
Sun Nov 20 18:24:10 EST 2016


https://hg.python.org/cpython/rev/1253ef20c947
changeset:   105242:1253ef20c947
branch:      3.6
parent:      105241:76d1f8001e27
parent:      105240:5f061870d49c
user:        Martin Panter <vadmium+py at gmail.com>
date:        Sun Nov 20 22:07:29 2016 +0000
summary:
  Issue #25659: Merge ctypes fix from 3.5

files:
  Lib/ctypes/test/test_frombuffer.py |   8 ++++++++
  Misc/NEWS                          |   3 +++
  Modules/_ctypes/_ctypes.c          |  14 ++++++++++----
  3 files changed, 21 insertions(+), 4 deletions(-)


diff --git a/Lib/ctypes/test/test_frombuffer.py b/Lib/ctypes/test/test_frombuffer.py
--- a/Lib/ctypes/test/test_frombuffer.py
+++ b/Lib/ctypes/test/test_frombuffer.py
@@ -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 --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -40,6 +40,9 @@
 Library
 -------
 
+- Issue #25659: In ctypes, prevent a crash calling the from_buffer() and
+  from_buffer_copy() methods on abstract classes like Array.
+
 - Issue #19717: Makes Path.resolve() succeed on paths that do not exist.
   Patch by Vajrasky Kok
 
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -463,7 +463,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;
@@ -531,9 +534,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) {

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list