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

fix #1982:json_pointer.contains() exception is incorrectly raised #2019

Merged
merged 2 commits into from
May 2, 2020
Merged
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
21 changes: 21 additions & 0 deletions include/nlohmann/detail/json_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,27 @@ class json_pointer
// "-" always fails the range check
return false;
}
if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 and not ("0" <= reference_token and reference_token <= "9")))
{
// invalid char
return false;
}
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
{
if (JSON_HEDLEY_UNLIKELY(not ('1' <= reference_token[0] and reference_token[0] <= '9')))
{
// first char should be between '1' and '9'
return false;
}
for (std::size_t i = 1; i < reference_token.size(); i++)
{
if (JSON_HEDLEY_UNLIKELY(not ('0' <= reference_token[i] and reference_token[i] <= '9')))
{
// other char should be between '0' and '9'
return false;
}
}
}

const auto idx = static_cast<size_type>(array_index(reference_token));
if (idx >= ptr->size())
Expand Down
21 changes: 21 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11081,6 +11081,27 @@ class json_pointer
// "-" always fails the range check
return false;
}
if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 and not ("0" <= reference_token and reference_token <= "9")))
{
// invalid char
return false;
}
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
{
if (JSON_HEDLEY_UNLIKELY(not ('1' <= reference_token[0] and reference_token[0] <= '9')))
{
// first char should be between '1' and '9'
return false;
}
for (std::size_t i = 1; i < reference_token.size(); i++)
{
if (JSON_HEDLEY_UNLIKELY(not ('0' <= reference_token[i] and reference_token[i] <= '9')))
{
// other char should be between '0' and '9'
return false;
}
}
}

const auto idx = static_cast<size_type>(array_index(reference_token));
if (idx >= ptr->size())
Expand Down
21 changes: 9 additions & 12 deletions test/src/unit-json_pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,11 @@ TEST_CASE("JSON pointers")
CHECK_THROWS_AS(j_const.at("/01"_json_pointer), json::parse_error&);
CHECK_THROWS_WITH(j_const.at("/01"_json_pointer),
"[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'");
CHECK_THROWS_AS(j.contains("/01"_json_pointer), json::parse_error&);
CHECK_THROWS_WITH(j.contains("/01"_json_pointer),
"[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'");
CHECK_THROWS_AS(j_const.contains("/01"_json_pointer), json::parse_error&);
CHECK_THROWS_WITH(j_const.contains("/01"_json_pointer),
"[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'");

CHECK(not j.contains("/01"_json_pointer));
CHECK(not j.contains("/01"_json_pointer));
CHECK(not j_const.contains("/01"_json_pointer));
CHECK(not j_const.contains("/01"_json_pointer));

// error with incorrect numbers
CHECK_THROWS_AS(j["/one"_json_pointer] = 1, json::parse_error&);
Expand Down Expand Up @@ -360,12 +359,10 @@ TEST_CASE("JSON pointers")
CHECK_THROWS_WITH(j_const.at("/one"_json_pointer) == 1,
"[json.exception.parse_error.109] parse error: array index 'one' is not a number");

CHECK_THROWS_AS(j.contains("/one"_json_pointer), json::parse_error&);
CHECK_THROWS_WITH(j.contains("/one"_json_pointer),
"[json.exception.parse_error.109] parse error: array index 'one' is not a number");
CHECK_THROWS_AS(j_const.contains("/one"_json_pointer), json::parse_error&);
CHECK_THROWS_WITH(j_const.contains("/one"_json_pointer),
"[json.exception.parse_error.109] parse error: array index 'one' is not a number");
CHECK(not j.contains("/one"_json_pointer));
CHECK(not j.contains("/one"_json_pointer));
CHECK(not j_const.contains("/one"_json_pointer));
CHECK(not j_const.contains("/one"_json_pointer));

CHECK_THROWS_AS(json({{"/list/0", 1}, {"/list/1", 2}, {"/list/three", 3}}).unflatten(), json::parse_error&);
CHECK_THROWS_WITH(json({{"/list/0", 1}, {"/list/1", 2}, {"/list/three", 3}}).unflatten(),
Expand Down