Bug #111630 Connector does crashes when retrieving data from the database
Submitted: 30 Jun 2023 10:52 Modified: 30 Sep 19:58
Reporter: Sidraya Jayagond (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version: OS:Ubuntu (20.04)
Assigned to: CPU Architecture:Other (s390x)
Tags: Contribution

[30 Jun 2023 10:52] Sidraya Jayagond
Description:
The connector is able to connect to the database and execute insert and delete statements on the database but crashes when attempting to run "Select" Sql queries on BigEndian Client machines.

How to repeat:
static void Main(string[] args){
    string MyConString = "SERVER=localhost; " +
    "Database=TestDB; " +
    "Uid=root;" +
    "Pwd=;";

    MySqlConnection Connection = new MySqlConnection(MyConString);
    Connection.Open();

    Console.WriteLine("Writing to the Database");
    string inQuery = "INSERT INTO Persons VALUES (103, \"ABC\", \"DEF\", \"xyz road\", \"pqr city\")";
    MySqlCommand CommandIn = new MySqlCommand(inQuery, Connection);
    CommandIn.ExecuteNonQuery();
    CommandIn.Dispose();
    PrintTable(Connection, "Persons");

    string updateQuery = "UPDATE Persons SET LastName='Potato' WHERE PersonID=103";
    MySqlCommand CommandUpdate = new MySqlCommand(updateQuery, Connection);
    CommandUpdate.ExecuteNonQuery();
    CommandUpdate.Dispose();
    PrintTable(Connection, "Persons");

    string deleteQuery = "DELETE FROM Persons Where PersonID=103";
    MySqlCommand CommandDelete = new MySqlCommand(deleteQuery, Connection);
    CommandDelete.ExecuteNonQuery();
    CommandDelete.Dispose();
    PrintTable(Connection, "Persons");

    Connection.Close();
}

Expected Output:
Writing to the Database
:PersonID:FirstName:LastName:Street:City:
:103:ABC:DEF:xyz road:pqr city:
:PersonID:FirstName:LastName:Street:City:
:103:ABC:Potato:xyz road:pqr city:

Error seen on the s390x Machine:
Unhandled Exception:
System.Collections.Generic.KeyNotFoundException: The given key '57344' was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2[[System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].get_Item(Int32 key)
   at MySql.Data.MySqlClient.MySqlField.SetFieldEncoding()
   at MySql.Data.MySqlClient.MySqlField.set_CharacterSetIndex(Int32 value)
   at MySql.Data.MySqlClient.NativeDriver.GetColumnData(MySqlField field)
   at MySql.Data.MySqlClient.NativeDriver.GetColumnsData(MySqlField[] columns)
   at MySql.Data.MySqlClient.Driver.GetColumns(Int32 count)
   at MySql.Data.MySqlClient.ResultSet.LoadColumns(Int32 numCols)
   at MySql.Data.MySqlClient.ResultSet..ctor(Driver d, Int32 statementId, Int32 numCols)
   at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlDataReader.Close()
   at MySql.Data.MySqlClient.MySqlCommand.ResetReader()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
   at ConsoleApplication.Program.PrintTable(MySqlConnection Connection, String tableName) in /home/manas/Workspace/MySqlADO/Program.cs:line 15
   at ConsoleApplication.Program.Main(String[] args) in /home/manas/Workspace/MySqlADO/Program.cs:line 57

Suggested fix:
The error seems to stem from the use of BitConverters throughout the code, the BitConverter class checks the endianness of the underlying system and does data serialization accordingly which leads to incorrect reads on incoming little endian data from the server.
[24 Aug 2023 13:32] OCA Admin
Contribution submitted via Github - [Bug #111630]Add support for BigEndian clients in MySQL.Data 
(*) Contribution by Sidraya Jayagond (Github sid8606, mysql-connector-net/pull/54#issuecomment-1683938768): I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: git_patch_1414683985.txt (text/plain), 10.49 KiB.

[5 Sep 2023 7:22] Sidraya Jayagond
Hi Team, can we get the status on the PR by the verification team?
[12 Oct 2023 9:41] Giridhar Trivedi
Hi, 
May I have an update on this issue ?
[23 Oct 2023 11:52] MySQL Verification Team
Hello Sidraya,

Thank you for the bug report and contribution.

Regards,
Ashwini Patil
[30 Sep 19:58] Philip Olson
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/NET 9.1.0 release, and here's the proposed changelog entry from the documentation team for review:

Executing SELECT statements on Big-Endian client machines could cause
MySQL to unexpectedly halt.

Our thanks to Sidraya Jayagond for the contribution.

Thank you for the bug report.