Skip to content

Commit 2b420d7

Browse files
committed
ODBC-280 The fix of possible crash in threaded environment.
The crash could occur if 2 connections established at same time in different threads. One of functions read/wrote to the same memory, and it's call wasn't guarded. The fix actually completely removed it from connection routine, because it's not really needed to be re-run for each connecion. Now it part of env init, and also the call is guarded. Some warnings fixes on Windows.
1 parent bc35428 commit 2b420d7

File tree

9 files changed

+38
-23
lines changed

9 files changed

+38
-23
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ IF(WIN32)
203203
# SET(DSN_DIALOG_FILES ${DSN_DIALOG_FILES}
204204
# ma_platform_win32.c)
205205

206-
SET(PLATFORM_DEPENDENCIES ws2_32 Shlwapi)
206+
SET(PLATFORM_DEPENDENCIES ws2_32 Shlwapi Pathcch)
207207
IF (MSVC)
208208
MESSAGE(STATUS "MSVC_VERSION= ${MSVC_VERSION}")
209209
IF (NOT(MSVC_VERSION LESS 1900))

ma_connection.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*************************************************************************************/
1919
#include <ma_odbc.h>
2020

21+
extern const char* DefaultPluginLocation;
22+
2123
struct st_madb_isolation MADB_IsolationLevel[] =
2224
{
2325
{SQL_TRANSACTION_REPEATABLE_READ, "REPEATABLE READ"},
@@ -140,7 +142,7 @@ SQLRETURN MADB_DbcSetAttr(MADB_Dbc *Dbc, SQLINTEGER Attribute, SQLPOINTER ValueP
140142
if (!Dbc)
141143
{
142144
/* Todo: check */
143-
if (Attribute != SQL_ATTR_TRACE ||
145+
if (Attribute != SQL_ATTR_TRACE &&
144146
Attribute != SQL_ATTR_TRACEFILE)
145147
return SQL_INVALID_HANDLE;
146148
return SQL_SUCCESS;
@@ -609,10 +611,9 @@ SQLRETURN MADB_DbcConnectDB(MADB_Dbc *Connection,
609611
}
610612
else
611613
{
612-
const char *DefaultLocation= MADB_GetDefaultPluginsDir(Connection);
613-
if (DefaultLocation != NULL)
614+
if (DefaultPluginLocation != NULL)
614615
{
615-
mysql_optionsv(Connection->mariadb, MYSQL_PLUGIN_DIR, DefaultLocation);
616+
mysql_optionsv(Connection->mariadb, MYSQL_PLUGIN_DIR, DefaultPluginLocation);
616617
}
617618
}
618619

ma_connection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ SQLRETURN MADB_Dbc_GetCurrentDB(MADB_Dbc *Connection, SQLPOINTER CurrentDB, SQLI
5757
SQLSMALLINT *StringLengthPtr, my_bool isWChar);
5858
BOOL MADB_SqlMode(MADB_Dbc *Connection, enum enum_madb_sql_mode SqlMode);
5959
/* Has platform versions */
60-
const char* MADB_GetDefaultPluginsDir(MADB_Dbc *Dbc);
60+
char* MADB_GetDefaultPluginsDir(char* Buffer, size_t Size);
6161

6262
#define MADB_SUPPORTED_CONVERSIONS SQL_CVT_BIGINT | SQL_CVT_BIT | SQL_CVT_CHAR | SQL_CVT_DATE |\
6363
SQL_CVT_DECIMAL | SQL_CVT_DOUBLE | SQL_CVT_FLOAT |\

ma_environment.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ extern Client_Charset utf8;
2222
extern MARIADB_CHARSET_INFO* DmUnicodeCs;
2323
extern MARIADB_CHARSET_INFO dummyUtf32le;
2424
Client_Charset SourceAnsiCs= {0, 0}; /* Basically it should be initialized with 0 anyway */
25+
char* DefaultPluginLocation= NULL;
26+
#ifndef _MAX_PATH
27+
# define _MAX_PATH 260
28+
#endif
29+
static char PluginLocationBuf[_MAX_PATH];
2530

2631
MARIADB_CHARSET_INFO * mysql_find_charset_name(const char *name);
2732

@@ -114,7 +119,11 @@ MADB_Env *MADB_EnvInit()
114119
utf8.cs_info= mariadb_get_charset_by_name("utf8mb4");
115120
GetDefaultLogDir();
116121
GetSourceAnsiCs(&SourceAnsiCs);
117-
122+
/* If we have something in the buffer - then we've already tried to get default location w/out much success */
123+
if (DefaultPluginLocation == NULL && strlen(PluginLocationBuf) == 0)
124+
{
125+
DefaultPluginLocation= MADB_GetDefaultPluginsDir(PluginLocationBuf, sizeof(PluginLocationBuf));
126+
}
118127
cleanup:
119128
#ifdef _WIN32
120129
if (!Env)

ma_platform_posix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ BOOL MADB_DSN_PossibleConnect(MADB_Dsn *Dsn)
398398

399399

400400
/* Stub - atm it looks like we don't need to do anything here */
401-
const char* MADB_GetDefaultPluginsDir(MADB_Dbc *Dbc)
401+
char* MADB_GetDefaultPluginsDir(char* Buffer, size_t Size)
402402
{
403403
return NULL;
404404
}

ma_platform_win32.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
/* NOTE If you change something in this program, please consider if other platform's version
2424
of the function you are changing, needs to be changed accordingly */
2525

26-
#include <ma_odbc.h>
27-
#include "Shlwapi.h"
26+
#include "ma_odbc.h"
27+
#include <pathcch.h>
2828

2929
extern Client_Charset utf8;
3030
char LogFile[256];
@@ -295,23 +295,25 @@ BOOL MADB_DirectoryExists(const char *Path)
295295
return (FileAttributes != INVALID_FILE_ATTRIBUTES) && (FileAttributes & FILE_ATTRIBUTE_DIRECTORY);
296296
}
297297

298-
const char* MADB_GetDefaultPluginsDir(MADB_Dbc *Dbc)
298+
char* MADB_GetDefaultPluginsDir(char* Buffer, size_t Size)
299299
{
300300
HMODULE hModule = GetModuleHandle(MADB_DRIVER_NAME);
301-
static char OurLocation[_MAX_PATH];
301+
wchar_t wOurLocation[_MAX_PATH];
302302
const char *PluginsSubDirName= "\\"MADB_DEFAULT_PLUGINS_SUBDIR;
303+
HRESULT hr;
303304

304-
memset(OurLocation, 0, sizeof(OurLocation));
305-
GetModuleFileName(hModule, OurLocation, _MAX_PATH);
306-
PathRemoveFileSpec(OurLocation);
305+
memset(Buffer, 0, Size);
306+
GetModuleFileNameW(hModule, wOurLocation, _MAX_PATH);
307+
hr= PathCchRemoveFileSpec(wOurLocation, _MAX_PATH);
307308

308-
if (strlen(OurLocation) < _MAX_PATH - strlen(PluginsSubDirName))
309+
WideCharToMultiByte(GetACP(), 0, wOurLocation, -1, Buffer, Size, NULL, NULL);
310+
if (strlen(Buffer) < Size - strlen(PluginsSubDirName))
309311
{
310-
strcpy(OurLocation + strlen(OurLocation), PluginsSubDirName);
312+
strcpy(Buffer + strlen(Buffer), PluginsSubDirName);
311313

312-
if (MADB_DirectoryExists(OurLocation) != FALSE)
314+
if (MADB_DirectoryExists(Buffer) != FALSE)
313315
{
314-
return OurLocation;
316+
return Buffer;
315317
}
316318
}
317319
return NULL;

ma_platform_win32.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828

2929
#define _ma_platform_x_h_
3030

31-
#define WIN32_LEAN_AND_MEAN
31+
#ifndef WIN32_LEAN_AND_MEAN
32+
# define WIN32_LEAN_AND_MEAN
33+
#endif // !WIN32_LEAN_AND_MEAN
34+
3235
#define _WINSOCKAPI_
3336
#define DONT_DEFINE_VOID
3437
#define HAVE_UNICODE

ma_result.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ SQLRETURN MADB_StmtMoreResults(MADB_Stmt *Stmt)
222222
mysql_stmt_data_seek(Stmt->stmt, 0);
223223
}
224224
}
225-
UNLOCK_MARIADB(Stmt->Connection);
226225
}
226+
UNLOCK_MARIADB(Stmt->Connection);
227227

228228
return ret;
229229
}
@@ -232,7 +232,7 @@ SQLRETURN MADB_StmtMoreResults(MADB_Stmt *Stmt)
232232
/* {{{ MADB_RecordsToFetch */
233233
SQLULEN MADB_RowsToFetch(MADB_Cursor *Cursor, SQLULEN ArraySize, unsigned long long RowsInResultst)
234234
{
235-
SQLLEN Position= Cursor->Position >= 0 ? Cursor->Position : 0;
235+
SQLULEN Position= Cursor->Position >= 0 ? Cursor->Position : 0;
236236
SQLULEN result= ArraySize;
237237

238238
Cursor->RowsetSize= ArraySize;

wininstall/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ ELSE()
5656
ENDIF()
5757

5858
ADD_EXECUTABLE(change_dsns_driver change_dsns_driver.c ${CMAKE_SOURCE_DIR}/ma_dsn.c ${CMAKE_SOURCE_DIR}/ma_platform_win32.c ${CMAKE_SOURCE_DIR}/ma_common.c)
59-
TARGET_LINK_LIBRARIES(change_dsns_driver ${ODBC_LIBS} ${ODBC_INSTLIBS} legacy_stdio_definitions Shlwapi)
59+
TARGET_LINK_LIBRARIES(change_dsns_driver ${ODBC_LIBS} ${ODBC_INSTLIBS} legacy_stdio_definitions Shlwapi Pathcch)
6060

6161
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/wininstall/mariadb_odbc.xml.in
6262
${CMAKE_BINARY_DIR}/wininstall/mariadb_odbc.xml)

0 commit comments

Comments
 (0)