Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd08fdc

Browse files
committed
Issue python#25659: Change assert to TypeError in from_buffer/_copy()
Based on suggestion by Eryk Sun.
1 parent f75a2eb commit fd08fdc

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

Lib/ctypes/test/test_frombuffer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,13 @@ def test_fom_buffer_copy_with_offset(self):
7777
self.assertRaises(ValueError,
7878
(c_int * 1).from_buffer_copy, a, 16 * sizeof(c_int))
7979

80+
def test_abstract(self):
81+
self.assertRaises(TypeError, Array.from_buffer, bytearray(10))
82+
self.assertRaises(TypeError, Structure.from_buffer, bytearray(10))
83+
self.assertRaises(TypeError, Union.from_buffer, bytearray(10))
84+
self.assertRaises(TypeError, Array.from_buffer_copy, b"123")
85+
self.assertRaises(TypeError, Structure.from_buffer_copy, b"123")
86+
self.assertRaises(TypeError, Union.from_buffer_copy, b"123")
87+
8088
if __name__ == '__main__':
8189
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ Core and Builtins
6363
Library
6464
-------
6565

66+
- Issue #25659: In ctypes, prevent a crash calling the from_buffer() and
67+
from_buffer_copy() methods on abstract classes like Array.
68+
6669
- Issue #28563: Fixed possible DoS and arbitrary code execution when handle
6770
plural form selections in the gettext module. The expression parser now
6871
supports exact syntax supported by GNU gettext.

Modules/_ctypes/_ctypes.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,10 @@ CDataType_from_buffer(PyObject *type, PyObject *args)
501501
Py_ssize_t offset = 0;
502502
PyObject *obj, *result;
503503
StgDictObject *dict = PyType_stgdict(type);
504-
assert (dict);
504+
if (!dict) {
505+
PyErr_SetString(PyExc_TypeError, "abstract class");
506+
return NULL;
507+
}
505508

506509
if (!PyArg_ParseTuple(args,
507510
#if (PY_VERSION_HEX < 0x02050000)
@@ -557,13 +560,16 @@ CDataType_from_buffer_copy(PyObject *type, PyObject *args)
557560
Py_ssize_t offset = 0;
558561
PyObject *obj, *result;
559562
StgDictObject *dict = PyType_stgdict(type);
560-
assert (dict);
563+
if (!dict) {
564+
PyErr_SetString(PyExc_TypeError, "abstract class");
565+
return NULL;
566+
}
561567

562568
if (!PyArg_ParseTuple(args,
563569
#if (PY_VERSION_HEX < 0x02050000)
564-
"O|i:from_buffer",
570+
"O|i:from_buffer_copy",
565571
#else
566-
"O|n:from_buffer",
572+
"O|n:from_buffer_copy",
567573
#endif
568574
&obj, &offset))
569575
return NULL;

0 commit comments

Comments
 (0)