Check array bounds when dereferencing JSON Pointer containing array index

This commit is contained in:
Tristan Penman 2015-05-07 09:35:28 +10:00
parent b3b94fb86b
commit 3900596a72
2 changed files with 72 additions and 14 deletions

View File

@ -83,7 +83,12 @@ inline AdapterType resolveJsonPointer(
typedef typename AdapterType::Array Array;
typename Array::const_iterator itr = node.asArray().begin();
// TODO: Check for array bounds
if (index > node.asArray().size() - 1) {
throw std::runtime_error("Expected reference token to identify "
"an element in the current array, but array index is "
"out of bounds; actual token: " + referenceToken);
}
itr.advance(index);
// Recursively process the remaining tokens

View File

@ -104,20 +104,73 @@ std::vector<boost::shared_ptr<JsonPointerTestCase> >
testCase->expectedValue = NULL;
testCases.push_back(testCase);
rapidjson::Value testArray;
testArray.SetArray();
testArray.PushBack("test0", allocator);
testArray.PushBack("test1", allocator);
testArray.PushBack("test2", allocator);
{
rapidjson::Value testArray;
testArray.SetArray();
testArray.PushBack("test0", allocator);
testArray.PushBack("test1", allocator);
testArray.PushBack("test2", allocator);
testCase = boost::make_shared<JsonPointerTestCase>(
"Resolve '/test/1' in object containing one member containing "
"an array with 3 elements");
testCase->value.SetObject();
testCase->value.AddMember("test", testArray, allocator);
testCase->jsonPointer = "/test/1";
testCase->expectedValue = &testCase->value.FindMember("test")->value[1];
testCases.push_back(testCase);
testCase = boost::make_shared<JsonPointerTestCase>(
"Resolve '/test/0' in object containing one member containing "
"an array with 3 elements");
testCase->value.SetObject();
testCase->value.AddMember("test", testArray, allocator);
testCase->jsonPointer = "/test/0";
testCase->expectedValue = &testCase->value.FindMember("test")->value[size_t(0)];
testCases.push_back(testCase);
}
{
rapidjson::Value testArray;
testArray.SetArray();
testArray.PushBack("test0", allocator);
testArray.PushBack("test1", allocator);
testArray.PushBack("test2", allocator);
testCase = boost::make_shared<JsonPointerTestCase>(
"Resolve '/test/1' in object containing one member containing "
"an array with 3 elements");
testCase->value.SetObject();
testCase->value.AddMember("test", testArray, allocator);
testCase->jsonPointer = "/test/1";
testCase->expectedValue = &testCase->value.FindMember("test")->value[1];
testCases.push_back(testCase);
}
{
rapidjson::Value testArray;
testArray.SetArray();
testArray.PushBack("test0", allocator);
testArray.PushBack("test1", allocator);
testArray.PushBack("test2", allocator);
testCase = boost::make_shared<JsonPointerTestCase>(
"Resolve '/test/2' in object containing one member containing "
"an array with 3 elements");
testCase->value.SetObject();
testCase->value.AddMember("test", testArray, allocator);
testCase->jsonPointer = "/test/2";
testCase->expectedValue = &testCase->value.FindMember("test")->value[2];
testCases.push_back(testCase);
}
{
rapidjson::Value testArray;
testArray.SetArray();
testArray.PushBack("test0", allocator);
testArray.PushBack("test1", allocator);
testArray.PushBack("test2", allocator);
testCase = boost::make_shared<JsonPointerTestCase>(
"Resolveing '/test/3' in object containing one member containing "
"an array with 3 elements should throw an exception");
testCase->value.SetObject();
testCase->value.AddMember("test", testArray, allocator);
testCase->jsonPointer = "/test/3";
testCase->expectedValue = NULL;
testCases.push_back(testCase);
}
return testCases;
}