From a2c3ef6d8108355ce5daf6ff72310ac93ae745f0 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Tue, 22 Mar 2022 21:18:04 +0300 Subject: Move BSTR wrapper to systools, and fix some wrong BSTR uses BSTR is documented [1] to be prefixed by a 32-bit integer specifying its length *in bytes* (not characters), so passing wchar_t* as BSTR is wrong, and the length member of rtl_uString can't substitute the proper BSTR length, since rtl_uString::length specifies length in characters. Any code taking BSTR and using SysStringLen to get its length would only get half of the passed OUString data. In dbaccess/source/ui/dlg/adodatalinks.cxx, the abovementioned error was implemented. OTOH, OLEVariant::getByteSequence() in connectivity/source/drivers/ado/Aolevariant.cxx passed BSTR from tagVARIANT to ctor of OLEString, which resulted in the BSTR being freed in both dtors of OLEString and OLEVariant (the latter calls VariantClear, which itself clears string when vtfield is VT_BSTR). [1] https://docs.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr Change-Id: Iedbd62b20133644258af3660616add7b63cac258 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131950 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- dbaccess/source/ui/dlg/adodatalinks.cxx | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'dbaccess') diff --git a/dbaccess/source/ui/dlg/adodatalinks.cxx b/dbaccess/source/ui/dlg/adodatalinks.cxx index be60cdb9b089..82af63688cc1 100644 --- a/dbaccess/source/ui/dlg/adodatalinks.cxx +++ b/dbaccess/source/ui/dlg/adodatalinks.cxx @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -57,12 +58,11 @@ OUString PromptNew(sal_IntPtr hWnd) sal::systools::COMReference piTmpConnection(piDispatch, sal::systools::COM_QUERY_THROW); - BSTR _result = nullptr; + sal::systools::BStr _result; sal::systools::ThrowIfFailed(piTmpConnection->get_ConnectionString(&_result), "get_ConnectionString failed"); - // FIXME: Don't we need SysFreeString(_result)? - return OUString(o3tl::toU(_result), SysStringLen(_result)); + return OUString(_result); } catch (const sal::systools::ComError&) { @@ -80,9 +80,8 @@ OUString PromptEdit(sal_IntPtr hWnd, OUString const & connstr) sal::systools::COMReference piTmpConnection; piTmpConnection.CoCreateInstance(CLSID_CADOConnection, nullptr, CLSCTX_INPROC_SERVER); - // FIXME: BSTR is not just cast from a random string sal::systools::ThrowIfFailed( - piTmpConnection->put_ConnectionString(const_cast(o3tl::toW(connstr.getStr()))), + piTmpConnection->put_ConnectionString(sal::systools::BStr(connstr)), "put_ConnectionString failed"); // Instantiate DataLinks object. @@ -111,12 +110,11 @@ OUString PromptEdit(sal_IntPtr hWnd, OUString const & connstr) piTmpConnection.set(piDispatch, sal::systools::COM_QUERY_THROW); } - BSTR _result = nullptr; + sal::systools::BStr _result; sal::systools::ThrowIfFailed(piTmpConnection->get_ConnectionString(&_result), "get_ConnectionString failed"); - // FIXME: Don't we need SysFreeString(_result)? - return OUString(o3tl::toU(_result), SysStringLen(_result)); + return OUString(_result); } catch (const sal::systools::ComError&) { -- cgit