Bug #378 ArrayIndexOutOfBoundsException when storing strings with char 0xFFFF
Submitted: 6 May 2003 8:36 Modified: 6 May 2003 10:26
Reporter: [ name withheld ] Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:3.0.7 OS:Linux (Linux, Windows 2000)
Assigned to:

[6 May 2003 8:36] [ name withheld ]
Description:
The JDBC driver throws an ArrayIndexOutOfBoundsException when PreparedStatement.setString() is called with a String containing Unicode char 0xFFFF.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 65535
        at com.mysql.jdbc.SingleByteCharsetConverter.toBytes(SingleByteCharsetConverter.java:160)
        at com.mysql.jdbc.StringUtils.getBytes(StringUtils.java:88)
        at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:1053)
        at Arr.main(Arr.java:9)

This happens with Connector/J 3.0.6 and 3.0.7

How to repeat:
Run this sample program, changing the connection URL/login info:

import java.sql.*;
public class Arr {
	public static void main(String args[]) throws Exception {
		Class.forName("com.mysql.jdbc.Driver");
		Connection con = DriverManager.getConnection("jdbc:mysql://XXXX:3306/XXX", "XXX", "XXX"); 
		PreparedStatement stmt = con.prepareStatement("SELECT ?");
		stmt.setString(1, "hello \uFFFF");
	}
}

Suggested fix:
The private arrays charToByteMap and unknownCharsMap in com.mysql.jdbc.SingleByteCharsetConverter need to be declared with 65536 bytes:

line 40:
    private static byte[] unknownCharsMap = new byte[65536];

line 56:
    private byte[] charToByteMap = new byte[65536];
[6 May 2003 10:26] Mark Matthews
Thank you for your bug report. This issue has been fixed in the latest
development tree for that product. You can find more information about
accessing our development trees at 
    http://www.mysql.com/doc/en/Installing_source_tree.html