mirror of
https://github.com/nlohmann/json.git
synced 2026-08-03 07:52:17 +00:00
validate ndarray element types in write_bjdata_ndarray (#5301)
* validate ndarray element types in write_bjdata_ndarray Signed-off-by: Angadi Yashaswini <angadi@digiscrypt.com> * read ndarray elements through get<> instead of a fixed union member _ArrayType_ names the wire type, not how the value is stored: parsing keeps a non-negative integer as number_unsigned while the C++ API keeps an int literal as number_integer. Selecting the union member from the type marker therefore reads the inactive alternative for one of the two, so read through get<> instead, which dispatches on the active member. Also reject a negative _ArraySize_ entry, which is not a usable dimension, and cover the parse-built path in the tests. Signed-off-by: Angadi Yashaswini <angadi@digiscrypt.com> --------- Signed-off-by: Angadi Yashaswini <angadi@digiscrypt.com>
This commit is contained in:
@@ -1662,7 +1662,15 @@ class binary_writer
|
||||
std::size_t len = (value.at(key).empty() ? 0 : 1);
|
||||
for (const auto& el : value.at(key))
|
||||
{
|
||||
len *= static_cast<std::size_t>(el.m_data.m_value.number_unsigned);
|
||||
// a dimension is read as an unsigned value below, so anything that
|
||||
// is not a non-negative integer is rejected: a non-integer entry
|
||||
// would pun unrelated bytes as the dimension, and a negative one
|
||||
// would wrap into a nonsensical length
|
||||
if (!el.is_number_integer() || (!el.is_number_unsigned() && el.template get<std::int64_t>() < 0))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
len *= static_cast<std::size_t>(el.template get<std::uint64_t>());
|
||||
}
|
||||
|
||||
key = "_ArrayData_";
|
||||
@@ -1671,6 +1679,24 @@ class binary_writer
|
||||
return true;
|
||||
}
|
||||
|
||||
// every element is written below as the number kind dtype names, so it
|
||||
// has to actually be a number of that category: an element of any other
|
||||
// type would reinterpret unrelated bytes, e.g. a string's heap pointer,
|
||||
// as that number. Such an object falls back to a plain object encoding.
|
||||
// dtype names the wire type, not the storage type: whether an integer
|
||||
// is held as number_integer or number_unsigned depends on how the value
|
||||
// was built (parsing stores non-negative integers as unsigned, the C++
|
||||
// API stores int literals as signed), so both are accepted here and the
|
||||
// writes below go through get<>, which reads the member that is active.
|
||||
const bool ndarray_is_float = (dtype == 'd' || dtype == 'D');
|
||||
for (const auto& el : value.at(key))
|
||||
{
|
||||
if (ndarray_is_float ? !el.is_number_float() : !el.is_number_integer())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
oa->write_character('[');
|
||||
oa->write_character('$');
|
||||
oa->write_character(dtype);
|
||||
@@ -1684,70 +1710,70 @@ class binary_writer
|
||||
{
|
||||
for (const auto& el : value.at(key))
|
||||
{
|
||||
write_number(static_cast<std::uint8_t>(el.m_data.m_value.number_unsigned), true);
|
||||
write_number(static_cast<std::uint8_t>(el.template get<std::uint64_t>()), true);
|
||||
}
|
||||
}
|
||||
else if (dtype == 'i')
|
||||
{
|
||||
for (const auto& el : value.at(key))
|
||||
{
|
||||
write_number(static_cast<std::int8_t>(el.m_data.m_value.number_integer), true);
|
||||
write_number(static_cast<std::int8_t>(el.template get<std::int64_t>()), true);
|
||||
}
|
||||
}
|
||||
else if (dtype == 'u')
|
||||
{
|
||||
for (const auto& el : value.at(key))
|
||||
{
|
||||
write_number(static_cast<std::uint16_t>(el.m_data.m_value.number_unsigned), true);
|
||||
write_number(static_cast<std::uint16_t>(el.template get<std::uint64_t>()), true);
|
||||
}
|
||||
}
|
||||
else if (dtype == 'I')
|
||||
{
|
||||
for (const auto& el : value.at(key))
|
||||
{
|
||||
write_number(static_cast<std::int16_t>(el.m_data.m_value.number_integer), true);
|
||||
write_number(static_cast<std::int16_t>(el.template get<std::int64_t>()), true);
|
||||
}
|
||||
}
|
||||
else if (dtype == 'm')
|
||||
{
|
||||
for (const auto& el : value.at(key))
|
||||
{
|
||||
write_number(static_cast<std::uint32_t>(el.m_data.m_value.number_unsigned), true);
|
||||
write_number(static_cast<std::uint32_t>(el.template get<std::uint64_t>()), true);
|
||||
}
|
||||
}
|
||||
else if (dtype == 'l')
|
||||
{
|
||||
for (const auto& el : value.at(key))
|
||||
{
|
||||
write_number(static_cast<std::int32_t>(el.m_data.m_value.number_integer), true);
|
||||
write_number(static_cast<std::int32_t>(el.template get<std::int64_t>()), true);
|
||||
}
|
||||
}
|
||||
else if (dtype == 'M')
|
||||
{
|
||||
for (const auto& el : value.at(key))
|
||||
{
|
||||
write_number(static_cast<std::uint64_t>(el.m_data.m_value.number_unsigned), true);
|
||||
write_number(el.template get<std::uint64_t>(), true);
|
||||
}
|
||||
}
|
||||
else if (dtype == 'L')
|
||||
{
|
||||
for (const auto& el : value.at(key))
|
||||
{
|
||||
write_number(static_cast<std::int64_t>(el.m_data.m_value.number_integer), true);
|
||||
write_number(el.template get<std::int64_t>(), true);
|
||||
}
|
||||
}
|
||||
else if (dtype == 'd')
|
||||
{
|
||||
for (const auto& el : value.at(key))
|
||||
{
|
||||
write_number(static_cast<float>(el.m_data.m_value.number_float), true);
|
||||
write_number(static_cast<float>(el.template get<double>()), true);
|
||||
}
|
||||
}
|
||||
else if (dtype == 'D')
|
||||
{
|
||||
for (const auto& el : value.at(key))
|
||||
{
|
||||
write_number(static_cast<double>(el.m_data.m_value.number_float), true);
|
||||
write_number(el.template get<double>(), true);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user