Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check we don't overflow when casting down integers during parsing #4353

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions include/nlohmann/detail/conversions/from_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
n = nullptr;
}

template <typename BasicJsonType, typename ArithmeticTypeTarget, typename ArithmeticTypeSource>
ArithmeticTypeTarget static_cast_check_range(const BasicJsonType& j)
{
const ArithmeticTypeSource val = *j.template get_ptr<ArithmeticTypeSource*>();
if constexpr (sizeof(ArithmeticTypeTarget) < sizeof(ArithmeticTypeSource) || std::is_floating_point<ArithmeticTypeSource>::value)
{
const ArithmeticTypeSource min =
std::is_signed<ArithmeticTypeSource>::value ? std::numeric_limits<ArithmeticTypeTarget>::lowest() : 0;
const ArithmeticTypeSource max = std::numeric_limits<ArithmeticTypeTarget>::max();
bool valIsInf = false;
if constexpr (std::is_floating_point<ArithmeticTypeSource>::value)
{
valIsInf = std::isinf(val);
}
if ((val < min || val > max) && !valIsInf)
{
JSON_THROW(
out_of_range::create(
406,
"value " + std::to_string(val) + " is out of target integer range [" +
std::to_string(std::numeric_limits<ArithmeticTypeTarget>::lowest()) + ", " +
std::to_string(std::numeric_limits<ArithmeticTypeTarget>::max()) + "]", &j));
}
}
return static_cast<ArithmeticTypeTarget>(val);
}


// overloads for basic_json template parameters
template < typename BasicJsonType, typename ArithmeticType,
enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
Expand All @@ -54,17 +82,17 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
{
case value_t::number_unsigned:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
val = static_cast_check_range<BasicJsonType, ArithmeticType, const typename BasicJsonType::number_unsigned_t>(j);
break;
}
case value_t::number_integer:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
val = static_cast_check_range<BasicJsonType, ArithmeticType, const typename BasicJsonType::number_integer_t>(j);
break;
}
case value_t::number_float:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
val = static_cast_check_range<BasicJsonType, ArithmeticType, const typename BasicJsonType::number_float_t>(j);
break;
}

Expand Down Expand Up @@ -343,17 +371,17 @@ inline void from_json(const BasicJsonType& j, ArithmeticType& val)
{
case value_t::number_unsigned:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
val = static_cast_check_range<BasicJsonType, ArithmeticType, const typename BasicJsonType::number_unsigned_t>(j);
break;
}
case value_t::number_integer:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
val = static_cast_check_range<BasicJsonType, ArithmeticType, const typename BasicJsonType::number_integer_t>(j);
break;
}
case value_t::number_float:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
val = static_cast_check_range<BasicJsonType, ArithmeticType, const typename BasicJsonType::number_float_t>(j);
break;
}
case value_t::boolean:
Expand Down
9 changes: 9 additions & 0 deletions tests/src/unit-class_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,15 @@ TEST_CASE("parser class")
CHECK_THROWS_WITH_AS(parser_helper("1.18973e+4932").empty(), "[json.exception.out_of_range.406] number overflow parsing '1.18973e+4932'", json::out_of_range&);
}

SECTION("out-of-range-conversion")
{
// overflows during parsing with casting yield an exception
CHECK_THROWS_WITH_AS(parser_helper("123456").get<int16_t>(), "[json.exception.out_of_range.406] value 123456 is out of target integer range [-32768, 32767]", json::out_of_range&);
CHECK_THROWS_WITH_AS(parser_helper("65536").get<int16_t>(), "[json.exception.out_of_range.406] value 65536 is out of target integer range [-32768, 32767]", json::out_of_range&);
CHECK_THROWS_WITH_AS(parser_helper("-32769").get<int16_t>(), "[json.exception.out_of_range.406] value -32769 is out of target integer range [-32768, 32767]", json::out_of_range&);
CHECK_THROWS_WITH_AS(parser_helper("18446744073709559808").get<uint64_t>(), "[json.exception.out_of_range.406] value 18446744073709559808.000000 is out of target integer range [0, 18446744073709551615]", json::out_of_range&);
}

SECTION("invalid numbers")
{
// numbers must not begin with "+"
Expand Down