Bug #5750 GetByte(int) throws a IndexOutOfRangeException on valid function call.
Submitted: 26 Sep 2004 8:49 Modified: 31 Jan 2005 21:18
Reporter: Wian Potgieter Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:1.0.0-beta OS:Windows (Windows XP)
Assigned to: Reggie Burnett CPU Architecture:Any

[26 Sep 2004 8:49] Wian Potgieter
Description:
When i run the query : 
<SQL>
SELECT M_Name, M_Password, M_Options, M_NewLogin, M_Theme, M_Language, M_TimeAdjust, M_Verified FROM Members WHERE M_ID = 1 </SQL>
by means of the code:
<CODE>
MySqlConnection dbconn = new MySqlConnection(cstr);
MySqlCommand fff = new MySqlCommand("SELECT M_Name, M_Password, M_Options, M_NewLogin, M_Theme, M_Language, M_TimeAdjust, M_Verified FROM Members WHERE M_ID = 1");
fff.Connection = dbconn;
dbconn.Open();
MySqlDataReader reader = fff.ExecuteReader();
if (reader.HasRows){
	reader.Read();
	Write(reader.GetString(0)+" : field "+reader.GetName(7)+" With GetByte(7): "+reader.GetByte(7)+"<BR>");
}
dbconn.Close();    
</CODE>
I get a IndexOutOfBoundsException thrown, because the code for GetByte is broken.

The 8'th field is an Unsigned TINYINT(3) with value 1 in this example. The problem is the following in file mysqlclient\datareader.cs (Line 196):
		public byte GetByte(int i)
		{
			byte[] buf = fields[i].MySqlValue.ToBytes();
			return buf[i];
		}
If you try to get the i'th column's byte value, and i > buf.Length, it will throw a IndexOutOfRangeException because, as in this example, 7 > TINYINT(3)'s 3 bytes.

How to repeat:
Run a query which has a value at a column position i, where i is greater than the byte size of the value.

Suggested fix:
The problem is what should it return? 
If the selected value is a multi-byte value, then a decision must be made as to which byte must be returned. I would personally opt to return the lowest significant bit. 
I have seached for any specifications that define what to return for this function, but I have not yet found anything.
[26 Sep 2004 8:52] Wian Potgieter
Lowest significant bit should be lowest significant byte. Apologies.
[21 Oct 2004 12:07] Hartmut Holzgraefe
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html