Bug #1695 ConnectorJ throwing ArrayIndexOutOfBoundsException
Submitted: 29 Oct 2003 2:22 Modified: 31 Oct 2003 20:47
Reporter: Paul Bergin Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:3.0.9 OS:Windows (Windows)
Assigned to: Mark Matthews CPU Architecture:Any

[29 Oct 2003 2:22] Paul Bergin
Description:
There is a problem running a query containing the text LIMIT when connecting to mysql 4.1.0 alpha using mysql-connector-java-3.0.7.jar and mysql-connector-java-3.0.9.jar. The followinf exception is thrown

java.sql.SQLException: Error during query: Unexpected Exception:
java.lang.ArrayIndexOutOfBoundsException message given: null
Nested Stack Trace:
** BEGIN NESTED EXCEPTION ** 
java.lang.ArrayIndexOutOfBoundsException
STACKTRACE:
java.lang.ArrayIndexOutOfBoundsException
 at com.mysql.jdbc.Buffer.readFieldLength(Buffer.java:311)
 at com.mysql.jdbc.Buffer.fastSkipLenString(Buffer.java:112)
 at com.mysql.jdbc.MysqlIO.unpackField(MysqlIO.java:381)
 at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:242)
 at com.mysql.jdbc.MysqlIO.readResults(MysqlIO.java:1189)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1150)
 at com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:1114)
 at com.mysql.jdbc.Connection.execSQL(Connection.java:1873)
 at com.mysql.jdbc.Connection.execSQL(Connection.java:1807)
 at com.mysql.jdbc.Statement.executeQuery(Statement.java:1072)

How to repeat:
Create a Statement and limit the results to 250 rows (testStmt.setMaxRows(250)), but if the text "LIMIT" appears anywhere in the statement an ArrayIndexOutOfBoundsException thrown 

e.g.
           try{
                Statement testStmt = con.createStatement();
                testStmt.setMaxRows(250);
          ResultSet testResults = testStmt.executeQuery("SELECT * FROM CUSTOMER_LIMIT);
                .
                .
                .
            }
            catch (SQLException e)
            {
             e.printStackTrace();
            }

Suggested fix:
I looked the mysql-connector-java-3.0.9-stable code in com.mysql.jdbc.Statement.executeQuery(String sql)it looks for the text "LIMIT" I presume it should be " LIMIT "

                if (sql.toUpperCase().indexOf("LIMIT") != -1) {
                    results = connection.execSQL(sql, maxRows,
                            resultSetConcurrency, createStreamingResultSet(),
                            true, this.currentCatalog);
                } else {

This will not totally fix the problem, because if a LIMIT is set in the sql query and it is larger than the setMaxRows(250) an exception is thrown again.

                Statement testStmt = con.createStatement();
                testStmt.setMaxRows(250);
                ResultSet testResults = testStmt.executeQuery("SELECT * FROM CUSTOMER LIMIT 500);

If the resultSet contains 500 it will cause the exception to be thrown again.
[31 Oct 2003 20:47] 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

Thank you for your bug report. This has been fixed in the 3.0 and 3.1 branches of Connector/J. Until a release is made, you can check out the fix from a nightly snapshot (after 00:00 GMT, November 1st) from http://downloads.mysql.com/snapshots.php