From 177792660697f85763b39f455d7ebff0f83084fd Mon Sep 17 00:00:00 2001 From: Lionel Elie Mamane Date: Tue, 17 Nov 2020 02:14:15 +0100 Subject: pgsql-sdbc: use libpq's custom free()... ... for stuff allocated by libpq Their documentation says this is important on Microsoft Windows: It is particularly important that this function, rather than free(), be used on Microsoft Windows. This is because allocating memory in a DLL and releasing it in the application works only if multithreaded/single-threaded, release/debug, and static/dynamic flags are the same for the DLL and the application. Also use const unique_ptr since we don't need the value to survive the scope in any way. Change-Id: If4637ea0cd1c05125d63e2f3d37dbeaf716973f9 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/105967 Tested-by: Lionel Mamane Reviewed-by: Lionel Mamane --- connectivity/source/drivers/postgresql/pq_baseresultset.cxx | 2 +- connectivity/source/drivers/postgresql/pq_connection.cxx | 3 ++- connectivity/source/drivers/postgresql/pq_preparedstatement.cxx | 2 +- connectivity/source/drivers/postgresql/pq_tools.hxx | 8 ++++++++ connectivity/source/drivers/postgresql/pq_updateableresultset.cxx | 2 +- 5 files changed, 13 insertions(+), 4 deletions(-) (limited to 'connectivity') diff --git a/connectivity/source/drivers/postgresql/pq_baseresultset.cxx b/connectivity/source/drivers/postgresql/pq_baseresultset.cxx index 8fc7140e4817..9ff5e01e098a 100644 --- a/connectivity/source/drivers/postgresql/pq_baseresultset.cxx +++ b/connectivity/source/drivers/postgresql/pq_baseresultset.cxx @@ -456,7 +456,7 @@ Sequence< sal_Int8 > BaseResultSet::getBytes( sal_Int32 columnIndex ) char * res = reinterpret_cast(PQunescapeBytea( reinterpret_cast(val.getStr()), &length)); ret = Sequence< sal_Int8 > ( reinterpret_cast(res), length ); if( res ) - free( res ); + PQfreemem( res ); } return ret; } diff --git a/connectivity/source/drivers/postgresql/pq_connection.cxx b/connectivity/source/drivers/postgresql/pq_connection.cxx index 5d97f2b2436d..e4716fe8855d 100644 --- a/connectivity/source/drivers/postgresql/pq_connection.cxx +++ b/connectivity/source/drivers/postgresql/pq_connection.cxx @@ -41,6 +41,7 @@ #include "pq_connection.hxx" #include "pq_statement.hxx" +#include "pq_tools.hxx" #include "pq_preparedstatement.hxx" #include "pq_databasemetadata.hxx" #include "pq_xtables.hxx" @@ -461,7 +462,7 @@ void Connection::initialize( const Sequence< Any >& aArguments ) if ( err != nullptr) { errorMessage = OUString( err, strlen(err), ConnectionSettings::encoding ); - free(err); + PQfreemem(err); } else errorMessage = "#no error message#"; diff --git a/connectivity/source/drivers/postgresql/pq_preparedstatement.cxx b/connectivity/source/drivers/postgresql/pq_preparedstatement.cxx index c1d9a4f66731..344c27175850 100644 --- a/connectivity/source/drivers/postgresql/pq_preparedstatement.cxx +++ b/connectivity/source/drivers/postgresql/pq_preparedstatement.cxx @@ -481,7 +481,7 @@ void PreparedStatement::setBytes( checkClosed(); checkColumnIndex( parameterIndex ); size_t len; - std::unique_ptr escapedString( + const std::unique_ptr> escapedString( PQescapeBytea( reinterpret_cast(x.getConstArray()), x.getLength(), &len)); if( ! escapedString ) { diff --git a/connectivity/source/drivers/postgresql/pq_tools.hxx b/connectivity/source/drivers/postgresql/pq_tools.hxx index 90490be81eb6..18b105870705 100644 --- a/connectivity/source/drivers/postgresql/pq_tools.hxx +++ b/connectivity/source/drivers/postgresql/pq_tools.hxx @@ -51,6 +51,14 @@ #include #include +namespace +{ +// helper to create one-time deleters +template +using deleter_from_fn = std::integral_constant; + +} + namespace pq_sdbc_driver { bool isWhitespace( sal_Unicode c ); diff --git a/connectivity/source/drivers/postgresql/pq_updateableresultset.cxx b/connectivity/source/drivers/postgresql/pq_updateableresultset.cxx index 880adc647c7e..d8780e76c563 100644 --- a/connectivity/source/drivers/postgresql/pq_updateableresultset.cxx +++ b/connectivity/source/drivers/postgresql/pq_updateableresultset.cxx @@ -481,7 +481,7 @@ void UpdateableResultSet::updateBytes( sal_Int32 columnIndex, const css::uno::Se m_updateableField[columnIndex-1].value <<= OUString( reinterpret_cast(escapedString), len, RTL_TEXTENCODING_ASCII_US ); - free( escapedString ); + PQfreemem( escapedString ); } void UpdateableResultSet::updateDate( sal_Int32 columnIndex, const css::util::Date& x ) -- cgit