Commit 91acc68a authored by Yoni Fogel's avatar Yoni Fogel

Addresses #783

Using DB_CURRENT no longer overwrites temporary memory owned by the db handle.
Added regression test.

git-svn-id: file:///svn/tokudb@3821 c7de825b-a66e-492c-adef-691d508d4ae1
parent 62703b13
......@@ -2832,13 +2832,17 @@ static int brt_cursor_current(BRT_CURSOR cursor, int op, DBT *outkey, DBT *outva
if (brt_cursor_not_set(cursor))
return EINVAL;
if (op == DB_CURRENT) {
DBT newkey; toku_init_dbt(&newkey);
DBT newval; toku_init_dbt(&newval);
int r = ENOSYS;
DBT newkey; toku_init_dbt(&newkey); newkey.flags = DB_DBT_MALLOC;
DBT newval; toku_init_dbt(&newval); newval.flags = DB_DBT_MALLOC;
brt_search_t search; brt_search_init(&search, brt_cursor_compare_set, BRT_SEARCH_LEFT, &cursor->key, &cursor->val, cursor->brt);
int r = toku_brt_search(cursor->brt, &search, &newkey, &newval, logger);
r = toku_brt_search(cursor->brt, &search, &newkey, &newval, logger);
if (r != 0 || compare_kv_xy(cursor->brt, &cursor->key, &cursor->val, &newkey, &newval) != 0)
return DB_KEYEMPTY;
r = DB_KEYEMPTY;
dbt_cleanup(&newkey);
dbt_cleanup(&newval);
if (r!=0) return r;
}
return brt_cursor_copyout(cursor, outkey, outval);
}
......
/* -*- mode: C; c-basic-offset: 4 -*- */
#ident "Copyright (c) 2007 Tokutek Inc. All rights reserved."
/* DB_CURRENT */
#include <assert.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <db.h>
#include "test.h"
// ENVDIR is defined in the Makefile
DB_ENV *env;
DB *db;
DB_TXN* null_txn = NULL;
int main (int UU(argc), char UU(*argv[])) {
int r;
system("rm -rf " ENVDIR);
r=mkdir(ENVDIR, 0777); CKERR(r);
r=db_env_create(&env, 0); CKERR(r);
r=env->open(env, ENVDIR, DB_PRIVATE|DB_CREATE, 0777); CKERR(r);
r=db_create(&db, env, 0); CKERR(r);
r = db->open(db, null_txn, "foo.db", "main", DB_BTREE, DB_CREATE, 0666); CKERR(r);
DBC *cursor;
r = db->cursor(db, null_txn, &cursor, 0); CKERR(r);
DBT key, val;
DBT ckey, cval;
int k1 = 1, v1=7;
enum foo { blob = 1 };
int k2 = 2;
int v2 = 8;
r = db->put(db, null_txn, dbt_init(&key, &k1, sizeof(k1)), dbt_init(&val, &v1, sizeof(v1)), 0);
CKERR(r);
r = db->put(db, null_txn, dbt_init(&key, &k2, sizeof(k2)), dbt_init(&val, &v2, sizeof(v2)), 0);
CKERR(r);
r = cursor->c_get(cursor, dbt_init(&ckey, NULL, 0), dbt_init(&cval, NULL, 0), DB_LAST);
CKERR(r);
//Copies a static pointer into val.
r = db->get(db, null_txn, dbt_init(&key, &k1, sizeof(k1)), dbt_init(&val, NULL, 0), 0);
CKERR(r);
assert(val.data != &v1);
assert(*(int*)val.data == v1);
r = cursor->c_get(cursor, dbt_init(&ckey, NULL, 0), dbt_init(&cval, NULL, 0), DB_LAST);
CKERR(r);
//Does not corrupt it.
assert(val.data != &v1);
assert(*(int*)val.data == v1);
r = cursor->c_get(cursor, &ckey, &cval, DB_CURRENT);
CKERR(r);
assert(*(int*)val.data == v1); // Will bring up valgrind error.
r = cursor->c_del(cursor, 0);
CKERR(r);
assert(*(int*)val.data == v1); // Will bring up valgrind error.
r = cursor->c_close(cursor);
CKERR(r);
r=db->close(db, 0); CKERR(r);
r=env->close(env, 0); CKERR(r);
return 0;
}
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