Skip to content

bpo-29843: raise AttributeError if given negative _length_ #10029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions Lib/ctypes/test/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ class Y(T):
self.assertEqual(Y()._length_, 187)

def test_bad_subclass(self):
import sys

with self.assertRaises(AttributeError):
class T(Array):
pass
Expand All @@ -174,14 +172,30 @@ class T(Array):
with self.assertRaises(AttributeError):
class T(Array):
_length_ = 13
with self.assertRaises(OverflowError):

def test_bad_length(self):
with self.assertRaises(ValueError):
class T(Array):
_type_ = c_int
_length_ = sys.maxsize * 2
with self.assertRaises(AttributeError):
_length_ = - sys.maxsize * 2
with self.assertRaises(ValueError):
class T(Array):
_type_ = c_int
_length_ = -1
with self.assertRaises(TypeError):
class T(Array):
_type_ = c_int
_length_ = 1.87
with self.assertRaises(OverflowError):
class T(Array):
_type_ = c_int
_length_ = sys.maxsize * 2

def test_zero_length(self):
# _length_ can be zero.
class T(Array):
_type_ = c_int
_length_ = 0

@unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform')
@bigmemtest(size=_2G, memuse=1, dry_run=False)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Raise :exc:`ValueError` instead of :exc:`OverflowError` in case of a negative
``_length_`` in a :class:`ctypes.Array` subclass. Also raise :exc:`TypeError`
instead of :exc:`AttributeError` for non-integer ``_length_``.
Original patch by Oren Milman.
25 changes: 20 additions & 5 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1405,13 +1405,28 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
type_attr = NULL;

length_attr = PyObject_GetAttrString((PyObject *)result, "_length_");
if (!length_attr || !PyLong_Check(length_attr)) {
PyErr_SetString(PyExc_AttributeError,
"class must define a '_length_' attribute, "
"which must be a positive integer");
Py_XDECREF(length_attr);
if (!length_attr) {
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_SetString(PyExc_AttributeError,
"class must define a '_length_' attribute");
}
goto error;
}

if (!PyLong_Check(length_attr)) {
Py_DECREF(length_attr);
PyErr_SetString(PyExc_TypeError,
"The '_length_' attribute must be an integer");
goto error;
}

if (_PyLong_Sign(length_attr) == -1) {
Py_DECREF(length_attr);
PyErr_SetString(PyExc_ValueError,
"The '_length_' attribute must not be negative");
goto error;
}

length = PyLong_AsSsize_t(length_attr);
Py_DECREF(length_attr);
if (length == -1 && PyErr_Occurred()) {
Expand Down