Unidata / thredds

THREDDS Data Server v4.6
https://www.unidata.ucar.edu/software/tds/v4.6/index.html
265 stars 179 forks source link

ClassCastException thrown when trying to read a ucar.nc2.Variable that is an empty ucar.nc2.Structure or a member of an empty Structure #1212

Open elzbietaa opened 5 years ago

elzbietaa commented 5 years ago

CCE-problem-report.zip

Summary:

The following ClassCastException is thrown when trying to read data from an empty Structure:

java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object;

    at ucar.ma2.Array.factory(Array.java:181)
    at ucar.ma2.Array.factory(Array.java:160)
    at ucar.nc2.iosp.hdf5.H5iosp.readData(H5iosp.java:155)
    at ucar.nc2.iosp.hdf5.H5iosp.readData(H5iosp.java:141)
    at ucar.nc2.NetcdfFile.readData(NetcdfFile.java:2020)
    at ucar.nc2.Variable.reallyRead(Variable.java:874)
    at ucar.nc2.Variable._read(Variable.java:845)
    at ucar.nc2.Variable.read(Variable.java:723)
    at ucar.nc2.dataset.StructureDS.reallyRead(StructureDS.java:232)
    at ucar.nc2.Variable._read(Variable.java:845)
    at ucar.nc2.Variable.read(Variable.java:723)
    at ucar.nc2.NetcdfFile.readArrays(NetcdfFile.java:2142)
    ...

Notes:

Example test cases highlighting the issue are:

1) For an empty structure:

    @Test
    public void testReadEmptyStructure() throws IOException {
        URL ncfile = getClass().getClassLoader()
                .getResource("psenterprise/gsa/netcdf/odnc4_empty_structures.nc");
        String variablePath = "/observations";
        try (NetcdfDataset dataSet = NetcdfDataset.acquireDataset(null, ncfile.toString(),
                EnumSet.noneOf(NetcdfDataset.Enhance.class), -1, null, null)) {
            Variable var = dataSet.findVariable(variablePath);
            assertThat(var, is(notNullValue()));
            dataSet.readArrays(ImmutableList.of(var));
        }
    }

2) For a member of an empty structure:

    @Test
    public void testReadEmptyStructure() throws IOException {
        URL ncfile = getClass().getClassLoader()
                .getResource("psenterprise/gsa/netcdf/odnc4_empty_structures.nc");
        String variablePath = "/observations.temperature";
        try (NetcdfDataset dataSet = NetcdfDataset.acquireDataset(null, ncfile.toString(),
                EnumSet.noneOf(NetcdfDataset.Enhance.class), -1, null, null)) {
            Variable var = dataSet.findVariable(variablePath);
            assertThat(var, is(notNullValue()));
            dataSet.readArrays(ImmutableList.of(var));
        }
    }   
lesserwhirls commented 5 years ago

@DennisHeimbigner - can you take a look at this one?

DennisHeimbigner commented 5 years ago

ok

DennisHeimbigner commented 5 years ago

It turns out this issue is related to this one: https://github.com/Unidata/thredds/issues/1211#issuecomment-460380193 Apparently when an array is created whose size is zero, some piece of code (probably in H5Iosp) decides that the Array needs a fill value, and creates one if not specified. later, when H5Iop.readData is called, it attempts to return that fillvalue as the data. This is probably because (as noted in the other issue) the size is one, not zero. In any case, the created fillvalue is an empty byte[] object. When an attempt is made to convert to Object[], it fails as noted on a ClassCastException. Not sure yet, what the proper solution is.

elzbietaa commented 5 years ago

Just note that the issue occurs only for empty Structures or members of an empty Structure. With empty ucar.nc2.dataset.VariableDS no exception is thrown.

The following test passes without problems:

    @Test
    public void testReadAVariableDs() throws IOException {
        URL ncfile = getClass().getClassLoader()
                .getResource("psenterprise/gsa/netcdf/odnc4_empty_structures.nc");
        String variablePath = "/elevation";
        try (NetcdfDataset dataSet = NetcdfDataset.acquireDataset(null, ncfile.toString(),
                EnumSet.noneOf(NetcdfDataset.Enhance.class), -1, null, null)) {
            Variable var = dataSet.findVariable(variablePath);
            assertThat(var, is(notNullValue()));
            dataSet.readArrays(ImmutableList.of(var));
        }
    }