Permalink
Browse files

fix: Binary handling of empty arrays.

The type of the empty array was not properly read as a result
getting the type of the array caused an NPE
fixes Issue #421 reported by Juha Komulainen
  • Loading branch information...
davecramer committed Nov 10, 2015
1 parent 2ad367e commit 38d8488559b4bbc5dcb7f87f542cd23d457b761d
Showing with 48 additions and 6 deletions.
  1. +20 −5 org/postgresql/jdbc2/AbstractJdbc2Array.java
  2. +28 −1 org/postgresql/test/jdbc2/ArrayTest.java
@@ -264,15 +264,30 @@ private ResultSet readBinaryResultSet(int index, int count) throws SQLException
}
List rows = new ArrayList();
Field[] fields = new Field[2];
if (dimensions > 0) {
storeValues(rows, fields, elementOid, dims, pos, 0, index);
}
storeValues(rows, fields, elementOid, dims, pos, 0, index);
BaseStatement stat = (BaseStatement) connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
return stat.createDriverResultSet(fields, rows);
}
private int storeValues(List rows, Field[] fields, int elementOid, final int[] dims, int pos, final int thisDimension, int index) throws SQLException {
if (thisDimension == dims.length - 1) {
private int storeValues(List rows, Field[] fields, int elementOid, final int[] dims, int pos, final int thisDimension, int index) throws SQLException
{
// handle an empty array
if (dims.length == 0)
{
fields[0] = new Field("INDEX", Oid.INT4);
fields[0].setFormat(Field.BINARY_FORMAT);
fields[1] = new Field("VALUE", elementOid);
fields[1].setFormat(Field.BINARY_FORMAT);
for (int i = 1; i < index; ++i) {
int len = ByteConverter.int4(fieldBytes, pos); pos += 4;
if (len != -1) {
pos += len;
}
}
}
else if (thisDimension == dims.length - 1) {
fields[0] = new Field("INDEX", Oid.INT4);
fields[0].setFormat(Field.BINARY_FORMAT);
fields[1] = new Field("VALUE", elementOid);
@@ -413,6 +413,33 @@ public void testNonStandardDelimiter() throws SQLException
assertTrue(!arrRS.next());
}
public void testEmptyBinaryArray() throws SQLException
{
PreparedStatement pstmt = conn.prepareStatement("SELECT '{}'::int[]");
// force binary
((org.postgresql.PGStatement)pstmt).setPrepareThreshold(-1);
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
Array array = rs.getArray(1);
if (!rs.wasNull())
{
ResultSet ars = array.getResultSet();
try
{
ars.getMetaData().getColumnType(1); // this throws NPE
}
catch (NullPointerException ex)
{
assertTrue("should not get here",true);
}
}
}
}
}
}

0 comments on commit 38d8488

Please sign in to comment.