Skip to content

Commit

Permalink
Check type of attribute before attempting modification (before castin…
Browse files Browse the repository at this point in the history
…g the pointer).
  • Loading branch information
pnorbert committed Aug 18, 2021
1 parent 057cfda commit 4205973
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
40 changes: 9 additions & 31 deletions source/adios2/core/Attribute.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,10 @@ void Attribute<T>::Modify(const T *data, const size_t elements)
{
if (m_AllowModification)
{
if (this->m_Type == helper::GetDataType<T>())
{
m_DataArray = std::vector<T>(data, data + elements);
Pad<T>::Zero(m_DataSingleValue);
this->m_IsSingleValue = false;
this->m_Elements = elements;
}
else
{
throw std::invalid_argument(
"ERROR: modifiable attribute " + this->m_Name +
" has been defined with type " + ToString(this->m_Type) +
". Type cannot be changed to " +
ToString(helper::GetDataType<T>()) + "\n");
}
m_DataArray = std::vector<T>(data, data + elements);
Pad<T>::Zero(m_DataSingleValue);
this->m_IsSingleValue = false;
this->m_Elements = elements;
}
else
{
Expand All @@ -126,22 +115,11 @@ void Attribute<T>::Modify(const T &data)
{
if (m_AllowModification)
{
if (this->m_Type == helper::GetDataType<T>())
{
m_DataArray.clear();
Pad<T>::Zero(m_DataSingleValue);
m_DataSingleValue = data;
this->m_IsSingleValue = true;
this->m_Elements = 1;
}
else
{
throw std::invalid_argument(
"ERROR: modifiable attribute " + this->m_Name +
" has been defined with type " + ToString(this->m_Type) +
". Type cannot be changed to " +
ToString(helper::GetDataType<T>()) + "\n");
}
m_DataArray.clear();
Pad<T>::Zero(m_DataSingleValue);
m_DataSingleValue = data;
this->m_IsSingleValue = true;
this->m_Elements = 1;
}
else
{
Expand Down
48 changes: 36 additions & 12 deletions source/adios2/core/IO.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,25 @@ Attribute<T> &IO::DefineAttribute(const std::string &name, const T &value,
if (helper::ValueToString(value) !=
itExistingAttribute->second->GetInfo()["Value"])
{
Attribute<T> &a =
static_cast<Attribute<T> &>(*itExistingAttribute->second);
a.Modify(value);
for (auto &e : m_Engines)
if (itExistingAttribute->second->m_Type == helper::GetDataType<T>())
{
e.second->NotifyEngineAttribute(
globalName, itExistingAttribute->second->m_Type);
Attribute<T> &a =
static_cast<Attribute<T> &>(*itExistingAttribute->second);
a.Modify(value);
for (auto &e : m_Engines)
{
e.second->NotifyEngineAttribute(
globalName, itExistingAttribute->second->m_Type);
}
}
else
{
throw std::invalid_argument(
"ERROR: modifiable attribute " + globalName +
" has been defined with type " +
ToString(itExistingAttribute->second->m_Type) +
". Type cannot be changed to " +
ToString(helper::GetDataType<T>()) + "\n");
}
}
return static_cast<Attribute<T> &>(*itExistingAttribute->second);
Expand Down Expand Up @@ -176,13 +188,25 @@ IO::DefineAttribute(const std::string &name, const T *array,

if (itExistingAttribute->second->GetInfo()["Value"] != arrayValues)
{
Attribute<T> &a =
static_cast<Attribute<T> &>(*itExistingAttribute->second);
a.Modify(array, elements);
for (auto &e : m_Engines)
if (itExistingAttribute->second->m_Type == helper::GetDataType<T>())
{
Attribute<T> &a =
static_cast<Attribute<T> &>(*itExistingAttribute->second);
a.Modify(array, elements);
for (auto &e : m_Engines)
{
e.second->NotifyEngineAttribute(
globalName, itExistingAttribute->second->m_Type);
}
}
else
{
e.second->NotifyEngineAttribute(
globalName, itExistingAttribute->second->m_Type);
throw std::invalid_argument(
"ERROR: modifiable attribute " + globalName +
" has been defined with type " +
ToString(itExistingAttribute->second->m_Type) +
". Type cannot be changed to " +
ToString(helper::GetDataType<T>()) + "\n");
}
}
return static_cast<Attribute<T> &>(*itExistingAttribute->second);
Expand Down

0 comments on commit 4205973

Please sign in to comment.