Bug? H5TCopy of empty enum

If we create an empty enum and try to copy this type, it fails.
Because of:


nalloc is 0, so the allocation returns an empty pointer.

In our usecase this was silently failing and way after it is segfaulting.
We were trying to create a compound type containing an empty enum.
It works and fails later when trying to use the Compound Type.
https://github.com/HDFGroup/hdf5/blob/develop/src/H5Tcompound.c#L476 The error here is silently ignored (type is 0).

So, is it normal it fails for this reason, or should it be fixed?
If it is a normal failing should we look to make the error happened earlier.

Here is a simple example file showing the problem:

#include <stdio.h>
#include <hdf5.h>

int main()
{
   hid_t enum_id = H5Tenum_create(H5Tcopy(H5T_NATIVE_INT));
   if (enum_id <= 0) {
       fprintf(stderr, "Error when creating enum\n");
       return -1;
   }
   hsize_t int_size = H5Tget_size(H5T_NATIVE_LONG);
   hid_t compound_id = H5Tcreate(H5T_COMPOUND, int_size * 2);
   if (compound_id <= 0) {
       fprintf(stderr, "Error when creating a compound type");
       return -1;
   }
   herr_t err = H5Tinsert(compound_id, "int", 0, H5T_NATIVE_LONG);
   if (err < 0) {
       fprintf(stderr, "Error when inserting into compound type\n");
       return -1;
   }
   err = H5Tinsert(compound_id, "enum", int_size, enum_id);
   if (err < 0) {
       fprintf(stderr, "Error when inserting into compound type\n");
       return -1;
    }
    hid_t new_compound = H5Tcopy(compound_id); // throw
}

This issue should be fixed in develop via PR #3088

3 Likes