Bug #1387 Connector/J SingleByteCharsetConverter Bug
Submitted: 23 Sep 2003 20:54 Modified: 26 Sep 2003 7:21
Reporter: [ name withheld ] Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:3.0.8 OS:Linux (Linux)
Assigned to: Mark Matthews CPU Architecture:Any

[23 Sep 2003 20:54] [ name withheld ]
Description:
The JDBC connector implements it's own character encoding to improve performance, instead of using the default Java routines. However, it does not seem to do it correctly. 

The connector builds arrays to map bytes from one character set to another. However, as it is building the table, it never provides a mapping for byte 127. Instead 127 is always mapped to 63, the default mapping.

The problem is in the routine below. Note that the size of the array is determined as "Byte.MAX_VALUE - Byte.MIN_VALUE". The value of this is 127 - -128 = 255. The correct value should by 256. This is not a problem for most sql statements, unless you are trying to write binary to the database. Then byte 127 (ascii DEL) is always corrupted to 63 (ascii ?).

    private SingleByteCharsetConverter(String encodingName)
        throws UnsupportedEncodingException {
	String allBytesString = new String(allBytes, 0,
	               Byte.MAX_VALUE - Byte.MIN_VALUE, encodingName);
        int allBytesLen = allBytesString.length();

        System.arraycopy(unknownCharsMap, 0, charToByteMap, 0,
            charToByteMap.length);

        for (int i = 0;
		(i < (Byte.MAX_VALUE - Byte.MIN_VALUE)) && (i < allBytesLen);
		i++) {
            char c = allBytesString.charAt(i);
            byteToChars[i] = c;
            charToByteMap[c] = allBytes[i];
        }
    }

How to repeat:
Try writing 127 to a text column in the database. Then read it back. It will read as 63.

Suggested fix:
Replace all occurances of "Byte.MAX_VALUE - Byte.MIN_VALUE" with 256.
[26 Sep 2003 7:21] Mark Matthews
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 bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

This was fixed a while back in the source tree. Please try a nightly snapshot of 3.0 from http://mmmysql.sourceforge.net/snapshots/stable/