Bug #72000 java.lang.ArrayIndexOutOfBoundsException on java.sql.ResultSet.getInt(String)
Submitted: 10 Mar 2014 20:55 Modified: 3 Apr 2014 22:15
Reporter: Peter Palmreuther Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:5.1.29 OS:Any
Assigned to: Filipe Silva CPU Architecture:Any

[10 Mar 2014 20:55] Peter Palmreuther
Description:
When trying to get an integer from a column value that's not a number instead of expected "NumberFormatException" an "ArrayIndexOutOfBoundsException" is thrown due to Off-By-One bug.

How to repeat:
CallableStatement call = c.prepareCall("SELECT ' ' AS FOO");
result = call.executeQuery();
result.next();
result.getInt("FOO");

results in

java.lang.ArrayIndexOutOfBoundsException: 1
	at com.mysql.jdbc.StringUtils.getInt(StringUtils.java:734)
	at com.mysql.jdbc.StringUtils.getInt(StringUtils.java:803)
	at com.mysql.jdbc.ByteArrayRow.getInt(ByteArrayRow.java:106)
	at com.mysql.jdbc.ResultSetImpl.getIntWithOverflowCheck(ResultSetImpl.java:7047)
	at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2732)
	at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2847)

Suggested fix:
Switch conditions for skipping white space:

/* Skip white space. */
while (Character.isWhitespace((char) buf[s]) && (s < endPos)) {
    ++s;
}

should be

/* Skip white space. */
while ((s < endPos) && Character.isWhitespace((char) buf[s])) {
    ++s;
}
[14 Mar 2014 10:25] Filipe Silva
Hi Peter,

Thank you for this bug report.
Verified as described.
[3 Apr 2014 22:15] Daniel So
Added an entry to the Connector/J 5.1.31 changelog:

"When trying to get an integer from a column value that was not a number, instead of a NumberFormatException , an ArrayIndexOutOfBoundsException was thrown instead due to an off-by-one error. This fix corrects the stop condition for trimming spaces in the beginning of byte buffers in the getShort(), getInt(), and getLong() methods in the StringUtils class."