Bug #22628 NonRegisteringDriver throws NPE for malformed JDBC URL
Submitted: 23 Sep 2006 12:23 Modified: 22 Feb 2007 14:35
Reporter: Rob Manning Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.0.3 OS:Microsoft Windows (WinXP)
Assigned to: Mark Matthews CPU Architecture:Any

[23 Sep 2006 12:23] Rob Manning
Description:
When database component and slash ("/") tokens are missing from the jdbc 
URL, the method NonRegisteringDriver.getPropertyInfo throws an NPE.

When I run the specified code I get the following exception:

java.lang.NullPointerException
        at com.mysql.jdbc.NonRegisteringDriver.getPropertyInfo(NonRegisteringDriver.java:349)
        at NonRegisteringDriverNPETest.main(NonRegisteringDriverNPETest.java:42)

I also see this behavior in mysql-connector-java-3.1.12-bin.jar, so it looks
like it has been around for a while.

How to repeat:
Run the following code:

import java.sql.Driver;
import java.util.Properties;

public class NonRegisteringDriverNPETest {

    public static void main(String[] args) throws Exception {
        String className1 = "com.mysql.jdbc.Driver";
        String className2 = "org.gjt.mm.mysql.Driver";
        
        // Testing class com.mysql.jdbc.Driver
        System.out.println("Testing class "+className1);
        Driver driverInst = 
            (Driver) Class.forName(className1).newInstance();
        Properties info = new Properties();
        // This works
        System.out.println("Getting driver properties with correct URL");
        driverInst.getPropertyInfo("jdbc:mysql://localhost:3306/test", info);

        try {
            System.out.println("Getting driver properties with malformed URL");
            // This throws NPE
            driverInst.getPropertyInfo("jdbc:mysql://localhost:3306", info);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        // Testing class org.gjt.mm.mysql.Driver
        System.out.println("Testing class "+className2);
        driverInst = (Driver) Class.forName(className2).newInstance();
        
        // This works
        System.out.println("Getting driver properties with correct URL");
        driverInst.getPropertyInfo("jdbc:mysql://localhost:3306/test", info);
        
        System.out.println("Getting driver properties with malformed URL");
        // This throws NPE
        try {
            driverInst.getPropertyInfo("jdbc:mysql://localhost:3306", info);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

}

Suggested fix:
Test for missing slash "/" and database tokens after port token before assuming
it is there in com.mysql.jdbc.NonRegisteringDriver.getPropertyInfo()
[22 Oct 2006 14:53] Tonci Grgin
Hi Rob and thanks for your problem report.
Verified as described by reporter with his test case.
 - MySQL server 5.0.27BK on WinXP Pro SP2 localhost
 - JDK 1.5.07
 - c/J 5.0.3GA

...
Getting driver properties with malformed URL
java.lang.NullPointerException
        at com.mysql.jdbc.NonRegisteringDriver.getPropertyInfo(NonRegisteringDriver.java:349)
        at bug22628.Main.main(Main.java:53)
java.lang.NullPointerException
        at com.mysql.jdbc.NonRegisteringDriver.getPropertyInfo(NonRegisteringDriver.java:349)
        at bug22628.Main.main(Main.java:77)
[13 Feb 2007 22:13] 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/19807
[22 Feb 2007 14:35] MC Brown
A note has been added to the 5.0.5 changelog.