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
if (!idx.isInt()) {
throw TypeError("int64", idx.type());
}
- if (idx >= parray->size()) {
+ if (idx < 0 || idx >= parray->size()) {
return nullptr;
}
return &(*parray)[idx.asInt()];
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;