Merge branch 'fix-session-leaks'

Fix resource leaks which could happen if backend session object ctor
threw an exception.

See #1314,

Closes #1148.
This commit is contained in:
Vadim Zeitlin
2025-11-14 14:10:48 +01:00
5 changed files with 53 additions and 26 deletions

View File

@@ -379,8 +379,33 @@ struct SOCI_ODBC_DECL odbc_session_backend : details::session_backend
// Return full ODBC connection string.
std::string get_connection_string() const { return connection_string_; }
SQLHENV henv_;
SQLHDBC hdbc_;
// Simple RAII wrapper for ODBC handles.
template <int HANDLE_TYPE>
struct auto_handle
{
~auto_handle()
{
if (h_ != 0)
{
// Ignore errors in destructor, we can't throw from here.
reset();
}
}
SQLRETURN reset()
{
SQLRETURN const rc = SQLFreeHandle(HANDLE_TYPE, h_);
h_ = 0;
return rc;
}
operator SQLHANDLE() const { return h_; }
SQLHANDLE h_ = 0;
};
auto_handle<SQL_HANDLE_ENV> henv_;
auto_handle<SQL_HANDLE_DBC> hdbc_;
std::string connection_string_;

View File

@@ -20,6 +20,7 @@
#include <soci/soci-backend.h>
#include "soci/connection-parameters.h"
#include <memory>
#include <vector>
#include <unordered_map>
@@ -441,8 +442,9 @@ struct SOCI_POSTGRESQL_DECL postgresql_session_backend : details::session_backen
PGconn * conn_;
connection_parameters connectionParameters_;
// File used for tracing or null if none.
FILE* traceFile_ = nullptr;
// File used for tracing or empty if none.
using FilePtr = std::unique_ptr<FILE, int(*)(FILE*)>;
FilePtr traceFile_{nullptr, nullptr};
};

View File

@@ -91,12 +91,12 @@ std::string extract_driver_complete_option(std::string& connectString)
odbc_session_backend::odbc_session_backend(
connection_parameters const & parameters)
: henv_(0), hdbc_(0), product_(prod_uninitialized)
: product_(prod_uninitialized)
{
SQLRETURN rc;
// Allocate environment handle
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv_);
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv_.h_);
if (is_odbc_error(rc))
{
throw soci_error("Unable to get environment handle");
@@ -110,7 +110,7 @@ odbc_session_backend::odbc_session_backend(
}
// Allocate connection handle
rc = SQLAllocHandle(SQL_HANDLE_DBC, henv_, &hdbc_);
rc = SQLAllocHandle(SQL_HANDLE_DBC, henv_, &hdbc_.h_);
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_,
@@ -537,13 +537,13 @@ void odbc_session_backend::clean_up()
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_, "disconnecting");
}
rc = SQLFreeHandle(SQL_HANDLE_DBC, hdbc_);
rc = hdbc_.reset();
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_DBC, hdbc_, "freeing connection");
}
rc = SQLFreeHandle(SQL_HANDLE_ENV, henv_);
rc = henv_.reset();
if (is_odbc_error(rc))
{
throw odbc_soci_error(SQL_HANDLE_ENV, henv_, "freeing environment");

View File

@@ -316,7 +316,7 @@ void postgresql_session_backend::connect(
mode = "w";
}
traceFile_ = fopen(value.c_str(), mode);
traceFile_ = FilePtr{fopen(value.c_str(), mode), std::fclose};
if (!traceFile_)
{
std::ostringstream oss;
@@ -363,7 +363,7 @@ void postgresql_session_backend::connect(
if (traceFile_)
{
PQtrace(conn, traceFile_);
PQtrace(conn, traceFile_.get());
}
if (!timeoutStr.empty())
@@ -456,12 +456,6 @@ void postgresql_session_backend::clean_up()
PQfinish(conn_);
conn_ = 0;
}
if (traceFile_)
{
std::fclose(traceFile_);
traceFile_ = nullptr;
}
}
std::string postgresql_session_backend::get_next_statement_name()

View File

@@ -68,9 +68,7 @@ void check_sqlite_err(sqlite_api::sqlite3* conn, int res,
std::ostringstream ss;
errCallback(ss);
sqlite3_soci_error const error(conn, ss.str());
sqlite3_close(conn); // connection must be closed here
throw error;
throw sqlite3_soci_error(conn, ss.str());
}
}
@@ -160,8 +158,14 @@ sqlite3_session_backend::sqlite3_session_backend(
throw soci_error("Database name must be specified");
}
int res = sqlite3_open_v2(dbname.c_str(), &conn_, connection_flags, (vfs.empty()?NULL:vfs.c_str()));
check_sqlite_err(conn_, res,
sqlite_api::sqlite3* conn = nullptr;
int res = sqlite3_open_v2(dbname.c_str(), &conn, connection_flags, (vfs.empty()?NULL:vfs.c_str()));
// Ensure that the database connection is closed if any errors occur.
std::unique_ptr<sqlite_api::sqlite3, int(*)(sqlite_api::sqlite3*)>
conn_ptr(conn, sqlite3_close);
check_sqlite_err(conn, res,
[&dbname](std::ostream& ostr)
{
ostr << "Cannot establish connection to \"" << dbname << "\"";
@@ -169,13 +173,13 @@ sqlite3_session_backend::sqlite3_session_backend(
);
// Set the timeout first to have effect on the following queries.
res = sqlite3_busy_timeout(conn_, timeout * 1000);
check_sqlite_err(conn_, res, "Failed to set busy timeout for connection. ");
res = sqlite3_busy_timeout(conn, timeout * 1000);
check_sqlite_err(conn, res, "Failed to set busy timeout for connection. ");
if (!synchronous.empty())
{
std::string const query("pragma synchronous=" + synchronous);
execute_hardcoded(conn_, query.c_str(),
execute_hardcoded(conn, query.c_str(),
[&synchronous](std::ostream& ostr)
{
ostr << "Setting synchronous pragma to \"" << synchronous << "\" failed";
@@ -186,13 +190,15 @@ sqlite3_session_backend::sqlite3_session_backend(
if (!foreignKeys.empty())
{
std::string const query("pragma foreign_keys=" + foreignKeys);
execute_hardcoded(conn_, query.c_str(),
execute_hardcoded(conn, query.c_str(),
[&foreignKeys](std::ostream& ostr)
{
ostr << "Setting foreign_keys pragma to \"" << foreignKeys << "\" failed";
}
);
}
conn_ = conn_ptr.release();
}
sqlite3_session_backend::~sqlite3_session_backend()