eclipse-vertx / vertx-sql-client

High performance reactive SQL Client written in Java
Apache License 2.0
894 stars 200 forks source link

null check missing for arrays of enums #1470

Open BendixPetersen opened 1 month ago

BendixPetersen commented 1 month ago

Version

4.5.7

Context

I get a ClassCastException (in RowImpl.getArrayOfEnums) when trying to get the value of a nullable array of enums column when the cell value is null.

Do you have a reproducer?

Steps to reproduce

Setup database like this:

CREATE TYPE test_enum AS ENUM(
    'VAL1', 'VAL2'
);

CREATE TABLE test_table
(
    test_column test_enum[]
);

INSERT INTO test_table VALUES (NULL);
  1. try to get the null value with the sql-client.

Extra

It looks to me like there is simply a null check missing in the following function in RowImpl (compare to getEnum). But maybe there is a reason why it was left out.

private Object[] getArrayOfEnums(Class enumType, int pos) {
    Object val = getValue(pos);
    if (val instanceof String[]) {
      String[] array = (String[]) val;
      Object[] ret = (Object[]) Array.newInstance(enumType, array.length);
      for (int i = 0;i < array.length;i++) {
        String string = array[i];
        if (string != null) {
          ret[i] = Enum.valueOf(enumType, string);
        }
      }
      return ret;
    } else if (val instanceof Number[]) {
      Number[] array = (Number[]) val;
      Object[] ret = (Object[]) Array.newInstance(enumType, array.length);
      Object[] constants = enumType.getEnumConstants();
      for (int i = 0;i < array.length;i++) {
        Number number = array[i];
        int ordinal = number.intValue();
        if (ordinal >= 0) {
          if (ordinal < constants.length) {
            ret[i] = constants[ordinal];
          }
        }
      }
      return ret;
    } else {
      throw new ClassCastException();
    }
  }
tsegismont commented 1 month ago

Thanks for reporting this, I'll look into it