summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorKristján Valur Jónsson <sweskman@gmail.com>2014-03-05 13:47:57 (GMT)
committerKristján Valur Jónsson <sweskman@gmail.com>2014-03-05 13:47:57 (GMT)
commit25dded041fe532fcb041b6e68582bf76b4968132 (patch)
tree3c70fceb3fe5ef98a17b85aa94704d20eb5a853d /Objects
parent4ca688edeb07de955e1ef67c11f0e327f12ffa6e (diff)
downloadcpython-25dded041fe532fcb041b6e68582bf76b4968132.zip
cpython-25dded041fe532fcb041b6e68582bf76b4968132.tar.gz
cpython-25dded041fe532fcb041b6e68582bf76b4968132.tar.bz2
Make the various iterators' "setstate" sliently and consistently clip the
index. This avoids the possibility of setting an iterator to an invalid state.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bytearrayobject.c10
-rw-r--r--Objects/bytesobject.c10
-rw-r--r--Objects/listobject.c2
-rw-r--r--Objects/rangeobject.c31
-rw-r--r--Objects/tupleobject.c4
-rw-r--r--Objects/unicodeobject.c10
6 files changed, 52 insertions, 15 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 60b2811..50667a6 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -3043,9 +3043,13 @@ bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
Py_ssize_t index = PyLong_AsSsize_t(state);
if (index == -1 && PyErr_Occurred())
return NULL;
- if (index < 0)
- index = 0;
- it->it_index = index;
+ if (it->it_seq != NULL) {
+ if (index < 0)
+ index = 0;
+ else if (index > PyByteArray_GET_SIZE(it->it_seq))
+ index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
+ it->it_index = index;
+ }
Py_RETURN_NONE;
}
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 9dcb74e..f6d16da 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2996,9 +2996,13 @@ striter_setstate(striterobject *it, PyObject *state)
Py_ssize_t index = PyLong_AsSsize_t(state);
if (index == -1 && PyErr_Occurred())
return NULL;
- if (index < 0)
- index = 0;
- it->it_index = index;
+ if (it->it_seq != NULL) {
+ if (index < 0)
+ index = 0;
+ else if (index > PyBytes_GET_SIZE(it->it_seq))
+ index = PyBytes_GET_SIZE(it->it_seq); /* iterator exhausted */
+ it->it_index = index;
+ }
Py_RETURN_NONE;
}
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 6e0d094..143c7b3 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2803,6 +2803,8 @@ listiter_setstate(listiterobject *it, PyObject *state)
if (it->it_seq != NULL) {
if (index < 0)
index = 0;
+ else if (index > PyList_GET_SIZE(it->it_seq))
+ index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
it->it_index = index;
}
Py_RETURN_NONE;
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index 4b218b8..f2030c3 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -1000,10 +1000,11 @@ rangeiter_setstate(rangeiterobject *r, PyObject *state)
long index = PyLong_AsLong(state);
if (index == -1 && PyErr_Occurred())
return NULL;
- if (index < 0 || index > r->len) {
- PyErr_SetString(PyExc_ValueError, "index out of range");
- return NULL;
- }
+ /* silently clip the index value */
+ if (index < 0)
+ index = 0;
+ else if (index > r->len)
+ index = r->len; /* exhausted iterator */
r->index = index;
Py_RETURN_NONE;
}
@@ -1178,6 +1179,28 @@ longrangeiter_reduce(longrangeiterobject *r)
static PyObject *
longrangeiter_setstate(longrangeiterobject *r, PyObject *state)
{
+ int cmp;
+
+ /* clip the value */
+ PyObject *zero = PyLong_FromLong(0);
+ if (zero == NULL)
+ return NULL;
+ cmp = PyObject_RichCompareBool(state, zero, Py_LT);
+ if (cmp > 0) {
+ Py_CLEAR(r->index);
+ r->index = zero;
+ Py_RETURN_NONE;
+ }
+ Py_DECREF(zero);
+ if (cmp < 0)
+ return NULL;
+
+ cmp = PyObject_RichCompareBool(r->len, state, Py_LT);
+ if (cmp < 0)
+ return NULL;
+ if (cmp > 0)
+ state = r->len;
+
Py_CLEAR(r->index);
r->index = state;
Py_INCREF(r->index);
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index 123df8c..f815595 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -998,8 +998,8 @@ tupleiter_setstate(tupleiterobject *it, PyObject *state)
if (it->it_seq != NULL) {
if (index < 0)
index = 0;
- else if (it->it_seq != NULL && index > PyTuple_GET_SIZE(it->it_seq))
- index = PyTuple_GET_SIZE(it->it_seq);
+ else if (index > PyTuple_GET_SIZE(it->it_seq))
+ index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
it->it_index = index;
}
Py_RETURN_NONE;
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 0300753..4085d22 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -14420,9 +14420,13 @@ unicodeiter_setstate(unicodeiterobject *it, PyObject *state)
Py_ssize_t index = PyLong_AsSsize_t(state);
if (index == -1 && PyErr_Occurred())
return NULL;
- if (index < 0)
- index = 0;
- it->it_index = index;
+ if (it->it_seq != NULL) {
+ if (index < 0)
+ index = 0;
+ else if (index > PyUnicode_GET_LENGTH(it->it_seq))
+ index = PyUnicode_GET_LENGTH(it->it_seq); /* iterator truncated */
+ it->it_index = index;
+ }
Py_RETURN_NONE;
}