Commit 601a8019 authored by Tim Peters's avatar Tim Peters

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 ...).
parent 05545312
...@@ -302,7 +302,7 @@ PyMalloc(size_t sz) ...@@ -302,7 +302,7 @@ PyMalloc(size_t sz)
ASSERT(sz > 0, "non-positive size malloc", NULL); ASSERT(sz > 0, "non-positive size malloc", NULL);
if ((r=PyMem_Malloc(sz))) return r; if ((r = malloc(sz))) return r;
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
...@@ -315,8 +315,8 @@ PyRealloc(void *p, size_t sz) ...@@ -315,8 +315,8 @@ PyRealloc(void *p, size_t sz)
ASSERT(sz > 0, "non-positive size realloc", NULL); ASSERT(sz > 0, "non-positive size realloc", NULL);
if (p) r=PyMem_Realloc(p,sz); if (p) r = realloc(p,sz);
else r=PyMem_Malloc(sz); else r = malloc(sz);
UNLESS (r) PyErr_NoMemory(); UNLESS (r) PyErr_NoMemory();
...@@ -369,7 +369,7 @@ static char BTree_module_documentation[] = ...@@ -369,7 +369,7 @@ static char BTree_module_documentation[] =
"\n" "\n"
MASTER_ID MASTER_ID
BTREEITEMSTEMPLATE_C BTREEITEMSTEMPLATE_C
"$Id: BTreeModuleTemplate.c,v 1.34 2002/06/18 02:26:19 tim_one Exp $\n" "$Id: BTreeModuleTemplate.c,v 1.35 2002/06/18 15:58:22 tim_one Exp $\n"
BTREETEMPLATE_C BTREETEMPLATE_C
BUCKETTEMPLATE_C BUCKETTEMPLATE_C
KEYMACROS_H KEYMACROS_H
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
****************************************************************************/ ****************************************************************************/
#define BUCKETTEMPLATE_C "$Id: BucketTemplate.c,v 1.42 2002/06/17 19:03:55 tim_one Exp $\n" #define BUCKETTEMPLATE_C "$Id: BucketTemplate.c,v 1.43 2002/06/18 15:58:22 tim_one Exp $\n"
/* Use BUCKET_SEARCH to find the index at which a key belongs. /* Use BUCKET_SEARCH to find the index at which a key belongs.
* INDEX An int lvalue to hold the index i such that KEY belongs at * INDEX An int lvalue to hold the index i such that KEY belongs at
...@@ -427,21 +427,30 @@ bucket_split(Bucket *self, int index, Bucket *next) ...@@ -427,21 +427,30 @@ bucket_split(Bucket *self, int index, Bucket *next)
ASSERT(self->len > 1, "split of empty bucket", -1); 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; next->keys = PyMalloc(sizeof(KEY_TYPE) * next_size);
memcpy(next->keys, self->keys+index, sizeof(KEY_TYPE)*next_size); if (!next->keys)
return -1;
memcpy(next->keys, self->keys + index, sizeof(KEY_TYPE) * next_size);
if (self->values) if (self->values)
{ {
UNLESS (next->values=PyMalloc(sizeof(VALUE_TYPE)*next_size)) next->values = PyMalloc(sizeof(VALUE_TYPE) * next_size);
if (!next->values)
{
free(next->keys);
next->keys = NULL;
return -1; return -1;
memcpy(next->values, self->values+index, sizeof(VALUE_TYPE)*next_size); }
memcpy(next->values, self->values + index,
sizeof(VALUE_TYPE) * next_size);
} }
next->size = next_size; next->size = next_size;
next->len= next_size; next->len = next_size;
self->len=index; self->len = index;
next->next = self->next; next->next = self->next;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment