@@ -74,6 +74,8 @@ public enum PostgresType {
7474 // PostgreSQL-compatible datetime format (ISO 8601 without 'T' separator)
7575 private static final String POSTGRES_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSSSSS" ;
7676 private static final DateTimeFormatter POSTGRES_DATETIME_FORMATTER = DateTimeFormatter .ofPattern (POSTGRES_TIMESTAMP_FORMAT );
77+ // PostgreSQL caps array dimensions at 6 (MAXDIM in pg_config_manual.h).
78+ private static final int MAX_ARRAY_DIMENSIONS = 6 ;
7779
7880 public final int code ;
7981 public final Class <?> cls ;
@@ -509,15 +511,73 @@ private static Object deserializeBinary(final long code, final byte[] valueAsByt
509511 buffer .get (bytes );
510512 yield new JSONObject (new String (bytes ));
511513 }
512- case ARRAY_INT , ARRAY_LONG , ARRAY_DOUBLE , ARRAY_REAL , ARRAY_TEXT , ARRAY_BOOLEAN , ARRAY_CHAR , ARRAY_JSON -> {
513- // For binary format, would need to implement proper array binary deserialization
514- // This is a simplified placeholder - proper implementation would need to handle
515- // array dimensions and element deserialization according to PostgreSQL protocol
516- throw new PostgresProtocolException ("Binary deserialization for arrays not yet implemented" );
517- }
514+ case ARRAY_INT , ARRAY_LONG , ARRAY_DOUBLE , ARRAY_REAL , ARRAY_TEXT , ARRAY_BOOLEAN , ARRAY_CHAR , ARRAY_JSON ->
515+ deserializeBinaryArray (buffer );
518516 };
519517 }
520518
519+ private static List <Object > deserializeBinaryArray (ByteBuffer buffer ) {
520+ final int ndim = buffer .getInt (); // number of dimensions
521+ buffer .getInt (); // hasnull flag (unused)
522+ final int elemOid = buffer .getInt (); // element type OID
523+
524+ if (ndim == 0 )
525+ return new ArrayList <>();
526+
527+ if (ndim < 0 || ndim > MAX_ARRAY_DIMENSIONS )
528+ throw new PostgresProtocolException ("Invalid array dimension count: " + ndim );
529+
530+ long totalElements = 1L ;
531+ for (int d = 0 ; d < ndim ; d ++) {
532+ final int dimSize = buffer .getInt ();
533+ buffer .getInt (); // lower bound (unused)
534+ if (dimSize < 0 )
535+ throw new PostgresProtocolException ("Negative array dimension size: " + dimSize );
536+ totalElements *= dimSize ;
537+ if (totalElements > Integer .MAX_VALUE )
538+ throw new PostgresProtocolException ("Array element count exceeds Integer.MAX_VALUE" );
539+ }
540+
541+ // Each element occupies at least 4 bytes (the length prefix), so an element count
542+ // exceeding remaining/4 cannot fit and signals a malformed or malicious payload.
543+ if (totalElements > buffer .remaining () / 4L )
544+ throw new PostgresProtocolException ("Array element count exceeds remaining buffer bytes" );
545+
546+ final int count = (int ) totalElements ;
547+ final ArrayList <Object > result = new ArrayList <>(count );
548+ for (int i = 0 ; i < count ; i ++) {
549+ final int elemLen = buffer .getInt ();
550+ if (elemLen == -1 ) {
551+ result .add (null );
552+ } else {
553+ final byte [] elemBytes = new byte [elemLen ];
554+ buffer .get (elemBytes );
555+ result .add (deserializeBinaryElement (elemOid , elemBytes ));
556+ }
557+ }
558+ return result ;
559+ }
560+
561+ private static Object deserializeBinaryElement (final int elemOid , final byte [] bytes ) {
562+ final ByteBuffer buf = ByteBuffer .wrap (bytes );
563+ if (elemOid == BOOLEAN .code )
564+ return buf .get () != 0 ;
565+ if (elemOid == LONG .code )
566+ return buf .getLong ();
567+ // int2[] (OID 1005) is not in the ARRAY_* enum, but int2 elements can appear inside
568+ // any array (e.g. composite types), so we still decode them as Short here.
569+ if (elemOid == SMALLINT .code )
570+ return buf .getShort ();
571+ if (elemOid == INTEGER .code )
572+ return buf .getInt ();
573+ if (elemOid == REAL .code )
574+ return buf .getFloat ();
575+ if (elemOid == DOUBLE .code )
576+ return buf .getDouble ();
577+ // text/varchar/bpchar/json/unknown - raw bytes are already the UTF-8 string content.
578+ return new String (bytes , DatabaseFactory .getDefaultCharset ());
579+ }
580+
521581 /**
522582 * Checks if this type is an array type.
523583 */
0 commit comments