Bug #38276 Short circuit evaluation error in MySqlCommand.CheckState()
Submitted: 22 Jul 2008 5:02 Modified: 25 Jul 2008 9:59
Reporter: Andrew Laughlin Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:5.1.6 OS:Any
Assigned to: CPU Architecture:Any
Tags: NullReferenceException

[22 Jul 2008 5:02] Andrew Laughlin
Description:
In the following code, short circuit evaluation fails when connection == null.  A NullReferenceException is thrown and not the expected InvalidOperationException.

if ((connection == null || connection.State != ConnectionState.Open) && !connection.SoftClosed)
	throw new InvalidOperationException("Connection must be valid and open");

How to repeat:
DbCommand command = DbCommandFactory.GetDbCommand();
command.CommandType = System.Data.CommandType.Text;
command.CommandText = "select table_name from tables where table_schema = '" + db.DatabaseName.ToLower() + "'";
//command.Connection = connection; // The error occurs when this line is commented.

DataTable dataTable = new DataTable();                                   
dataTable.Load( command.ExecuteReader( CommandBehavior.SingleResult ) );	// NullReferenceException thrown here.

Suggested fix:
if( connection == null )
{
    throw new InvalidOperationException( "Null Connection specified." );
}

if( connection.State != ConnectionState.Open || connection.SoftClosed )
{
    throw new InvalidOperationException("Connection must be open");
}
[22 Jul 2008 6:24] Tonci Grgin
Hi Andrew and thanks for your report. I must say I agree with you completely.

Verified as described, same code present in latest sources too.
[22 Jul 2008 13:51] 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/50196
[22 Jul 2008 13:52] Reggie Burnett
Fixed in 5.1.7 and 5.2.3+
[25 Jul 2008 9:59] Tony Bedford
An entry was added to the 5.1.7 and 5.2.3 changelogs:

There was a short circuit evaluation error in the MySqlCommand.CheckState() method. When the statement connection == null was true a NullReferenceException was thrown and not the expected InvalidOperationException.