summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-01-25 21:32:39 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-01-25 21:32:39 (GMT)
commit512d2cc64328d06f4ff627497ab444e83e513348 (patch)
tree6d3599b5f69310a74dba4eb02c79e91899aba4a0 /Modules
parent5543e8135240cfe4f3d1021c1debe752e3cd7309 (diff)
downloadcpython-512d2cc64328d06f4ff627497ab444e83e513348.zip
cpython-512d2cc64328d06f4ff627497ab444e83e513348.tar.gz
cpython-512d2cc64328d06f4ff627497ab444e83e513348.tar.bz2
Issue #11004: Repair edge case in deque.count().
(Reviewed by Georg Brandl.) Also made similar changes to deque.reverse() though this wasn't strictly necessary (the edge case cannot occur with two pointers moving to meet in the middle). Making the change in reverse() was more a matter of future-proofing.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_collectionsmodule.c11
1 files changed, 7 insertions, 4 deletions
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index f4a2c8b..2391c0d 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -485,7 +485,8 @@ deque_reverse(dequeobject *deque, PyObject *unused)
/* Advance left block/index pair */
leftindex++;
if (leftindex == BLOCKLEN) {
- assert (leftblock->rightlink != NULL);
+ if (leftblock->rightlink == NULL)
+ break;
leftblock = leftblock->rightlink;
leftindex = 0;
}
@@ -493,7 +494,8 @@ deque_reverse(dequeobject *deque, PyObject *unused)
/* Step backwards with the right block/index pair */
rightindex--;
if (rightindex == -1) {
- assert (rightblock->leftlink != NULL);
+ if (rightblock->leftlink == NULL)
+ break;
rightblock = rightblock->leftlink;
rightindex = BLOCKLEN - 1;
}
@@ -509,7 +511,7 @@ deque_count(dequeobject *deque, PyObject *v)
{
block *leftblock = deque->leftblock;
Py_ssize_t leftindex = deque->leftindex;
- Py_ssize_t n = (deque->len);
+ Py_ssize_t n = deque->len;
Py_ssize_t i;
Py_ssize_t count = 0;
PyObject *item;
@@ -533,7 +535,8 @@ deque_count(dequeobject *deque, PyObject *v)
/* Advance left block/index pair */
leftindex++;
if (leftindex == BLOCKLEN) {
- assert (leftblock->rightlink != NULL);
+ if (leftblock->rightlink == NULL) /* can occur when i==n-1 */
+ break;
leftblock = leftblock->rightlink;
leftindex = 0;
}