From 904812bc0ddb9b442720e92645e06829c6eff815 Mon Sep 17 00:00:00 2001 From: Adam Simpkins Date: Wed, 20 Jun 2018 16:24:02 -0700 Subject: [PATCH] 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 --- eden/fs/sqlite/Sqlite.cpp | 10 ++++++++-- eden/fs/sqlite/Sqlite.h | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/eden/fs/sqlite/Sqlite.cpp b/eden/fs/sqlite/Sqlite.cpp index 88bdcc92a9..7ad03eb167 100644 --- a/eden/fs/sqlite/Sqlite.cpp +++ b/eden/fs/sqlite/Sqlite.cpp @@ -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; } diff --git a/eden/fs/sqlite/Sqlite.h b/eden/fs/sqlite/Sqlite.h index 7ccc2269c5..46be721116 100644 --- a/eden/fs/sqlite/Sqlite.h +++ b/eden/fs/sqlite/Sqlite.h @@ -48,7 +48,7 @@ class SqliteDatabase { folly::Synchronized::LockedPtr lock(); private: - folly::Synchronized db_; + folly::Synchronized db_{nullptr}; }; /** Represents the sqlite vm that will execute a SQL statement.