Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-650] NegativeArraySizeException while executing getObject(colum…
…nName, byte[].class) with null value
  • Loading branch information
rusher committed Oct 4, 2018
1 parent 93535f2 commit da51b94
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 42 deletions.
Expand Up @@ -1331,42 +1331,31 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
return (T) row.getInternalString(col, null, timeZone);

} else if (type.equals(Integer.class)) {
if (row.lastValueWasNull()) {
return null;
}
if (row.lastValueWasNull()) return null;
return (T) (Integer) row.getInternalInt(col);

} else if (type.equals(Long.class)) {
if (row.lastValueWasNull()) {
return null;
}
if (row.lastValueWasNull()) return null;
return (T) (Long) row.getInternalLong(col);

} else if (type.equals(Short.class)) {
if (row.lastValueWasNull()) {
return null;
}
if (row.lastValueWasNull()) return null;
return (T) (Short) row.getInternalShort(col);

} else if (type.equals(Double.class)) {
if (row.lastValueWasNull()) {
return null;
}
if (row.lastValueWasNull()) return null;
return (T) (Double) row.getInternalDouble(col);

} else if (type.equals(Float.class)) {
if (row.lastValueWasNull()) {
return null;
}
if (row.lastValueWasNull()) return null;
return (T) (Float) row.getInternalFloat(col);

} else if (type.equals(Byte.class)) {
if (row.lastValueWasNull()) {
return null;
}
if (row.lastValueWasNull()) return null;
return (T) (Byte) row.getInternalByte(col);

} else if (type.equals(byte[].class)) {
if (row.lastValueWasNull()) return null;
byte[] data = new byte[row.getLengthMaxFieldSize()];
System.arraycopy(row.buf, row.pos, data, 0, row.getLengthMaxFieldSize());
return (T) data;
Expand All @@ -1381,6 +1370,7 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
return (T) row.getInternalTimestamp(col, null, timeZone);

} else if (type.equals(Boolean.class)) {
if (row.lastValueWasNull()) return null;
return (T) (Boolean) row.getInternalBoolean(col);

} else if (type.equals(Calendar.class)) {
Expand All @@ -1393,22 +1383,16 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
return type.cast(calendar);

} else if (type.equals(Clob.class) || type.equals(NClob.class)) {
if (row.lastValueWasNull()) {
return null;
}
if (row.lastValueWasNull()) return null;
return (T) new MariaDbClob(row.buf, row.pos, row.getLengthMaxFieldSize());

} else if (type.equals(InputStream.class)) {
if (row.lastValueWasNull()) {
return null;
}
if (row.lastValueWasNull()) return null;
return (T) new ByteArrayInputStream(row.buf, row.pos, row.getLengthMaxFieldSize());

} else if (type.equals(Reader.class)) {
String value = row.getInternalString(col, null, timeZone);
if (value == null) {
return null;
}
if (value == null) return null;
return (T) new StringReader(value);

} else if (type.equals(BigDecimal.class)) {
Expand All @@ -1428,9 +1412,7 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
} else if (type.equals(ZonedDateTime.class)) {
ZonedDateTime zonedDateTime = row
.getInternalZonedDateTime(col, ZonedDateTime.class, timeZone);
if (zonedDateTime == null) {
return null;
}
if (zonedDateTime == null) return null;
return type.cast(row.getInternalZonedDateTime(col, ZonedDateTime.class, timeZone));

} else if (type.equals(OffsetDateTime.class)) {
Expand All @@ -1440,30 +1422,22 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {

} else if (type.equals(OffsetDateTime.class)) {
LocalDate localDate = row.getInternalLocalDate(col, timeZone);
if (localDate == null) {
return null;
}
if (localDate == null) return null;
return type.cast(localDate);

} else if (type.equals(LocalDate.class)) {
LocalDate localDate = row.getInternalLocalDate(col, timeZone);
if (localDate == null) {
return null;
}
if (localDate == null) return null;
return type.cast(localDate);

} else if (type.equals(LocalTime.class)) {
LocalTime localTime = row.getInternalLocalTime(col, timeZone);
if (localTime == null) {
return null;
}
if (localTime == null) return null;
return type.cast(localTime);

} else if (type.equals(OffsetTime.class)) {
OffsetTime offsetTime = row.getInternalOffsetTime(col, timeZone);
if (offsetTime == null) {
return null;
}
if (offsetTime == null) return null;
return type.cast(offsetTime);

}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/mariadb/jdbc/BooleanTest.java
Expand Up @@ -170,6 +170,7 @@ private void checkBooleanValue(ResultSet rs, boolean expectedValue, Boolean expe
assertEquals(expectedValue, rs.getBoolean(i));
if (i == 1 || i == 2) {
assertEquals(expectedNull, rs.getObject(i));
assertEquals(expectedNull, rs.getObject(i, Boolean.class));
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/mariadb/jdbc/DatatypeTest.java
Expand Up @@ -52,6 +52,7 @@

package org.mariadb.jdbc;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -1070,6 +1071,25 @@ public void testBitValues() throws SQLException {
}
}

/**
* CONJ-650 : NegativeArraySizeException while executing getObject(columnName, byte[].class) with null value
*
* @throws SQLException if any exception occur.
*/
@Test
public void testNullGetObject() throws SQLException {
createTable("testTextNullValue", "id int, val text");
Statement stmt = sharedConnection.createStatement();
stmt.execute(
"INSERT INTO testTextNullValue VALUES (1, 'abc'), (2, null)");
ResultSet rs = stmt.executeQuery("SELECT * FROM testTextNullValue");
assertTrue(rs.next());
assertArrayEquals(rs.getObject(2, byte[].class), "abc".getBytes());
assertTrue(rs.next());
assertNull(rs.getObject(2, byte[].class));

}

private void validResultSetBitValue(ResultSet rs) throws SQLException {
assertTrue(rs.next());

Expand Down

0 comments on commit da51b94

Please sign in to comment.