diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2024-07-30 15:07:35 +0500 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2024-08-01 07:26:54 +0200 |
commit | 1c8a48ba766f30c9babc80db46e2cf24074486b8 (patch) | |
tree | 85f2a5c755141e01982f14c86374342d410f1180 | |
parent | 6c413b1e75c98175bc2b498e8980150d9f69d13e (diff) |
Fix OTools::ThrowException
First, it used 5-character buffer for SQL state, where MS documentation
for SQLGetDiagRec specifies, that the buffer is for five-character code
plus terminating NULL. The declaration of the function in Windows Kit's
sqlucode.h uses decoration '_Out_writes_opt_(6)' for the parameter.
Next, SQLGetDiagRec may fail or return SQL_NO_DATA; and then, the data
in buffers will be undefiled.
Change-Id: Ia36555b4fe4f49c8803a25f8fc1cc1a3b6ac4ddc
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/171238
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
-rw-r--r-- | connectivity/source/drivers/odbc/OTools.cxx | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/connectivity/source/drivers/odbc/OTools.cxx b/connectivity/source/drivers/odbc/OTools.cxx index 999324a4d571..0909b03f14b2 100644 --- a/connectivity/source/drivers/odbc/OTools.cxx +++ b/connectivity/source/drivers/odbc/OTools.cxx @@ -203,7 +203,7 @@ void OTools::ThrowException(const OConnection* _pConnection, // corresponding for hdbc. if (bUseWChar && _pConnection->functions().has(ODBC3SQLFunctionId::GetDiagRecW)) { - SQLWCHAR szSqlState[5]; + SQLWCHAR szSqlState[6]; SQLWCHAR szErrorMessage[SQL_MAX_MESSAGE_LENGTH]; szErrorMessage[0] = '\0'; SQLSMALLINT cchErrorMsg = 0; @@ -212,12 +212,15 @@ void OTools::ThrowException(const OConnection* _pConnection, szSqlState, &pfNativeError, szErrorMessage, std::size(szErrorMessage) - 1, &cchErrorMsg); - errorMessage = toUString(szErrorMessage, cchErrorMsg); - sqlState = toUString(szSqlState, std::size(szSqlState)); + if (SQL_SUCCEEDED(n)) + { + errorMessage = toUString(szErrorMessage, cchErrorMsg); + sqlState = toUString(szSqlState, 5); + } } else { - SQLCHAR szSqlState[5]; + SQLCHAR szSqlState[6]; SQLCHAR szErrorMessage[SQL_MAX_MESSAGE_LENGTH]; szErrorMessage[0] = '\0'; SQLSMALLINT pcbErrorMsg = 0; @@ -226,9 +229,12 @@ void OTools::ThrowException(const OConnection* _pConnection, szSqlState, &pfNativeError, szErrorMessage,sizeof szErrorMessage - 1,&pcbErrorMsg); - rtl_TextEncoding _nTextEncoding = osl_getThreadTextEncoding(); - errorMessage = toUString(szErrorMessage, pcbErrorMsg, _nTextEncoding); - sqlState = toUString(szSqlState, std::size(szSqlState), _nTextEncoding); + if (SQL_SUCCEEDED(n)) + { + rtl_TextEncoding _nTextEncoding = osl_getThreadTextEncoding(); + errorMessage = toUString(szErrorMessage, pcbErrorMsg, _nTextEncoding); + sqlState = toUString(szSqlState, 5, _nTextEncoding); + } } OSL_ENSURE(n != SQL_INVALID_HANDLE,"SdbODBC3_SetStatus: SQLError returned SQL_INVALID_HANDLE"); OSL_ENSURE(n == SQL_SUCCESS || n == SQL_SUCCESS_WITH_INFO || n == SQL_NO_DATA_FOUND || n == SQL_ERROR,"SdbODBC3_SetStatus: SQLError failed"); |