Skip to content

Commit

Permalink
WIP: safely check size_t -> integer conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
scottwittenburg committed Aug 18, 2023
1 parent 3416176 commit 7e3b840
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions source/adios2/engine/hdf5/HDF5ReaderP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include "adios2/helper/adiosFunctions.h" //CSVToVector
#include "adios2/helper/adiosFunctions.h" //IsHDF5
#include <limits>
#include <stdexcept>
#include <vector>

namespace adios2
Expand Down Expand Up @@ -183,7 +185,14 @@ size_t HDF5ReaderP::ReadDataset(hid_t dataSetId, hid_t h5Type, Variable<T> &vari
if (ret < 0)
return 0;

hid_t memDataSpace = H5Screate_simple(ndims, count.data(), NULL);
if (ndims > std::numeric_limits<int>::max())
{
helper::Throw<std::overflow_error>("Engine", "HDF5ReaderP", "ReadDataset",
"Number of dimensions is too large to be "
"represented by an int");
}

hid_t memDataSpace = H5Screate_simple(static_cast<int>(ndims), count.data(), NULL);
interop::HDF5TypeGuard g_mds(memDataSpace, interop::E_H5_SPACE);

int elementsRead = 1;
Expand Down Expand Up @@ -261,9 +270,17 @@ void HDF5ReaderP::UseHDFRead(Variable<T> &variable, T *data, hid_t h5Type)

while (ts < variable.m_StepsCount)
{
size_t adiosStep = variableStart + ts;
if (adiosStep > std::numeric_limits<int>::max())
{
helper::Throw<std::overflow_error>("Engine", "HDF5ReaderP", "UseHDFRead",
"Timestep value is too large to be "
"represented by an int");
}

try
{
m_H5File.SetAdiosStep(variableStart + ts);
m_H5File.SetAdiosStep(static_cast<int>(adiosStep));
}
catch (std::exception &e)
{
Expand Down

0 comments on commit 7e3b840

Please sign in to comment.