Bug #113092 MySql.Data driver crash
Submitted: 15 Nov 2023 14:54 Modified: 16 Nov 2023 10:53
Reporter: Tuomas Hietanen Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:all (8.2.0) OS:Windows
Assigned to: CPU Architecture:Any
Tags: LoadCharacterSets, show collation

[15 Nov 2023 14:54] Tuomas Hietanen
Description:
On connect, the driver tries to load collations and cast them to integers.
However collation is nullable and there are some null-collations, for example:

+--------------------------------+----------+------+---------+----------+---------+
| Collation                      | Charset  | Id   | Default | Compiled | Sortlen |
+--------------------------------+----------+------+---------+----------+---------+

| uca1400_ai_ci                  | NULL     | NULL | NULL    | Yes      |       8 |
| uca1400_ai_cs                  | NULL     | NULL | NULL    | Yes      |       8 |
| uca1400_as_ci                  | NULL     | NULL | NULL    | Yes      |       8 |
| uca1400_as_cs                  | NULL     | NULL | NULL    | Yes      |       8 |

So the driver fails to connect the database:
InvalidCast_FromDBNull: Object cannot be cast from DBNull to other types.

Driver has the following code:

		MySqlCommand mySqlCommand = new MySqlCommand("SHOW COLLATION", connection);
		try
		{
			using MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
			CharacterSets = new Dictionary<int, string>();
			while (mySqlDataReader.Read())
			{
				CharacterSets[Convert.ToInt32(mySqlDataReader["id"], NumberFormatInfo.InvariantInfo)] = mySqlDataReader.GetString(mySqlDataReader.GetOrdinal("charset"));
			}
		}
		catch (Exception ex)
		{
			MySqlTrace.LogError(ThreadID, ex.Message);
			throw;
		}

How to repeat:
Try to use MySql.Data driver for example with the current MariaDB.

Suggested fix:
Add more null-checking, and just skip the null-collations on Driver.cs method LoadCharacterSets
[15 Nov 2023 21:57] Tuomas Hietanen
Thanks! I tried to search duplicates but really didn't find with this forum search.

Ok, sounds easy fix, but whatever. I wouldn't use MariaDb in production anyway, I was just quickly testing if my tech-stack could be run on non-Microsoft database.
[16 Nov 2023 10:53] MySQL Verification Team
Hello Tuomas,

Thank you for the bug report.
As Bradley Grainger said this is duplicate of bug #109331. Please see bug #109331. Thanks.

Regards,
Ashwini Patil