avoid leaking the sqlite3 db if we fail to open it

Summary:
We need to call sqlite3_close() if sqlite3_open() fails to avoid leaking the
DB object that was allocated.

Reviewed By: chadaustin, strager

Differential Revision: D8529745

fbshipit-source-id: 1087dac8343b8b3120c89bd3c9a250970e69df8e
This commit is contained in:
Adam Simpkins 2018-06-20 16:24:02 -07:00 committed by Facebook Github Bot
parent ceb647df7a
commit 904812bc0d
2 changed files with 9 additions and 3 deletions

View File

@ -38,8 +38,14 @@ void checkSqliteResult(sqlite3* db, int result) {
}
SqliteDatabase::SqliteDatabase(AbsolutePathPiece path) {
sqlite3* db;
checkSqliteResult(nullptr, sqlite3_open(path.copy().c_str(), &db));
sqlite3* db = nullptr;
auto result = sqlite3_open(path.copy().c_str(), &db);
if (result != SQLITE_OK) {
// On most error conditions sqlite3_open() does allocate the DB object,
// and it needs to be closed afterwards if it is non-null.
sqlite3_close(db);
checkSqliteResult(nullptr, result);
}
db_ = db;
}

View File

@ -48,7 +48,7 @@ class SqliteDatabase {
folly::Synchronized<sqlite3*>::LockedPtr lock();
private:
folly::Synchronized<sqlite3*> db_;
folly::Synchronized<sqlite3*> db_{nullptr};
};
/** Represents the sqlite vm that will execute a SQL statement.