Skip to content

Commit c19c8d3

Browse files
Add checks for empty HDF5 datasets in readArrayNdOfValues
Enhanced the readArrayNdOfValues method to check if the target HDF5 dataset is empty before attempting to read, throwing exceptions for empty or inaccessible datasets. Also improved error handling for dataspace and dataset closing operations.
1 parent 6eb532c commit c19c8d3

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

src/eml2/HdfProxy.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ void HdfProxy::close()
254254
}
255255
}
256256

257-
void HdfProxy::readArrayNdOfValues(const std::string & datasetName, void* values, hid_t datatype)
257+
void HdfProxy::readArrayNdOfValues(const std::string& datasetName, void* values, hid_t datatype)
258258
{
259259
if (!isOpened()) {
260260
open();
@@ -264,9 +264,33 @@ void HdfProxy::readArrayNdOfValues(const std::string & datasetName, void* values
264264
if (dataset < 0) {
265265
throw invalid_argument("The HDF5 dataset " + datasetName + " could not be opened.");
266266
}
267+
268+
// check if the dataset is empty
269+
hid_t dataspace = H5Dget_space(dataset);
270+
if (dataspace < 0) {
271+
H5Dclose(dataset);
272+
throw invalid_argument("The dataspace for the dataset " + datasetName + " could not be opened.");
273+
}
274+
hssize_t hdf5ValueCount = H5Sget_simple_extent_npoints(dataspace);
275+
if (hdf5ValueCount < 0) {
276+
H5Sclose(dataspace);
277+
H5Dclose(dataset);
278+
throw invalid_argument("The number of values of the dataset " + datasetName + " could not be read.");
279+
}
280+
else if (hdf5ValueCount == 0) {
281+
H5Sclose(dataspace);
282+
H5Dclose(dataset);
283+
throw invalid_argument("The dataset " + datasetName + " is empty and consequently cannot be read.");
284+
}
285+
if (H5Sclose(dataspace) < 0) {
286+
throw invalid_argument("Cannot close the HDF5 dataspace of the HDF5 dataset " + datasetName);
287+
}
288+
267289
hid_t readingError = H5Dread (dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, values);
268290

269-
H5Dclose(dataset);
291+
if (H5Dclose(dataset) < 0) {
292+
throw invalid_argument("Cannot close the HDF5 dataset " + datasetName);
293+
}
270294
if (readingError < 0) {
271295
throw invalid_argument("The HDF5 dataset " + datasetName + " could not be read.");
272296
}

0 commit comments

Comments
 (0)