From 706c8cce4e28083fc763c8d3b2c001c90fb81518 Mon Sep 17 00:00:00 2001 From: prasad-sawantdesai Date: Fri, 22 May 2026 12:14:40 +0200 Subject: [PATCH 1/2] raise exception when rank mismatch instead of segfaulting --- src/hdf5/hdf5_reader.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/hdf5/hdf5_reader.cpp b/src/hdf5/hdf5_reader.cpp index a4a553b9..3b0f1720 100644 --- a/src/hdf5/hdf5_reader.cpp +++ b/src/hdf5/hdf5_reader.cpp @@ -1288,6 +1288,18 @@ int HDF5Reader::readPersistentShapes_Get(Context *ctx, hid_t gid, const std::str new_data_set->open(tensorized_path.c_str(), gid, &dataset_id, 1, nullptr, alconst::integer_data, true, true, dec->getURI()); if (!(new_data_set->dataset_id >= 0)) throw ALBackendException("HDF5Backend: unexpected dataset_id value in HDF5Reader::readPersistentShapes_Get()", LOG); + + { + int shape_rank = new_data_set->getRank(); + int expected_rank = static_cast(current_arrctx_indices.size()) + 1; + if (shape_rank != expected_rank) { + throw ALBackendException( + "HDF5Backend: SHAPE dataset '" + tensorized_path + + "' has rank " + std::to_string(shape_rank) + + " but rank " + std::to_string(expected_rank) + " was expected. ", + LOG); + } + } *dataset_id_shapes = dataset_id; int dim = -1; auto hsSelectionReader = std::unique_ptr(new HDF5HsSelectionReader(new_data_set->getRank(), new_data_set->dataset_id, @@ -1325,6 +1337,22 @@ int HDF5Reader::readPersistentShapes_GetSlice(Context *ctx, DataEntryContext *dec = getDataEntryContext(ctx); std::unique_ptr new_data_set(new HDF5DataSetHandler(false, dec->getURI())); new_data_set->open(tensorized_path.c_str(), gid, &dataset_id, 1, nullptr, alconst::integer_data, true, true, dec->getURI()); + + // Validate _SHAPE dataset rank before any index arithmetic. + { + int shape_rank = new_data_set->getRank(); + int expected_rank = static_cast(aos_indices.size()) + 1; + if (shape_rank != expected_rank) { + throw ALBackendException( + "HDF5Backend: SHAPE dataset '" + tensorized_path + + "' has rank " + std::to_string(shape_rank) + + " but rank " + std::to_string(expected_rank) + " was expected. " + "The file was likely written by a direct HDF5/h5py writer or an " + "incompatible imas-python version that uses a different _SHAPE rank " + "convention. Re-generate the file using a supported imas-python release.", + LOG); + } + } int dim = -1; auto hsSelectionReader = std::unique_ptr(new HDF5HsSelectionReader(new_data_set->getRank(), dataset_id, new_data_set->getDataSpace(), new_data_set->getLargestDims(), alconst::integer_data, aos_indices.size(), &dim)); From dfa8dab9c4e96dae6e06291efa1efca666226f8a Mon Sep 17 00:00:00 2001 From: prasad-sawantdesai Date: Fri, 22 May 2026 12:22:12 +0200 Subject: [PATCH 2/2] fixed message --- src/hdf5/hdf5_reader.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/hdf5/hdf5_reader.cpp b/src/hdf5/hdf5_reader.cpp index 3b0f1720..fb24b394 100644 --- a/src/hdf5/hdf5_reader.cpp +++ b/src/hdf5/hdf5_reader.cpp @@ -1338,7 +1338,6 @@ int HDF5Reader::readPersistentShapes_GetSlice(Context *ctx, std::unique_ptr new_data_set(new HDF5DataSetHandler(false, dec->getURI())); new_data_set->open(tensorized_path.c_str(), gid, &dataset_id, 1, nullptr, alconst::integer_data, true, true, dec->getURI()); - // Validate _SHAPE dataset rank before any index arithmetic. { int shape_rank = new_data_set->getRank(); int expected_rank = static_cast(aos_indices.size()) + 1; @@ -1346,10 +1345,7 @@ int HDF5Reader::readPersistentShapes_GetSlice(Context *ctx, throw ALBackendException( "HDF5Backend: SHAPE dataset '" + tensorized_path + "' has rank " + std::to_string(shape_rank) - + " but rank " + std::to_string(expected_rank) + " was expected. " - "The file was likely written by a direct HDF5/h5py writer or an " - "incompatible imas-python version that uses a different _SHAPE rank " - "convention. Re-generate the file using a supported imas-python release.", + + " but rank " + std::to_string(expected_rank) + " was expected. ", LOG); } }