Bug #26660 MySqlConnection.GetSchema fails with NullReferenceException for Foreign Keys
Submitted: 27 Feb 2007 1:58 Modified: 9 Mar 2007 11:54
Reporter: Jeff M Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:5.0.3 OS:Windows (Windows Vista Business)
Assigned to: CPU Architecture:Any
Tags: GetSchema, restrictions

[27 Feb 2007 1:58] Jeff M
Description:
In most cases, attempting to retrieve foreign key schema for a connection results in a NullReferenceException.  This does not happen if a restrictions array containing at least 4 elements is passed to the overloaded GetSchema function.

How to repeat:
Runnign the following code will result in a NullReferenceException:

'Assumes an initialized connection object 'mySqlConn'
Dim fkeySchema As DataTable = mySqlConn.GetSchema("Foreign Keys")

Suggested fix:
I dug around in the source code and it looks like the null reference is
coming from the usage of the 'restrictions' array in
SchemaProvider.GetForeignKeys.  The problem is that the function
assumes that the array will be able to access an item at index 3.  In
many cases the array is Null or does not have enough elements.  The code should check if the array is null or has a length >= 4 before assigning a value at index 3.

A temporary workaround to this problem from a client's perspective is
to explicitly populate the restrictions array with at least 4 elements:

Dim schema As DataTable = mysqlconn.GetSchema("Foreign Keys", New
String() {Nothing, Nothing, Nothing, Nothing})

This may be a problem in other SchemaProvider functions (as they use
restrictions as well) but I did not look over any of them.
[27 Feb 2007 20:06] 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/20687
[27 Feb 2007 20:06] Reggie Burnett
Fixed in 5.0.4
[9 Mar 2007 11:54] MC Brown
A note has been added to the 5.0.4 changelog.