summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-05-30 14:48:19 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-05-30 14:48:19 (GMT)
commitac5569b1fa483c50edca82bab1ab0a8a927ba86a (patch)
tree9eada65cbf98a2158fda4b3a668ad5ee5969f1c5 /Objects
parentbf2b3b72d370f866aa5b8f9077ff37e7c53de894 (diff)
parentfa494fd88384acc52cf9292d0c89e2961c8f747f (diff)
downloadcpython-ac5569b1fa483c50edca82bab1ab0a8a927ba86a.zip
cpython-ac5569b1fa483c50edca82bab1ab0a8a927ba86a.tar.gz
cpython-ac5569b1fa483c50edca82bab1ab0a8a927ba86a.tar.bz2
Issue #24115: Update uses of PyObject_IsTrue(), PyObject_Not(),
PyObject_IsInstance(), PyObject_RichCompareBool() and _PyDict_Contains() to check for and handle errors correctly.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bytearrayobject.c10
-rw-r--r--Objects/bytesobject.c33
-rw-r--r--Objects/rangeobject.c5
-rw-r--r--Objects/setobject.c17
4 files changed, 46 insertions, 19 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 28b1f68..49db367 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -1011,13 +1011,17 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op)
Py_buffer self_bytes, other_bytes;
PyObject *res;
Py_ssize_t minsize;
- int cmp;
+ int cmp, rc;
/* Bytes can be compared to anything that supports the (binary)
buffer API. Except that a comparison with Unicode is always an
error, even if the comparison is for equality. */
- if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
- PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) {
+ rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
+ if (!rc)
+ rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
+ if (rc < 0)
+ return NULL;
+ if (rc) {
if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
if (PyErr_WarnEx(PyExc_BytesWarning,
"Comparison between bytearray and string", 1))
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index c576678..6a6e930 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -1419,25 +1419,36 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
Py_ssize_t len_a, len_b;
Py_ssize_t min_len;
PyObject *result;
+ int rc;
/* Make sure both arguments are strings. */
if (!(PyBytes_Check(a) && PyBytes_Check(b))) {
if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
- if (PyObject_IsInstance((PyObject*)a,
- (PyObject*)&PyUnicode_Type) ||
- PyObject_IsInstance((PyObject*)b,
- (PyObject*)&PyUnicode_Type)) {
+ rc = PyObject_IsInstance((PyObject*)a,
+ (PyObject*)&PyUnicode_Type);
+ if (!rc)
+ rc = PyObject_IsInstance((PyObject*)b,
+ (PyObject*)&PyUnicode_Type);
+ if (rc < 0)
+ return NULL;
+ if (rc) {
if (PyErr_WarnEx(PyExc_BytesWarning,
- "Comparison between bytes and string", 1))
+ "Comparison between bytes and string", 1))
return NULL;
}
- else if (PyObject_IsInstance((PyObject*)a,
- (PyObject*)&PyLong_Type) ||
- PyObject_IsInstance((PyObject*)b,
- (PyObject*)&PyLong_Type)) {
- if (PyErr_WarnEx(PyExc_BytesWarning,
- "Comparison between bytes and int", 1))
+ else {
+ rc = PyObject_IsInstance((PyObject*)a,
+ (PyObject*)&PyLong_Type);
+ if (!rc)
+ rc = PyObject_IsInstance((PyObject*)b,
+ (PyObject*)&PyLong_Type);
+ if (rc < 0)
return NULL;
+ if (rc) {
+ if (PyErr_WarnEx(PyExc_BytesWarning,
+ "Comparison between bytes and int", 1))
+ return NULL;
+ }
}
}
result = Py_NotImplemented;
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index 17fef42..da1d703 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -194,8 +194,11 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
}
/* if (lo >= hi), return length of 0. */
- if (PyObject_RichCompareBool(lo, hi, Py_GE) == 1) {
+ cmp_result = PyObject_RichCompareBool(lo, hi, Py_GE);
+ if (cmp_result != 0) {
Py_XDECREF(step);
+ if (cmp_result < 0)
+ return NULL;
return PyLong_FromLong(0);
}
diff --git a/Objects/setobject.c b/Objects/setobject.c
index 1805deb..d962c1e 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -1519,9 +1519,15 @@ set_difference(PySetObject *so, PyObject *other)
if (PyDict_CheckExact(other)) {
while (set_next(so, &pos, &entry)) {
setentry entrycopy;
+ int rv;
entrycopy.hash = entry->hash;
entrycopy.key = entry->key;
- if (!_PyDict_Contains(other, entry->key, entry->hash)) {
+ rv = _PyDict_Contains(other, entry->key, entry->hash);
+ if (rv < 0) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ if (!rv) {
if (set_add_entry((PySetObject *)result, &entrycopy)) {
Py_DECREF(result);
return NULL;
@@ -1757,7 +1763,8 @@ PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
static PyObject *
set_richcompare(PySetObject *v, PyObject *w, int op)
{
- PyObject *r1, *r2;
+ PyObject *r1;
+ int r2;
if(!PyAnySet_Check(w))
Py_RETURN_NOTIMPLEMENTED;
@@ -1775,9 +1782,11 @@ set_richcompare(PySetObject *v, PyObject *w, int op)
r1 = set_richcompare(v, w, Py_EQ);
if (r1 == NULL)
return NULL;
- r2 = PyBool_FromLong(PyObject_Not(r1));
+ r2 = PyObject_IsTrue(r1);
Py_DECREF(r1);
- return r2;
+ if (r2 < 0)
+ return NULL;
+ return PyBool_FromLong(!r2);
case Py_LE:
return set_issubset(v, w);
case Py_GE: