From 4869f48f84a1a032f5697ecdf60ee2c13f93d232 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Mon, 16 Mar 2020 20:30:28 -0700 Subject: [PATCH] sqlite: don't leak on connection failure --- sqlx-core/src/sqlite/connection.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sqlx-core/src/sqlite/connection.rs b/sqlx-core/src/sqlite/connection.rs index f6812be3..2f1eaded 100644 --- a/sqlx-core/src/sqlite/connection.rs +++ b/sqlx-core/src/sqlite/connection.rs @@ -73,7 +73,19 @@ async fn establish(url: crate::Result) -> crate::Result { #[allow(unsafe_code)] let status = unsafe { sqlite3_open_v2(filename.as_ptr(), &mut handle, flags, null()) }; + if handle.is_null() { + // Failed to allocate memory + panic!("SQLite is unable to allocate memory to hold the sqlite3 object"); + } + if status != SQLITE_OK { + // Close the handle if there was an error here + // https://sqlite.org/c3ref/close.html + #[allow(unsafe_code)] + unsafe { + let _ = sqlite3_close(handle); + } + return Err(SqliteError::from_connection(handle).into()); }