Commit c636ccbe authored by Nick Terrell's avatar Nick Terrell Committed by facebook-github-bot-4

Add bounds check in get_ptr.

Summary: `get_ptr()` checks if the index is past the end of the array, but it
doesn't check if it is less than 0, while `at()` does.

Reviewed By: @yfeldblum

Differential Revision: D2258548
parent 031ed38a
......@@ -165,7 +165,7 @@ const dynamic* dynamic::get_ptr(dynamic const& idx) const {
if (!idx.isInt()) {
throw TypeError("int64", idx.type());
}
if (idx >= parray->size()) {
if (idx < 0 || idx >= parray->size()) {
return nullptr;
}
return &(*parray)[idx.asInt()];
......
......@@ -269,6 +269,7 @@ TEST(Dynamic, ObjectForwarding) {
TEST(Dynamic, GetPtr) {
dynamic array = { 1, 2, "three" };
EXPECT_TRUE(array.get_ptr(0));
EXPECT_FALSE(array.get_ptr(-1));
EXPECT_FALSE(array.get_ptr(3));
EXPECT_EQ(dynamic("three"), *array.get_ptr(2));
const dynamic& carray = array;
......
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