summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorTal Einat <taleinat+github@gmail.com>2018-10-22 15:33:10 (GMT)
committerGitHub <noreply@github.com>2018-10-22 15:33:10 (GMT)
commit2447773573e74819e163f8963ab107bc5db123e5 (patch)
tree4db0104b09186c80bdcf9d0d7feeeb0390b89a61 /Modules
parent121eb1694cab14df857ba6abe9839654cada15cf (diff)
downloadcpython-2447773573e74819e163f8963ab107bc5db123e5.zip
cpython-2447773573e74819e163f8963ab107bc5db123e5.tar.gz
cpython-2447773573e74819e163f8963ab107bc5db123e5.tar.bz2
bpo-29843: raise AttributeError if given negative _length_ (GH-10029)
Raise ValueError OverflowError in case of a negative _length_ in a ctypes.Array subclass. Also raise TypeError instead of AttributeError for non-integer _length_. Co-authored-by: Oren Milman <orenmn@gmail.com>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_ctypes/_ctypes.c25
1 files changed, 20 insertions, 5 deletions
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 3ae6348..60f6985 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -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()) {