[Zope-Checkins] CVS: ZODB3/BTrees - BTreeItemsTemplate.c:1.17.12.2

Tim Peters tim.one@comcast.net
Wed, 18 Jun 2003 16:37:30 -0400


Update of /cvs-repository/ZODB3/BTrees
In directory cvs.zope.org:/tmp/cvs-serv24959/BTrees

Modified Files:
      Tag: ZODB3-3_1-branch
	BTreeItemsTemplate.c 
Log Message:
Backport.
BTreeItems_seek():  If a BTree mutates via deletion while it's being
iterated over, the iterator object can end up pointing into trash memory,
leading to a segfault.  Catch this case and raise an exception instead.


=== ZODB3/BTrees/BTreeItemsTemplate.c 1.17.12.1 => 1.17.12.2 ===
--- ZODB3/BTrees/BTreeItemsTemplate.c:1.17.12.1	Fri Apr 18 14:08:36 2003
+++ ZODB3/BTrees/BTreeItemsTemplate.c	Wed Jun 18 16:36:59 2003
@@ -137,6 +137,7 @@
 {
     int delta, pseudoindex, currentoffset;
     Bucket *b, *currentbucket;
+    int error;
 
     pseudoindex = self->pseudoindex;
     currentoffset = self->currentoffset;
@@ -210,6 +211,21 @@
     }
 
     assert(pseudoindex == i);
+
+    /* Alas, the user may have mutated the bucket since the last time we
+     * were called, and if they deleted stuff, we may be pointing into
+     * trash memory now.
+     */
+    PER_USE_OR_RETURN(currentbucket, -1);
+    error = currentoffset < 0 || currentoffset >= currentbucket->len;
+    PER_ALLOW_DEACTIVATION(currentbucket);
+    PER_ACCESSED(currentbucket);
+    if (error) {
+	PyErr_SetString(PyExc_RuntimeError,
+	                "the bucket being iterated changed size");
+	return -1;
+    }
+
     Py_INCREF(currentbucket);
     Py_DECREF(self->currentbucket);
     self->currentbucket = currentbucket;