Bug #24599 Possible NPE in com.mysql.jdbc.Connection, isSameResource.
Submitted: 26 Nov 2006 16:12 Modified: 31 Dec 2007 11:11
Reporter: Nils Hammar Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.0.4 OS:Any (Any)
Assigned to: CPU Architecture:Any

[26 Nov 2006 16:12] Nils Hammar
Description:
A NullPointerException may occur on line 4251 in com.mysql.jdbc.Connection if otherHost is null.

Code:
	} else if (otherHost != null & otherHost.indexOf(",") == -1 && 

How to repeat:
Compiler warning.

Suggested fix:
Proposed code:
	} else if (otherHost != null && otherHost.indexOf(",") == -1 && 

the use of '&&' will use a lazy evaluation.
[27 Nov 2006 14:51] Tonci Grgin
Hi Nils and thanks for your problem report.
Please do not refer to line numbers as I'm obliged to test on latest sources. Your line is actually now at 4304...
Anyway, I was not able to get reported compiler warnings with latest c/J 5 sources (Ant and Eclipse).
[28 Nov 2006 18:36] Nils Hammar
Sorry about the linenumber, it was only to provide a rough location.

A more thorough explanation:
Using double ampersands "&&" in an evaluation will allow the compiler to take a short-cut by avoiding to evaluate the rest of the statements in the condition. By using a single "&" both sides of the conditions will be evaluated and then AND:ed together. However the condition:

		} else if (otherHost != null & otherHost.indexOf(",") == -1 && 

will cause a NullPointerException in this case whenever otherHost is 'null', and that will be avoided if "&&" is used instead!
[31 Dec 2007 11:11] Tonci Grgin
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 bug fix. More information about accessing the source trees is available at

    http://dev.mysql.com/doc/en/installing-source.html

Hi Nils. In my source repository I see this fixed as you suggested:
		if (!nullSafeCompare(otherHost, this.origHostToConnectTo)) {
			directCompare = false;
		} else if (otherHost != null && otherHost.indexOf(",") == -1 && 
				otherHost.indexOf(":") == -1) {
Closing the report now.