[Zope3-checkins] CVS: Zope3/src/zodb/btrees - BTreeItemsTemplate.c:1.3

Tim Peters tim.one@comcast.net
Fri, 31 Jan 2003 14:51:01 -0500


Update of /cvs-repository/Zope3/src/zodb/btrees
In directory cvs.zope.org:/tmp/cvs-serv13788/src/zodb/btrees

Modified Files:
	BTreeItemsTemplate.c 
Log Message:
BTreeItems_seek():  Even if i == pseudoindex on entry, bucket mutation
since the last call may have left us with an invalid index.  Raise
RuntimeError instead of segfaulting (or picking up random trash) if so.
A new (and previously segfaulting) test case is courtesy of Steve
Alexander.


=== Zope3/src/zodb/btrees/BTreeItemsTemplate.c 1.2 => 1.3 ===
--- Zope3/src/zodb/btrees/BTreeItemsTemplate.c:1.2	Wed Dec 25 09:12:16 2002
+++ Zope3/src/zodb/btrees/BTreeItemsTemplate.c	Fri Jan 31 14:50:28 2003
@@ -138,6 +138,7 @@
 {
     int delta, pseudoindex, currentoffset;
     Bucket *b, *currentbucket;
+    int error;
 
     pseudoindex = self->pseudoindex;
     currentoffset = self->currentoffset;
@@ -211,6 +212,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;