Commit bfc003ca authored by chenguoping's avatar chenguoping

catch exceptions for json_point : /xx/+99

parent 0feea616
...@@ -329,6 +329,20 @@ class json_pointer ...@@ -329,6 +329,20 @@ class json_pointer
*/ */
static int array_index(const std::string& s) static int array_index(const std::string& s)
{ {
// error condition (cf. RFC 6901, Sect. 4)
if (JSON_HEDLEY_UNLIKELY(s.size() > 1 and s[0] == '0'))
{
JSON_THROW(detail::parse_error::create(106, 0,
"array index '" + s +
"' must not begin with '0'"));
}
// error condition (cf. RFC 6901, Sect. 4 & Sect. 7)
if (JSON_HEDLEY_UNLIKELY(s.size() > 1 and not (s[0] >= '1' and s[0] <= '9')))
{
JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number"));
}
std::size_t processed_chars = 0; std::size_t processed_chars = 0;
const int res = std::stoi(s, &processed_chars); const int res = std::stoi(s, &processed_chars);
...@@ -474,14 +488,6 @@ class json_pointer ...@@ -474,14 +488,6 @@ class json_pointer
case detail::value_t::array: case detail::value_t::array:
{ {
// error condition (cf. RFC 6901, Sect. 4)
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
{
JSON_THROW(detail::parse_error::create(106, 0,
"array index '" + reference_token +
"' must not begin with '0'"));
}
if (reference_token == "-") if (reference_token == "-")
{ {
// explicitly treat "-" as index beyond the end // explicitly treat "-" as index beyond the end
...@@ -541,14 +547,6 @@ class json_pointer ...@@ -541,14 +547,6 @@ class json_pointer
") is out of range")); ") is out of range"));
} }
// error condition (cf. RFC 6901, Sect. 4)
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
{
JSON_THROW(detail::parse_error::create(106, 0,
"array index '" + reference_token +
"' must not begin with '0'"));
}
// note: at performs range check // note: at performs range check
JSON_TRY JSON_TRY
{ {
...@@ -606,14 +604,6 @@ class json_pointer ...@@ -606,14 +604,6 @@ class json_pointer
") is out of range")); ") is out of range"));
} }
// error condition (cf. RFC 6901, Sect. 4)
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
{
JSON_THROW(detail::parse_error::create(106, 0,
"array index '" + reference_token +
"' must not begin with '0'"));
}
// use unchecked array access // use unchecked array access
JSON_TRY JSON_TRY
{ {
...@@ -665,14 +655,6 @@ class json_pointer ...@@ -665,14 +655,6 @@ class json_pointer
") is out of range")); ") is out of range"));
} }
// error condition (cf. RFC 6901, Sect. 4)
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
{
JSON_THROW(detail::parse_error::create(106, 0,
"array index '" + reference_token +
"' must not begin with '0'"));
}
// note: at performs range check // note: at performs range check
JSON_TRY JSON_TRY
{ {
...@@ -724,14 +706,6 @@ class json_pointer ...@@ -724,14 +706,6 @@ class json_pointer
return false; return false;
} }
// error condition (cf. RFC 6901, Sect. 4)
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
{
JSON_THROW(detail::parse_error::create(106, 0,
"array index '" + reference_token +
"' must not begin with '0'"));
}
JSON_TRY JSON_TRY
{ {
const auto idx = static_cast<size_type>(array_index(reference_token)); const auto idx = static_cast<size_type>(array_index(reference_token));
......
...@@ -10403,6 +10403,20 @@ class json_pointer ...@@ -10403,6 +10403,20 @@ class json_pointer
*/ */
static int array_index(const std::string& s) static int array_index(const std::string& s)
{ {
// error condition (cf. RFC 6901, Sect. 4)
if (JSON_HEDLEY_UNLIKELY(s.size() > 1 and s[0] == '0'))
{
JSON_THROW(detail::parse_error::create(106, 0,
"array index '" + s +
"' must not begin with '0'"));
}
// error condition (cf. RFC 6901, Sect. 4 & Sect. 7)
if (JSON_HEDLEY_UNLIKELY(s.size() > 1 and not (s[0] >= '1' and s[0] <= '9')))
{
JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number"));
}
std::size_t processed_chars = 0; std::size_t processed_chars = 0;
const int res = std::stoi(s, &processed_chars); const int res = std::stoi(s, &processed_chars);
...@@ -10548,14 +10562,6 @@ class json_pointer ...@@ -10548,14 +10562,6 @@ class json_pointer
case detail::value_t::array: case detail::value_t::array:
{ {
// error condition (cf. RFC 6901, Sect. 4)
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
{
JSON_THROW(detail::parse_error::create(106, 0,
"array index '" + reference_token +
"' must not begin with '0'"));
}
if (reference_token == "-") if (reference_token == "-")
{ {
// explicitly treat "-" as index beyond the end // explicitly treat "-" as index beyond the end
...@@ -10615,14 +10621,6 @@ class json_pointer ...@@ -10615,14 +10621,6 @@ class json_pointer
") is out of range")); ") is out of range"));
} }
// error condition (cf. RFC 6901, Sect. 4)
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
{
JSON_THROW(detail::parse_error::create(106, 0,
"array index '" + reference_token +
"' must not begin with '0'"));
}
// note: at performs range check // note: at performs range check
JSON_TRY JSON_TRY
{ {
...@@ -10680,14 +10678,6 @@ class json_pointer ...@@ -10680,14 +10678,6 @@ class json_pointer
") is out of range")); ") is out of range"));
} }
// error condition (cf. RFC 6901, Sect. 4)
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
{
JSON_THROW(detail::parse_error::create(106, 0,
"array index '" + reference_token +
"' must not begin with '0'"));
}
// use unchecked array access // use unchecked array access
JSON_TRY JSON_TRY
{ {
...@@ -10739,14 +10729,6 @@ class json_pointer ...@@ -10739,14 +10729,6 @@ class json_pointer
") is out of range")); ") is out of range"));
} }
// error condition (cf. RFC 6901, Sect. 4)
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
{
JSON_THROW(detail::parse_error::create(106, 0,
"array index '" + reference_token +
"' must not begin with '0'"));
}
// note: at performs range check // note: at performs range check
JSON_TRY JSON_TRY
{ {
...@@ -10798,14 +10780,6 @@ class json_pointer ...@@ -10798,14 +10780,6 @@ class json_pointer
return false; return false;
} }
// error condition (cf. RFC 6901, Sect. 4)
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
{
JSON_THROW(detail::parse_error::create(106, 0,
"array index '" + reference_token +
"' must not begin with '0'"));
}
JSON_TRY JSON_TRY
{ {
const auto idx = static_cast<size_type>(array_index(reference_token)); const auto idx = static_cast<size_type>(array_index(reference_token));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment