diff --git a/include/boost/json/impl/pointer.ipp b/include/boost/json/impl/pointer.ipp index d07c48082..0004c85d7 100644 --- a/include/boost/json/impl/pointer.ipp +++ b/include/boost/json/impl/pointer.ipp @@ -200,15 +200,19 @@ parse_number_token( return {}; } - std::size_t new_result = result * 10 + d; - if( new_result < result ) + if( result > std::size_t(-1) / 10 ) { BOOST_JSON_FAIL(ec, error::token_overflow); return {}; } + result *= 10; - result = new_result; - + if( result > std::size_t(-1) - d ) + { + BOOST_JSON_FAIL(ec, error::token_overflow); + return {}; + } + result += d; } return result; } diff --git a/test/pointer.cpp b/test/pointer.cpp index ef4c12bb2..6b658befc 100644 --- a/test/pointer.cpp +++ b/test/pointer.cpp @@ -231,6 +231,14 @@ class pointer_test jv.find_pointer(s, ec); BOOST_TEST(ec == error::token_overflow); BOOST_TEST(hasLocation(ec)); + + // a token an order of magnitude past max() still overflows size_t + string s2 = "/foo/"; + s2 += std::to_string((std::numeric_limits::max)()); + s2 += '9'; + jv.find_pointer(s2, ec); + BOOST_TEST(ec == error::token_overflow); + BOOST_TEST(hasLocation(ec)); } void