[Zodb-checkins] CVS: Zope/lib/python/BTrees - BTreeModuleTemplate.c:1.35 BucketTemplate.c:1.43

Tim Peters tim.one@comcast.net
Tue, 18 Jun 2002 11:58:23 -0400


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

Modified Files:
	BTreeModuleTemplate.c BucketTemplate.c 
Log Message:
bucket_split():  Don't leak the keys if memory for values can't be found.

PyMalloc(), PyRealloc():  Call malloc() and realloc() directly instead
of PyMem_Malloc() and PyMem_Realloc().  If the latter are used, then
memory must be freed via PyMem_Free(), but PyMem_Free() isn't called
anywhere -- memory is released via raw system free().  It would probably
be better to change all uses of free() instead, but that's A Project (well,
compared to this ...).


=== Zope/lib/python/BTrees/BTreeModuleTemplate.c 1.34 => 1.35 ===
   ASSERT(sz > 0, "non-positive size malloc", NULL);
 
-  if ((r=PyMem_Malloc(sz))) return r;
+  if ((r = malloc(sz))) return r;
 
   PyErr_NoMemory();
   return NULL;
@@ -315,8 +315,8 @@
 
   ASSERT(sz > 0, "non-positive size realloc", NULL);
 
-  if (p) r=PyMem_Realloc(p,sz);
-  else r=PyMem_Malloc(sz);
+  if (p) r = realloc(p,sz);
+  else r = malloc(sz);
 
   UNLESS (r) PyErr_NoMemory();
 


=== Zope/lib/python/BTrees/BucketTemplate.c 1.42 => 1.43 ===
   ASSERT(self->len > 1, "split of empty bucket", -1);
 
-  if (index < 0 || index >= self->len) index=self->len/2;
+  if (index < 0 || index >= self->len)
+    index = self->len / 2;
 
-  next_size=self->len-index;
+  next_size = self->len - index;
 
-  UNLESS (next->keys=PyMalloc(sizeof(KEY_TYPE)*next_size)) return -1;
-  memcpy(next->keys, self->keys+index, sizeof(KEY_TYPE)*next_size);
+  next->keys = PyMalloc(sizeof(KEY_TYPE) * next_size);
+  if (!next->keys)
+      return -1;
+  memcpy(next->keys, self->keys + index, sizeof(KEY_TYPE) * next_size);
   if (self->values)
     {
-      UNLESS (next->values=PyMalloc(sizeof(VALUE_TYPE)*next_size))
-        return -1;
-      memcpy(next->values, self->values+index, sizeof(VALUE_TYPE)*next_size);
+      next->values = PyMalloc(sizeof(VALUE_TYPE) * next_size);
+      if (!next->values)
+        {
+          free(next->keys);
+          next->keys = NULL;
+          return -1;
+        }
+      memcpy(next->values, self->values + index,
+             sizeof(VALUE_TYPE) * next_size);
     }
   next->size = next_size;
-  next->len= next_size;
-  self->len=index;
+  next->len = next_size;
+  self->len = index;
 
   next->next = self->next;