Bug #46844 NullReferenceException thrown in MySQLPacket when opening connection
Submitted: 20 Aug 2009 22:34 Modified: 9 Sep 2009 9:47
Reporter: Joel Dolisy Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.0.4 OS:Windows (Not version specific)
Assigned to: Vladislav Vaintroub CPU Architecture:Any

[20 Aug 2009 22:34] Joel Dolisy
Description:
When an error happens during the connection to a MySQL server (in my case remotely), when the error message is being deserialized from the packet buffer, a NullReferenceException is being thrown.

How to repeat:
Try to run the TableEditor sample and access a remote machine that does not have access to a remote MySQL server (for instance an error 1130 happen when connecting).
When the method MySqlPacket::ReadString() method executes to retrieve the error message, the following line will throw an exception:
string s = encoding.GetString(bits, (int)buffer.Position, end - (int)buffer.Position);

This is due to the fact that the encoding field has not been initialized properly.

Suggested fix:
The MySqlPacket instance is created by the MySqlStream constructor, but that constructor uses the MySqlPacket default constructor, which does not initialize the encoding field. All the MySqlStream constructor are passed an encoding argument. The constructor should instead call the packet's constructor that takes an encoding argument.

So for instance the folling code in MySqlStream.cs:
...
        MySqlPacket packet = new MySqlPacket();
        public MySqlStream(Encoding encoding)
	{
  	    // we have no idea what the real value is so we start off with the max value
...

should be changed to the following:
...
        MySqlPacket packet;

	public MySqlStream(Encoding encoding)
	{
            packet = new MySqlPacket(encoding);
...
[21 Aug 2009 8:54] Tonci Grgin
Hi Joel and thanks for your report.

I managed to raise all sorts of errors (but not 1130) connecting to remote server and the behavior of TableEditor test was always correct.

Can you give more details on how to set up privileges for this to happen?
[24 Aug 2009 13:14] Joel Dolisy
The remote database I was trying to connect to did not have any bind-address entry defined in the my.cnf. That database is embedded within a product that I'm trying to integrate with from another application.
The skip-networking entry was commented out as well.
[1 Sep 2009 14:02] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/82140

761 Vladislav Vaintroub	2009-09-01
      Ensure that MySqlPacket always has a valid encoding. This prevents null reference exceptions in ReadString() (bug#46844)
[9 Sep 2009 9:47] Tony Bedford
An entry was added to the 6.0.5 and 6.1.2 changelogs:

If an error occurred during connection to a MySQL Server, deserializing the error message from the packet buffer caused a NullReferenceException to be thrown. When the method MySqlPacket::ReadString() attempted to retrieve the error message, the following line of code threw the exception:

        string s = encoding.GetString(bits, (int)buffer.Position, end - (int)buffer.Position);
      
This was due to the fact that the encoding field had not been initialized correctly.