json_pointer::array_index: Use unsigned values for the array index when parsing
The current code uses std::stoi to convert the input string to an int array_index. This limits the maximum addressable array size to ~2GB on most platforms. But all callers immediately convert the result of array_index to BasicJsonType::size_type. So let's parse it as unsigned long long, which allows us to have as big arrays as available memory. And also makes the call sites nicer to read. One complication arises on platforms where size_type is smaller than unsigned long long. We need to bail out on these if the parsed array index does not fit into size_type.
This commit is contained in:
parent
f0e73163f2
commit
ecbb2756fd
4 changed files with 69 additions and 34 deletions
|
@ -348,12 +348,29 @@ TEST_CASE("JSON pointers")
|
|||
CHECK_THROWS_WITH(j_const["/1+1"_json_pointer] == 1,
|
||||
"[json.exception.out_of_range.404] unresolved reference token '1+1'");
|
||||
|
||||
CHECK_THROWS_AS(j["/111111111111111111111111"_json_pointer] = 1, json::out_of_range&);
|
||||
CHECK_THROWS_WITH(j["/111111111111111111111111"_json_pointer] = 1,
|
||||
"[json.exception.out_of_range.404] unresolved reference token '111111111111111111111111'");
|
||||
CHECK_THROWS_AS(j_const["/111111111111111111111111"_json_pointer] == 1, json::out_of_range&);
|
||||
CHECK_THROWS_WITH(j_const["/111111111111111111111111"_json_pointer] == 1,
|
||||
"[json.exception.out_of_range.404] unresolved reference token '111111111111111111111111'");
|
||||
{
|
||||
auto too_large_index = std::to_string((std::numeric_limits<unsigned long long>::max)()) + "1";
|
||||
json::json_pointer jp(std::string("/") + too_large_index);
|
||||
std::string throw_msg = std::string("[json.exception.out_of_range.404] unresolved reference token '") + too_large_index + "'";
|
||||
|
||||
CHECK_THROWS_AS(j[jp] = 1, json::out_of_range&);
|
||||
CHECK_THROWS_WITH(j[jp] = 1, throw_msg.c_str());
|
||||
CHECK_THROWS_AS(j_const[jp] == 1, json::out_of_range&);
|
||||
CHECK_THROWS_WITH(j_const[jp] == 1, throw_msg.c_str());
|
||||
}
|
||||
|
||||
if (sizeof(typename json::size_type) < sizeof(unsigned long long))
|
||||
{
|
||||
auto size_type_max_uul = static_cast<unsigned long long>((std::numeric_limits<json::size_type>::max)());
|
||||
auto too_large_index = std::to_string(size_type_max_uul);
|
||||
json::json_pointer jp(std::string("/") + too_large_index);
|
||||
std::string throw_msg = std::string("[json.exception.out_of_range.410] array index ") + too_large_index + " exceeds size_type";
|
||||
|
||||
CHECK_THROWS_AS(j[jp] = 1, json::out_of_range&);
|
||||
CHECK_THROWS_WITH(j[jp] = 1, throw_msg.c_str());
|
||||
CHECK_THROWS_AS(j_const[jp] == 1, json::out_of_range&);
|
||||
CHECK_THROWS_WITH(j_const[jp] == 1, throw_msg.c_str());
|
||||
}
|
||||
|
||||
CHECK_THROWS_AS(j.at("/one"_json_pointer) = 1, json::parse_error&);
|
||||
CHECK_THROWS_WITH(j.at("/one"_json_pointer) = 1,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue