Bug #93374 MySqlDataReader.GetStream throws IndexOutOfRangeException
Submitted: 27 Nov 2018 21:34 Modified: 29 Nov 2021 18:11
Reporter: Bradley Grainger (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:8.0.13 OS:Windows (10)
Assigned to: CPU Architecture:Other (x64)

[27 Nov 2018 21:34] Bradley Grainger
Description:
The DbDataReader.GetStream method was added in .NET 4.5: https://docs.microsoft.com/en-us/dotnet/api/system.data.common.dbdatareader.getstream

Any usage of this method seems to throw an IndexOutOfRangeException in Connector/NET:

IndexOutOfRangeException: Data index must be a valid index in the field
   at MySql.Data.MySqlClient.Interceptors.ExceptionInterceptor.Throw(Exception exception)
   at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)
   at MySql.Data.MySqlClient.MySqlDataReader.Throw(Exception ex)
   at MySql.Data.MySqlClient.MySqlDataReader.GetBytes(Int32 i, Int64 fieldOffset, Byte[] buffer, Int32 bufferoffset, Int32 length)
   at System.Data.Common.DbDataReader.GetStream(Int32 ordinal)
   at (program)

How to repeat:
Run the following C# code:

using (var connection = new MySqlConnection("..."))
{
	connection.Open();
	using (var command = new MySqlCommand(@"drop table if exists blob_test; create table blob_test(data blob);", connection))
		command.ExecuteNonQuery();
		
	var length = 1; // any value seems to cause the crash
	using (var command = new MySqlCommand(@"insert into blob_test(data) values(@data);", connection))
	{
		command.Parameters.AddWithValue("@data", new byte[length]);
		command.ExecuteNonQuery();
	}
	
	using (var command = new MySqlCommand(@"select data from blob_test", connection))
	using (var reader = command.ExecuteReader())
	{
		reader.Read();
		var bytes = new byte[length];
		reader.GetBytes(0, 0, bytes, 0, bytes.Length); // works
		using (var stream = reader.GetStream(0)) // throws IndexOutOfRangeException
		{
		}
	}
}
[28 Nov 2018 6:17] MySQL Verification Team
Hello Bradley,

Thank you for the report and test case.
Observed with VS 2017 (C#.Net) and Connector/NET 8.0.13 version.

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

Attempts to execute the GetStream method of the MySqlDataReader class
returned only exceptions. Now, the GetStream(int i) method implementation
returns the expected result for BINARY and GUID type columns.

Thank you for the bug report.