Skip to content

Commit c6484cb

Browse files
authored
Merge pull request #2 from matejmatuska/check-db-open-failure
Check return values when connecting to/creating a database
2 parents e1dc9f8 + 766a358 commit c6484cb

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

convert.cc

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int main(int argc, char* argv[]){
2525
{"src", required_argument, NULL, 's'},
2626
{"lmdb", no_argument, NULL, 'l'},
2727
{"help", no_argument, NULL, 'h'},
28-
{0}
28+
{0, 0, 0, 0}
2929
};
3030
int option_index = 0;
3131
while (1) {
@@ -54,18 +54,28 @@ int main(int argc, char* argv[]){
5454

5555

5656
DB_ * x = new Libdb();
57-
x->connect_database(src);
57+
if (!x->connect_database(src)) {
58+
// error is printed in the connect_database function
59+
delete x;
60+
return 1;
61+
}
5862
DB_ *b;
5963
if (lmdb)
6064
b = new LMDB_();
6165
else
6266
b = new GDBM_();
63-
b->create_database(dst);
67+
if (!b->create_database(dst)) {
68+
std::cerr<<"Failed to create destination database\n";
69+
delete x;
70+
delete b;
71+
return 1;
72+
}
6473
if (!b->fill_database(x)){
6574
std::cerr<<"database filling failed\n";
75+
delete x;
76+
delete b;
6677
return 1;
6778
}
68-
b->close_db();
6979
delete x;
7080
delete b;
7181
return 0;

convert_db.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ class DB_ {
3737
virtual bool create_database(std::string name);
3838
bool create_db();
3939
virtual bool fill_database(DB_ * old_database);
40-
virtual void close_db();
40+
virtual void close_db() = 0;
4141
virtual DBC * get_database();
42+
virtual ~DB_();
4243
};
4344
/*
4445
* Libdb class needs only open and read data from libdb database
@@ -53,6 +54,8 @@ class Libdb: public DB_ {
5354
Libdb();
5455
bool connect_database(std::string path);
5556
DBC * get_database();
57+
void close_db();
58+
~Libdb();
5659
};
5760
/*
5861
* GDBM class provides API for GDBM, allowes to open and create and fill gdbm database
@@ -69,6 +72,7 @@ class GDBM_: public DB_ {
6972
bool create_database(std::string name);
7073
bool fill_database(DB_ * old_database);
7174
void close_db();
75+
~GDBM_();
7276
};
7377
//https://github.com/LMDB/lmdb/blob/mdb.master/libraries/liblmdb/mtest2.c
7478
//further inspiration
@@ -86,5 +90,6 @@ class LMDB_: public DB_ {
8690
bool create_database(std::string name);
8791
bool fill_database(DB_ * old_database);
8892
void close_db();
93+
~LMDB_();
8994
};
9095

db.cc

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
#include "convert_db.h"
22

3-
4-
5-
Libdb::Libdb(){
6-
database_type = DB_type::LIBDB;
7-
};
83
bool DB_::connect_database(std::string){
94
return false;
105
}
@@ -21,6 +16,14 @@ DBC * DB_::get_database() {
2116
return NULL;
2217
}
2318
void DB_::close_db(){}
19+
20+
DB_::~DB_() {
21+
}
22+
23+
Libdb::Libdb(){
24+
database_type = DB_type::LIBDB;
25+
};
26+
2427
bool Libdb::connect_database(std::string path){
2528
DB * db;
2629
int status;
@@ -48,6 +51,15 @@ DBC * Libdb::get_database(){
4851
return this->cursorp;
4952
}
5053

54+
void Libdb::close_db() {
55+
cursorp->close(cursorp);
56+
db->close(db, 0);
57+
}
58+
59+
Libdb::~Libdb() {
60+
close_db();
61+
}
62+
5163
GDBM_::GDBM_(){
5264
database_type = DB_type::GDBM;
5365
}
@@ -81,13 +93,19 @@ bool GDBM_::fill_database(DB_ * old_database){
8193
if (status != 0)
8294
return false;
8395
}
96+
free(data_db);
97+
free(key_db);
8498
return true;
8599
}
86100

87101
void GDBM_::close_db(){
88102
gdbm_close(this->f);
89103
}
90104

105+
GDBM_::~GDBM_() {
106+
close_db();
107+
}
108+
91109
LMDB_::LMDB_(){
92110
database_type = DB_type::LMDB;
93111

@@ -161,4 +179,6 @@ void LMDB_::close_db(){
161179
mdb_env_close(this->lmdb_database);
162180
}
163181

164-
182+
LMDB_::~LMDB_() {
183+
close_db();
184+
}

0 commit comments

Comments
 (0)