[Zope-Checkins] CVS: Zope/lib/python/BTrees - BucketTemplate.c:1.51

Tim Peters tim.one@comcast.net
Fri, 11 Apr 2003 15:17:08 -0400


Update of /cvs-repository/Zope/lib/python/BTrees
In directory cvs.zope.org:/tmp/cvs-serv13428/lib/python/BTrees

Modified Files:
	BucketTemplate.c 
Log Message:
_bucket_set():  This could leave a mapping bucket in a variety of insane
states when the value passed in was of the wrong type (for example,
doing
    b[obj] = 3.7
when b is an OIBTree).  This manifested as a refcount leak in the test
suite, but could have been much worse (most likely in real life is that
a seemingly arbitrary existing key would "go missing").


=== Zope/lib/python/BTrees/BucketTemplate.c 1.50 => 1.51 ===
--- Zope/lib/python/BTrees/BucketTemplate.c:1.50	Mon Apr  7 17:43:14 2003
+++ Zope/lib/python/BTrees/BucketTemplate.c	Fri Apr 11 15:17:07 2003
@@ -299,17 +299,25 @@
 {
     int i, cmp;
     KEY_TYPE key;
+    VALUE_TYPE value;
     int result = -1;    /* until proven innocent */
     int copied = 1;
 
     COPY_KEY_FROM_ARG(key, keyarg, copied);
     UNLESS(copied) return -1;
 
+    /* Copy the value early (if needed), so that in case of error a
+     * pile of bucket mutations don't need to be undone.
+     */
+    if (v && !noval) {
+    	COPY_VALUE_FROM_ARG(value, v, copied);
+    	UNLESS(copied) return -1;
+    }
+
     PER_USE_OR_RETURN(self, -1);
 
     BUCKET_SEARCH(i, cmp, self, key, goto Done);
     if (cmp == 0) {
-        VALUE_TYPE value;
         /* The key exists, at index i. */
 
         if (v) {
@@ -397,8 +405,7 @@
     INCREF_KEY(self->keys[i]);
 
     if (! noval) {
-        COPY_VALUE_FROM_ARG(self->values[i], v, copied);
-        UNLESS(copied) return -1;
+        COPY_VALUE(self->values[i], value);
         INCREF_VALUE(self->values[i]);
     }