Fix free memoory for getLast/getFirst
authorteodor <teodor>
Fri, 18 Feb 2005 14:06:32 +0000 (14:06 +0000)
committerteodor <teodor>
Fri, 18 Feb 2005 14:06:32 +0000 (14:06 +0000)
tbtree.c

index b7bb757..e075b0a 100644 (file)
--- a/tbtree.c
+++ b/tbtree.c
@@ -1053,6 +1053,18 @@ TBTGetFirst(TBTree *db, TBTValue *key, TBTValue *value) {
 
        rc=TBTIterate(db, &iterator, key, value);
 
+       if ( key->value ) {
+               char * ptr;
+
+               ptr = tmalloc( key->length );
+               memcpy( ptr, key->value, key->length );
+               key->value = ptr;
+               ptr = tmalloc( value->length );
+               memcpy( ptr, value->value, value->length );
+               value->value = ptr; 
+       }
+
        TBTFreeIterator(db, &iterator);
 
        return rc;
@@ -1076,16 +1088,14 @@ findLast(TBTree *db, TBTValue *key, TBTValue *value, u_int32_t pagenumber) {
        page->islocked=1;
        if ( page->page.isleaf ) {
                ptr = (TBTPointer*)(page->page.data + db->pointersize * (page->page.npointer-1));
-               if ( db->keylen ) {
-                       key->length = db->keylen;
-                       key->value = ptr->key.fixed.key;
-               } else {
-                       key->length = ptr->key.varlen.length; 
-                       key->value = page->page.data + ptr->key.varlen.offset;
-               }
 
-               value->length = ptr->pointer.leaf.length; 
-               value->value = page->page.data + ptr->pointer.leaf.offset;
+               key->length = ( db->keylen ) ? db->keylen : ptr->key.varlen.length;
+               key->value = tmalloc( key->length );
+               memcpy( key->value, ( db->keylen ) ? ptr->key.fixed.key : page->page.data + ptr->key.varlen.offset, key->length ); 
+
+               value->length = ptr->pointer.leaf.length;
+               value->value = tmalloc( value->length );
+               memcpy( value->value, page->page.data + ptr->pointer.leaf.offset, value->length ); 
        } else {
                for(i=page->page.npointer-1; i>=0; i--) {
                        ptr = (TBTPointer*)( page->page.data + db->pointersize * i );
@@ -1106,6 +1116,6 @@ int
 TBTGetLast(TBTree *db, TBTValue *key, TBTValue *value) {
        memset(key, 0, sizeof(TBTValue));
        memset(value, 0, sizeof(TBTValue));
-       return findLast(db, key, value, TBTPAGEROOT);
+       return  findLast(db, key, value, TBTPAGEROOT);
 }