Bug #103390 Can't read CHAR(36) column if MySqlCommand is prepared
Submitted: 20 Apr 2021 19:19 Modified: 7 Sep 2021 19:29
Reporter: Bradley Grainger (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:8.0.24, 8.0.25 OS:Windows (10)
Assigned to: CPU Architecture:Other (x64)

[20 Apr 2021 19:19] Bradley Grainger
Description:
This bug was introduced in 8.0.22 but may have been obscured by bug #101252 (that occurs in a similar situation); it was also less visible in that release because IgnorePrepare defaulted to true. It's still present in 8.0.24.

When querying a CHAR(36) column, reading a non-NULL value will fail if the MySqlCommand is prepared.

How to repeat:
Create a table and insert a GUID value:

drop table if exists prepared_command_test;
create table prepared_command_test(value char(36));
insert into prepared_command_test(value) values ('3e22b63e-8077-43ab-8cee-17aa1db80861');

Query that table with the following C# code:

using var connection = new MySqlConnection("...");
connection.Open();
using var cmd = connection.CreateCommand();
cmd.CommandText = "select value from prepared_command_test";
cmd.Prepare(); // works fine if this line is commented out
using var reader = cmd.ExecuteReader();
while (reader.Read()) // <-- throws here
	Console.WriteLine(reader.GetGuid(0));

The program throws an exception:

Unhandled Exception: System.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: count
   at System.Text.UTF8Encoding.GetString(Byte[] bytes, Int32 index, Int32 count)
   at MySql.Data.MySqlClient.NativeDriver.ReadColumnValue(Int32 index, MySqlField field, IMySqlValue valObject)
   at MySql.Data.MySqlClient.ResultSet.ReadColumnData(Boolean outputParms)
   at MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlDataReader.Read()
   at Test.Program.Main(String[] args)
[28 May 2021 8:14] MySQL Verification Team
Hello Bradley,

Thank you for the report and test case.
Verified as described.

regards,
Umesh
[7 Sep 2021 19:29] Christine Cole
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/NET 8.0.27 release, and here's the proposed changelog entry from the documentation team:

When querying a CHAR(36) column, reading a non-NULL value could emit an
error if MySqlCommand is prepared.

Thank you for the bug report.