mirror of
https://github.com/facebook/sapling.git
synced 2025-01-06 21:48:36 +03:00
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:
parent
ceb647df7a
commit
904812bc0d
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user