Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

null check missing for arrays of enums #1470

Open
BendixPetersen opened this issue Sep 27, 2024 · 1 comment
Open

null check missing for arrays of enums #1470

BendixPetersen opened this issue Sep 27, 2024 · 1 comment
Assignees
Labels
Milestone

Comments

@BendixPetersen
Copy link

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 tsegismont self-assigned this Sep 30, 2024
@tsegismont tsegismont added this to the 4.5.11 milestone Sep 30, 2024
@tsegismont
Copy link
Contributor

Thanks for reporting this, I'll look into it

@vietj vietj modified the milestones: 4.5.11, 4.5.12 Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants