From ccdac337f6cb69b4c57c5ad05cfff6be459b69b2 Mon Sep 17 00:00:00 2001 From: Mark Waddingham Date: Thu, 18 Apr 2019 14:30:39 +0100 Subject: [PATCH] [[ Bug 22020 ]] Fix memory leak when dbsqlite connection fails This patch fixes a memory leak which occurs when opening a dbsqlite connection fails. As sqlite3_open will create a connection struct even if there is an error, sqlite3_close must be called regardless of the error returned when opening it. --- libsqlite/src/sqlitedataset.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/libsqlite/src/sqlitedataset.cpp b/libsqlite/src/sqlitedataset.cpp index 8d58546a..d3f4c160 100644 --- a/libsqlite/src/sqlitedataset.cpp +++ b/libsqlite/src/sqlitedataset.cpp @@ -426,7 +426,17 @@ int SqliteDatabase::connect(bool p_use_uri) return DB_CONNECTION_OK; } else + { + /* sqlite3_open will still create a connection object even if there is an + * error - unless it runs out of memory, in which case the connection + * pointer will be nullptr. */ + if (conn != nullptr) + { + sqlite3_close(conn); + conn = nullptr; + } throw DbErrors(getErrorMsg()); + } return DB_CONNECTION_NONE; };