Commit c742a2df authored by Bradley C. Kuszmaul's avatar Bradley C. Kuszmaul

Fix up memory issues in exceptions. Addresses #215.

git-svn-id: file:///svn/tokudb@1311 c7de825b-a66e-492c-adef-691d508d4ae1
parent fb491655
#include <db.h>
#include <db_cxx.h>
static char*cpp_strdup (const char *s)
{
int l=strlen(s)+1;
char *r = new char[l];
strncpy(r, s, l);
return r;
}
DbException::~DbException() throw()
{
if (the_what!=0) {
......@@ -18,7 +26,7 @@ DbException::DbException(int err)
void DbException::FillTheWhat(void)
{
if (the_err!=0) {
the_what = strdup(db_strerror(the_err));
the_what = cpp_strdup(db_strerror(the_err));
}
}
......@@ -42,6 +50,27 @@ void DbException::set_env(DbEnv *new_env)
the_env = new_env;
}
// Must define a copy constructor so that the delete[] of the same the_what doesn't happen
DbException::DbException (const DbException &that)
: std::exception(),
the_what(cpp_strdup(that.the_what)),
the_err(that.the_err),
the_env(that.the_env)
{
}
DbException &DbException::operator = (const DbException &that)
{
if (this != &that) {
delete [] the_what;
the_what = cpp_strdup(that.the_what);
the_err = that.the_err;
the_env = that.the_env;
}
return (*this);
}
DbDeadlockException::DbDeadlockException (DbEnv *env)
: DbException(DB_LOCK_DEADLOCK)
{
......
#include <db_cxx.h>
#include <errno.h>
#include <assert.h>
#include <iostream>
......
#include <db_cxx.h>
#include <errno.h>
#include <assert.h>
#include <iostream>
using namespace std;
void test_dbt(void) {
u_int32_t size = 3;
u_int32_t flags = 5;
u_int32_t ulen = 7;
void* data = &size;
Dbt dbt;
dbt.set_size(size);
dbt.set_flags(flags);
dbt.set_data(data);
dbt.set_ulen(ulen);
assert(dbt.get_size() == size);
assert(dbt.get_flags() == flags);
assert(dbt.get_data() == data);
assert(dbt.get_ulen() == ulen);
}
int cmp(DB *db, const DBT *dbt1, const DBT *dbt2) {
return 0;
}
void test_db(void) {
DbEnv env(0);
env.open(NULL, DB_PRIVATE, 0666);
Db db(&env, 0);
int r;
r = db.set_bt_compare(cmp); assert(r == 0);
try {
r = db.remove("DoesNotExist.db", NULL, 0);
abort(); // must not make it here.
} catch (DbException e) {
assert(e.get_errno() == ENOENT);
}
// The db is closed.
env.close(0);
}
void test_db_env(void) {
DbEnv dbenv(0);
int r;
r = dbenv.set_data_dir("."); assert(r == 0);
r = dbenv.set_data_dir(".."); assert(r == 0);
try {
r = dbenv.set_data_dir(NULL);
abort();
} catch (DbException e) {
assert(e.get_errno() == EINVAL);
}
dbenv.set_errpfx("Prefix");
dbenv.set_errfile(stdout);
dbenv.err(0, "Hello %s!\n", "Name");
dbenv.close(0);
}
int main()
{
test_dbt();
test_db();
test_db_env();
cout << "Hello World!" << endl; cout << "Welcome to C++ Programming" << endl;
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