Check array get for < 0
authorMarc Horowitz <mhorowitz@fb.com>
Wed, 17 Jun 2015 18:06:45 +0000 (11:06 -0700)
committerSara Golemon <sgolemon@fb.com>
Fri, 19 Jun 2015 02:30:11 +0000 (19:30 -0700)
Summary: dynamic's integer type is signed, so make sure array indices
are not negative.

(See https://our.intern.facebook.com/intern/tasks/?t=7445055)

Reviewed By: @Gownta

Differential Revision: D2145689

folly/dynamic.cpp
folly/test/DynamicTest.cpp

index 26d1751658af5cb83981aab0292cc888531e7ad1..840c5f336ab7b180db51a484a738d49e466bc80a 100644 (file)
@@ -185,7 +185,7 @@ dynamic const& dynamic::at(dynamic const& idx) const {
     if (!idx.isInt()) {
       throw TypeError("int64", idx.type());
     }
-    if (idx >= parray->size()) {
+    if (idx < 0 || idx >= parray->size()) {
       throw std::out_of_range("out of range in dynamic array");
     }
     return (*parray)[idx.asInt()];
index aa43c0a9b2c6dc0f0a99e41dc27dd8a17677c73d..e471d021fce8d9387c42731089e7d54acf6035dd 100644 (file)
@@ -157,6 +157,7 @@ TEST(Dynamic, ArrayBasics) {
   EXPECT_EQ(array.at(1), 2);
   EXPECT_EQ(array.at(2), 3);
 
+  EXPECT_ANY_THROW(array.at(-1));
   EXPECT_ANY_THROW(array.at(3));
 
   array.push_back("foo");