From 3c3aaf5a48621804d0c8671a3c393700e570fb56 Mon Sep 17 00:00:00 2001 From: heck Date: Fri, 16 Apr 2021 04:28:13 +0200 Subject: [PATCH] pEpSQLite - Exception on execute() on closed db. (not segfault) --- src/pEpSQLite.cc | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/pEpSQLite.cc b/src/pEpSQLite.cc index 6e88022..a6ac759 100644 --- a/src/pEpSQLite.cc +++ b/src/pEpSQLite.cc @@ -20,7 +20,8 @@ namespace pEp { int rc{::sqlite3_open(db_path.c_str(), &db)}; if (rc) { - runtime_error e{string("Can't open database (" + db_path + "):" + ::sqlite3_errmsg(db))}; + ::sqlite3_close(db); + runtime_error e{string("Can't open database (" + db_path + "): " + ::sqlite3_errmsg(db))}; throw (e); } } @@ -62,14 +63,19 @@ namespace pEp { ResultSet pEpSQLite::execute(const string& stmt) { - pEpLogClass("called"); - this->resultset.clear(); - char *zErrMsg = nullptr; - int rc = ::sqlite3_exec(db, stmt.c_str(), (int (*)(void *, int, char **, char **)) &callback, this, &zErrMsg); - if (rc != SQLITE_OK) { - runtime_error e{string("execute: " + string(::sqlite3_errmsg(db)) + ":" + string(zErrMsg))}; - ::sqlite3_free(zErrMsg); + if(db == nullptr) { + runtime_error e{string("execute(): - Error: db is not open")}; throw (e); + } else { + pEpLogClass("called"); + this->resultset.clear(); + char *zErrMsg = nullptr; + int rc = ::sqlite3_exec(db, stmt.c_str(), (int (*)(void *, int, char **, char **)) &callback, this, &zErrMsg); + if (rc != SQLITE_OK) { + runtime_error e{string("execute: " + string(::sqlite3_errmsg(db)) + ":" + string(zErrMsg))}; + ::sqlite3_free(zErrMsg); + throw (e); + } } return resultset; }