# Bug? H5TCopy of empty enum

**URL:** <https://forum.hdfgroup.org/t/bug-h5tcopy-of-empty-enum/9634>\
**Category:** HDF5 Library\
**Created:** [April 6, 2022, 3:56pm UTC](https://forum.hdfgroup.org/t/bug-h5tcopy-of-empty-enum/9634 "2022-04-06T15:56:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicolac76](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/n/5e9695/32.png) [@nicolac76](https://forum.hdfgroup.org/u/nicolac76)\
**Post date:** [April 6, 2022, 3:56pm UTC](https://forum.hdfgroup.org/t/bug-h5tcopy-of-empty-enum/9634/1 "2022-04-06T15:56:42Z")

</div>

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

> <https://github.com/HDFGroup/hdf5/blob/develop/src/H5T.c#L3637-L3638>

  
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](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.

---

<div class="post-metadata">

**Author:** ![nicolac76](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/n/5e9695/32.png) [@nicolac76](https://forum.hdfgroup.org/u/nicolac76)\
**Post date:** [April 7, 2022, 6:47am UTC](https://forum.hdfgroup.org/t/bug-h5tcopy-of-empty-enum/9634/2 "2022-04-07T06:47:10Z")

</div>

Here is a simple example file showing the problem:

```auto
#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
}

```

---

<div class="post-metadata">

**Author:** ![derobins](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/derobins/32/1481_2.png) [@derobins](https://forum.hdfgroup.org/u/derobins)\
**Post date:** [June 9, 2023, 7:25pm UTC](https://forum.hdfgroup.org/t/bug-h5tcopy-of-empty-enum/9634/3 "2023-06-09T19:25:26Z")

</div>

This issue should be fixed in develop via PR #3088

> <https://github.com/HDFGroup/hdf5/pull/3088>
>
> When copying an empty enum type (including implicitly, as when an enum is contai…ned in a compound type), the library would allocate 0-size blocks of memory and attempt to memcpy 0 bytes from NULL pointers, which are undefined behavior. In debug mode, the library would raise an assert in H5MM.
> 
> The library now avoid undefined memory operations when copying empty enum types and a test that copies empty enums has been added.
