Bug #95498 Access violation exception (0xC0000005) using Connector/ODBC
Submitted: 23 May 2019 15:26 Modified: 15 Nov 2019 15:20
Reporter: Stefano Gozzi Email Updates:
Status: Won't fix Impact on me:
None 
Category:Connector / ODBC Severity:S2 (Serious)
Version:8.0.16.0, 8.0.17 OS:Windows (Win7 & Win10)
Assigned to: CPU Architecture:Any

[23 May 2019 15:26] Stefano Gozzi
Description:
In my application there are some call to a function that create a table.
The connection to the database is made by means of a DSN that point to the localhost instance of MySQL 8.
The database  exists on the server.
The application loads the OpenSSL modules libeay32.dll and ssleay32.dll at startup (linked with .lib)
The creation of the first table is fine. The second time that I try to open the connection to the database, I get the Access Violation exception.

this is the call stack of the exception:
libeay32.dll!50d71b35() 	
[Frames below may be incorrect and/or missing, no symbols loaded for libeay32.dll]	
myodbc8w.dll!ssl_start()  Line 420	C++
myodbc8w.dll!mysql_server_init(int argc=-859635712, char * * argv=0xcccccccc, char * * groups=0xcccccccc)  Line 140	C++
myodbc8w.dll!mysql_init(MYSQL * mysql=0xccc30000)  Line 3051 + 0xb bytes	C++
0001b8ff()	

If the OpenSSL modules are not loaded by the application, the code create the tables correctly, without exceptions.

How to repeat:
- Create a database in the local instance of MySQL
- Create a DSN, in the ODBC Administration console, using MySQL Connector 8 and pointing to the created database.
- Run application that loads OpenSSL.
- Execute code that open connection to the database, create a table and close connection.
- Execute the same code a second time.
- Exception occur.

Here's my code to create a table:
void CreateTable(CString szTable)
{
	CDatabase Database;
	CString szDataSourceName = _T("DSN=ODBC_A;UID=root;PWD=pwd");

	BOOL bOpen = FALSE;
	TRY
	{
		bOpen = Database.OpenEx(szDataSourceName, CDatabase::noOdbcDialog);

	}
	CATCH(CDBException, e)
	{
		CString szErr;
		szErr.Format(_T("Error opening database (%s)"), e->m_strError);
		AfxMessageBox(szErr);
		return;
	}
	END_CATCH

		Database.SetQueryTimeout(5);
	Database.SetLoginTimeout(5);

	TRY
	{
		CString SQLDropTable = _T("DROP TABLE ");
		SQLDropTable += szTable;
		Database.ExecuteSQL(SQLDropTable);
	}
	CATCH(CDBException, e)
	{
	}
	END_CATCH

	CString SQLCreateTable;
	SQLCreateTable.Format(_T("CREATE TABLE %s (TimeCol datetime,MSecCol smallint,LocalCol datetime,UserCol varchar(255),EventCol varchar(24),EvNumCol smallint,EvDescCol varchar(255),DescCol varchar(255),CommCol varchar(255),DurCol integer,UniID integer,TraID integer)")
		, szTable);

	TRY
	{
		Database.ExecuteSQL(SQLCreateTable);

	}
	CATCH(CDBException, e)
	{
		CString szErr;
		szErr.Format(_T("Error opening database (%s)"), e->m_strError);
		AfxMessageBox(szErr);
		return;
	}
	END_CATCH
	

	if(bOpen)
		Database.Close();
}
[23 May 2019 15:48] Stefano Gozzi
Example project to reproduce the problem

Attachment: mysql-bug-example-95498.zip (application/zip, text), 2.30 MiB.

[30 Jul 2019 11:23] Rakesh M
I am facing similar issue, first time I connect to my local instance of MYSQL DB through my C++ code it works fine. 
In same flow I disconnect and then later try to reconnect using same credentials. It gives same error as above. On my windows console message is : 
"ORA-24550: signal received: Unhandled exception: Code=c0000005 Flags=0"

I am using ODBC driver version is : MYODBC8W.DLL . 

MY Visual Studio Debugger throws error pop up stating "First-chance exception at 0x00007ff8882c6dc1 (myodbc8w.dll) in ****.exe: 0xC0000005: Access violation reading location 0x0000000000000190."
[19 Sep 2019 7:33] MySQL Verification Team
Hello Stefano Gozzi,

Thank you for the report and test case.
Verified as described using VS 2019, Connector/ODBC 8.0.17 on Windows 10.

Regards
Ashwini Patil
[15 Nov 2019 15:20] Rafal Somla
Posted by developer:
 
This issue is most probably due to a mismatch between version of OpenSSL library that was used to build Con/ODBC and the version that was used at runtime. As such, there is little we can do about it. The only way to fix it is to use for your application exact the same version of OpenSSL that was used to build the connector. This information can be found in INFO_BIN file distributed in the binary package. For example, in case of Con/ODBC 8.0.17 it is OpenSSL 1.0.2s.

The situation we have here is that user code links to OpenSSL library so that the resulting executable loads OpenSSL DLLs into its image at startup. These DLLs are provided in the project. Later, when application opens ODBC connection, the ODBC driver manager loads MySQL ODBC drvier, which also depends on ssleay32.dll and will use the version already loaded into the image. But it was most likely built with a different version of OpenSSL which can easily lead to a crash (since OpenSSL does not guarantee that their DLLs coming from different versions are all compatible with each other).

In my tests I was able to reproduce the crash. If I re-built test app with the same version of OpenSSL that was used to build Con/ODBC, the crash is gone.

I will close this bug because there is nothing we can do about it. Hopefully the situation will improve with OpenSSL 1.1.1 where more care is taken about ABI compatibility of different OpenSSL versions.