HDFGroup / HDFGroup/hdf5

H5Literate2 crash when iterating over a broken file

Open
#5,417 1 comment 0 reactions 1 assignee Assigned to @bmribler View on GitHub
Component - C Library
Dominant language
C
Stars
988
Forks
355
Avg merge
4d 2h
Merged PRs (30d)
12

Description

**Describe the bug**
We had a computer that crashed completely while writing it's data, resulting in a corrupted file. Opening the file and reading it with `H5Literate2` causes the whole application to crash. You can try this yourself with this minimal working example:

```cpp
#include
#include
#include

int indent = 0;

herr_t file_info(hid_t loc_id, const char *name, const H5L_info2_t * /*linfo*/, void * /*opdata*/)
{
auto group = H5Gopen2(loc_id, name, H5P_DEFAULT);

printf("%*sName: %s\n", indent * 4, "", name);

indent++;
H5Literate2(group, H5_INDEX_NAME, H5_ITER_INC, nullptr, file_info, nullptr);
indent--;

H5Gclose(group);
return 0;
}

int main(int argc, char const *argv[])
{
if(argc < 2)
{
std::cerr << "First argument must be file name\n";
return 0;
}
try
{
H5::Exception::dontPrint();
std::string name{argv[1]};
auto file = H5::H5File{name, H5F_ACC_RDONLY};
H5Literate2(file.getId(), H5_INDEX_NAME, H5_ITER_INC, nullptr, file_info, nullptr);
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
return 1;
}
}
```
```cmake
cmake_minimum_required (VERSION 3.18)
project (HDF5MyApp CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)

set(ENV{HDF5_ROOT} "C:\\Program Files\\HDF_Group\\HDF5\\1.14.6")

set (LIB_TYPE STATIC) # or SHARED
string(TOLOWER ${LIB_TYPE} SEARCH_TYPE)

find_package (HDF5 REQUIRED NAMES hdf5 COMPONENTS C CXX ${SEARCH_TYPE})
set_directory_properties(PROPERTIES INCLUDE_DIRECTORIES "${HDF5_INCLUDE_DIR}")

set (example main)

add_executable (${example} ${PROJECT_SOURCE_DIR}/src/${example}.cpp)
target_link_libraries (${example} ${HDF5_CXX_${LIB_TYPE}_LIBRARY})
```

The executable was built in Release mode and the broken file can be downloaded [here](https://www.byte-physics.de/Downloads/hdf5/2024_08_14_123155.nwb) (620MB).

**Expected behavior**
I would expect no crash, but an exception or error message similar to `h5dump`.

**Platform (please complete the following information)**
- HDF5 version: 1.14.6
- OS and version: Windows 11 24H2, 26100.3476
- Compiler and version: MSBuild-Version 17.13.15+18b3035f6 for .NET Framework (printed during build), MSVC 19.43.34808.0 (reported by Cmake)
- Build system (e.g. CMake version) and generator (e.g. XCode, Ninja): CMake 3.31.2, Visual Studio 17 2022 17.13.2
- Any configure options you specified: Release
- MPI library and version (parallel HDF5)

**Additional context**
*none*

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.