Bug #77649 URL start with word "address",JDBC can't parse the "host:port" Correctly
Submitted: 8 Jul 2015 5:48 Modified: 13 Sep 2016 21:49
Reporter: guo Miner Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:since 5.1.34, 5.1.33, 5.1.35, 5.1.36 OS:Any
Assigned to: Filipe Silva CPU Architecture:Any
Tags: JDBC parseHostPortPair "address"

[8 Jul 2015 5:48] guo Miner
Description:
When the database connection url start with the word "address",the JDBC Driver can't parse the "host:port" Correctly.
For example: 
 my db connection url is :addressXXX.xxx.com:3306,JDBC Driver will report:
Caused by: java.net.UnknownHostException: addressXXX.xxx.com:3306
	at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
	at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:883)
	at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1236)
	at java.net.InetAddress.getAllByName0(InetAddress.java:1187)
	at java.net.InetAddress.getAllByName(InetAddress.java:1117)
	at java.net.InetAddress.getAllByName(InetAddress.java:1053)
	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:190)
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
	

How to repeat:
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class tmpjdbc {
	public static void main(String[] args) throws SQLException, ClassNotFoundException, UnknownHostException, SocketException {
		

		Class.forName("com.mysql.jdbc.Driver");

		Properties pro = new Properties();
		pro.setProperty("user", "XXX");
		pro.setProperty("password", "XXX");

		Driver driver = new com.mysql.jdbc.Driver();

		Connection connection1 = driver.connect("jdbc:mysql://addxxx.com:3306/?zeroDateTimeBehavior=convertToNull", pro);
		Statement statement1 = connection1.createStatement();

		ResultSet _result = statement1.executeQuery("select 1");
		while (_result.next()) {
			System.out.println(_result.getInt(1));
		}

		connection1.close();
	}
}

Suggested fix:
change the method parseHostPortPair of  com/mysql/jdbc/NonRegisteringDriver.java  from

protected static String[] parseHostPortPair(String hostPortPair) throws SQLException {

        String[] splitValues = new String[2];

        if (StringUtils.startsWithIgnoreCaseAndWs(hostPortPair, "address")) {
            splitValues[HOST_NAME_INDEX] = hostPortPair.trim();
            splitValues[PORT_NUMBER_INDEX] = null;

            return splitValues;
        }

.....

to

protected static String[] parseHostPortPair(String hostPortPair) throws SQLException {

        String[] splitValues = new String[2];

        if (StringUtils.startsWithIgnoreCaseAndWs(hostPortPair, "address=")) {
            splitValues[HOST_NAME_INDEX] = hostPortPair.trim();
            splitValues[PORT_NUMBER_INDEX] = null;

            return splitValues;
        }
.....
[8 Jul 2015 5:49] guo Miner
SORRY ,repeat code should like this:
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class tmpjdbc {
	public static void main(String[] args) throws SQLException, ClassNotFoundException, UnknownHostException, SocketException {
		

		Class.forName("com.mysql.jdbc.Driver");

		Properties pro = new Properties();
		pro.setProperty("user", "appusr");
		pro.setProperty("password", "appusr");

		Driver driver = new com.mysql.jdbc.Driver();

		Connection connection1 = driver.connect("jdbc:mysql://addressxxx.com:3306/?zeroDateTimeBehavior=convertToNull", pro);
		Statement statement1 = connection1.createStatement();

		ResultSet _result = statement1.executeQuery("select 1");
		while (_result.next()) {
			System.out.println(_result.getInt(1));
		}

		connection1.close();
	}
}
[8 Jul 2015 8:03] MySQL Verification Team
Hello guo Miner,

Thank you for the report and test case.

Thanks,
Umesh
[13 Sep 2016 21:49] Daniel So
Posted by developer:
 
Added the following entry to the C/J 5.1.40 changelog:

"Connector/J could not parse the host name and port number from connection URLS staring with the word “address,” resulting in an UnknownHostException. This was because the URLs were being interpreted incorrectly as using the alternate URL format, which starts with “address=” This patch fixes the parser for proper interpretation of the URL in the situation. "