| Index: core/fpdfapi/parser/cpdf_object_unittest.cpp
|
| diff --git a/core/fpdfapi/parser/cpdf_object_unittest.cpp b/core/fpdfapi/parser/cpdf_object_unittest.cpp
|
| index 64dc8c63ecfb5e04d2e07d039130c1172b6085a3..8215226ef3919ef2fdcac3abaf60bf179f66554a 100644
|
| --- a/core/fpdfapi/parser/cpdf_object_unittest.cpp
|
| +++ b/core/fpdfapi/parser/cpdf_object_unittest.cpp
|
| @@ -21,6 +21,11 @@
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| namespace {
|
| +
|
| +using ScopedArray = std::unique_ptr<CPDF_Array, ReleaseDeleter<CPDF_Array>>;
|
| +using ScopedDict =
|
| + std::unique_ptr<CPDF_Dictionary, ReleaseDeleter<CPDF_Dictionary>>;
|
| +using ScopedStream = std::unique_ptr<CPDF_Stream, ReleaseDeleter<CPDF_Stream>>;
|
|
|
| void TestArrayAccessors(const CPDF_Array* arr,
|
| size_t index,
|
| @@ -168,12 +173,14 @@
|
| }
|
|
|
| protected:
|
| + using ScopedObj = std::unique_ptr<CPDF_Object, ReleaseDeleter<CPDF_Object>>;
|
| +
|
| // m_ObjHolder needs to be declared first and destructed last since it also
|
| // refers to some objects in m_DirectObjs.
|
| std::unique_ptr<CPDF_IndirectObjectHolder> m_ObjHolder;
|
| - std::vector<std::unique_ptr<CPDF_Object>> m_DirectObjs;
|
| + std::vector<ScopedObj> m_DirectObjs;
|
| std::vector<int> m_DirectObjTypes;
|
| - std::vector<std::unique_ptr<CPDF_Object>> m_RefObjs;
|
| + std::vector<ScopedObj> m_RefObjs;
|
| CPDF_Dictionary* m_DictObj;
|
| CPDF_Dictionary* m_StreamDictObj;
|
| CPDF_Array* m_ArrayObj;
|
| @@ -268,13 +275,13 @@
|
| TEST_F(PDFObjectsTest, Clone) {
|
| // Check for direct objects.
|
| for (size_t i = 0; i < m_DirectObjs.size(); ++i) {
|
| - std::unique_ptr<CPDF_Object> obj(m_DirectObjs[i]->Clone());
|
| + ScopedObj obj(m_DirectObjs[i]->Clone());
|
| EXPECT_TRUE(Equal(m_DirectObjs[i].get(), obj.get()));
|
| }
|
|
|
| // Check indirect references.
|
| for (const auto& it : m_RefObjs) {
|
| - std::unique_ptr<CPDF_Object> obj(it->Clone());
|
| + ScopedObj obj(it->Clone());
|
| EXPECT_TRUE(Equal(it.get(), obj.get()));
|
| }
|
| }
|
| @@ -386,7 +393,7 @@
|
| {2.3f, 4.05f, 3, -2, -3, 0.0f},
|
| {0.05f, 0.1f, 0.56f, 0.67f, 1.34f, 99.9f}};
|
| for (size_t i = 0; i < FX_ArraySize(elems); ++i) {
|
| - std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
|
| + ScopedArray arr(new CPDF_Array);
|
| CFX_Matrix matrix(elems[i][0], elems[i][1], elems[i][2], elems[i][3],
|
| elems[i][4], elems[i][5]);
|
| for (size_t j = 0; j < 6; ++j)
|
| @@ -407,7 +414,7 @@
|
| {2.3f, 4.05f, -3, 0.0f},
|
| {0.05f, 0.1f, 1.34f, 99.9f}};
|
| for (size_t i = 0; i < FX_ArraySize(elems); ++i) {
|
| - std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
|
| + ScopedArray arr(new CPDF_Array);
|
| CFX_FloatRect rect(elems[i]);
|
| for (size_t j = 0; j < 4; ++j)
|
| arr->AddNumber(elems[i][j]);
|
| @@ -423,7 +430,7 @@
|
| {
|
| // Boolean array.
|
| const bool vals[] = {true, false, false, true, true};
|
| - std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
|
| + ScopedArray arr(new CPDF_Array);
|
| for (size_t i = 0; i < FX_ArraySize(vals); ++i)
|
| arr->InsertAt(i, new CPDF_Boolean(vals[i]));
|
| for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
|
| @@ -440,7 +447,7 @@
|
| {
|
| // Integer array.
|
| const int vals[] = {10, 0, -345, 2089345456, -1000000000, 567, 93658767};
|
| - std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
|
| + ScopedArray arr(new CPDF_Array);
|
| for (size_t i = 0; i < FX_ArraySize(vals); ++i)
|
| arr->InsertAt(i, new CPDF_Number(vals[i]));
|
| for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
|
| @@ -461,7 +468,7 @@
|
| 897.34f, -2.5f, -1.0f, -345.0f, -0.0f};
|
| const char* const expected_str[] = {
|
| "0", "0", "10", "10", "0.0345", "897.34", "-2.5", "-1", "-345", "0"};
|
| - std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
|
| + ScopedArray arr(new CPDF_Array);
|
| for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
|
| arr->InsertAt(i, new CPDF_Number(vals[i]));
|
| }
|
| @@ -480,8 +487,8 @@
|
| // String and name array
|
| const char* const vals[] = {"this", "adsde$%^", "\r\t", "\"012",
|
| ".", "EYREW", "It is a joke :)"};
|
| - std::unique_ptr<CPDF_Array> string_array(new CPDF_Array);
|
| - std::unique_ptr<CPDF_Array> name_array(new CPDF_Array);
|
| + ScopedArray string_array(new CPDF_Array);
|
| + ScopedArray name_array(new CPDF_Array);
|
| for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
|
| string_array->InsertAt(i, new CPDF_String(vals[i], false));
|
| name_array->InsertAt(i, new CPDF_Name(vals[i]));
|
| @@ -507,7 +514,7 @@
|
| }
|
| {
|
| // Null element array.
|
| - std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
|
| + ScopedArray arr(new CPDF_Array);
|
| for (size_t i = 0; i < 3; ++i)
|
| arr->InsertAt(i, new CPDF_Null);
|
| for (size_t i = 0; i < 3; ++i) {
|
| @@ -524,7 +531,7 @@
|
| {
|
| // Array of array.
|
| CPDF_Array* vals[3];
|
| - std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
|
| + ScopedArray arr(new CPDF_Array);
|
| for (size_t i = 0; i < 3; ++i) {
|
| vals[i] = new CPDF_Array;
|
| for (size_t j = 0; j < 3; ++j) {
|
| @@ -547,7 +554,7 @@
|
| {
|
| // Dictionary array.
|
| CPDF_Dictionary* vals[3];
|
| - std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
|
| + ScopedArray arr(new CPDF_Array);
|
| for (size_t i = 0; i < 3; ++i) {
|
| vals[i] = new CPDF_Dictionary();
|
| for (size_t j = 0; j < 3; ++j) {
|
| @@ -574,7 +581,7 @@
|
| // Stream array.
|
| CPDF_Dictionary* vals[3];
|
| CPDF_Stream* stream_vals[3];
|
| - std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
|
| + ScopedArray arr(new CPDF_Array);
|
| for (size_t i = 0; i < 3; ++i) {
|
| vals[i] = new CPDF_Dictionary();
|
| for (size_t j = 0; j < 3; ++j) {
|
| @@ -604,7 +611,7 @@
|
| }
|
| {
|
| // Mixed array.
|
| - std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
|
| + ScopedArray arr(new CPDF_Array);
|
| // Array arr will take ownership of all the objects inserted.
|
| arr->InsertAt(0, new CPDF_Boolean(true));
|
| arr->InsertAt(1, new CPDF_Boolean(false));
|
| @@ -669,7 +676,7 @@
|
| TEST(PDFArrayTest, AddNumber) {
|
| float vals[] = {1.0f, -1.0f, 0, 0.456734f,
|
| 12345.54321f, 0.5f, 1000, 0.000045f};
|
| - std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
|
| + ScopedArray arr(new CPDF_Array);
|
| for (size_t i = 0; i < FX_ArraySize(vals); ++i)
|
| arr->AddNumber(vals[i]);
|
| for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
|
| @@ -680,7 +687,7 @@
|
|
|
| TEST(PDFArrayTest, AddInteger) {
|
| int vals[] = {0, 1, 934435456, 876, 10000, -1, -24354656, -100};
|
| - std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
|
| + ScopedArray arr(new CPDF_Array);
|
| for (size_t i = 0; i < FX_ArraySize(vals); ++i)
|
| arr->AddInteger(vals[i]);
|
| for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
|
| @@ -692,8 +699,8 @@
|
| TEST(PDFArrayTest, AddStringAndName) {
|
| const char* vals[] = {"", "a", "ehjhRIOYTTFdfcdnv", "122323",
|
| "$#%^&**", " ", "This is a test.\r\n"};
|
| - std::unique_ptr<CPDF_Array> string_array(new CPDF_Array);
|
| - std::unique_ptr<CPDF_Array> name_array(new CPDF_Array);
|
| + ScopedArray string_array(new CPDF_Array);
|
| + ScopedArray name_array(new CPDF_Array);
|
| for (size_t i = 0; i < FX_ArraySize(vals); ++i) {
|
| string_array->AddString(vals[i]);
|
| name_array->AddName(vals[i]);
|
| @@ -718,8 +725,8 @@
|
| CPDF_Object* indirect_objs[] = {boolean_obj, int_obj, float_obj,
|
| str_obj, name_obj, null_obj};
|
| unsigned int obj_nums[] = {2, 4, 7, 2345, 799887, 1};
|
| - std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
|
| - std::unique_ptr<CPDF_Array> arr1(new CPDF_Array);
|
| + ScopedArray arr(new CPDF_Array);
|
| + ScopedArray arr1(new CPDF_Array);
|
| // Create two arrays of references by different AddReference() APIs.
|
| for (size_t i = 0; i < FX_ArraySize(indirect_objs); ++i) {
|
| // All the indirect objects inserted will be owned by holder.
|
| @@ -745,7 +752,7 @@
|
|
|
| TEST(PDFArrayTest, CloneDirectObject) {
|
| CPDF_IndirectObjectHolder objects_holder;
|
| - std::unique_ptr<CPDF_Array> array(new CPDF_Array);
|
| + ScopedArray array(new CPDF_Array);
|
| array->AddReference(&objects_holder, 1234);
|
| ASSERT_EQ(1U, array->GetCount());
|
| CPDF_Object* obj = array->GetObjectAt(0);
|
| @@ -756,7 +763,7 @@
|
| ASSERT_TRUE(cloned_array_object);
|
| ASSERT_TRUE(cloned_array_object->IsArray());
|
|
|
| - std::unique_ptr<CPDF_Array> cloned_array(cloned_array_object->AsArray());
|
| + ScopedArray cloned_array(cloned_array_object->AsArray());
|
| ASSERT_EQ(1U, cloned_array->GetCount());
|
| CPDF_Object* cloned_obj = cloned_array->GetObjectAt(0);
|
| EXPECT_FALSE(cloned_obj);
|
| @@ -764,7 +771,7 @@
|
|
|
| TEST(PDFArrayTest, ConvertIndirect) {
|
| CPDF_IndirectObjectHolder objects_holder;
|
| - std::unique_ptr<CPDF_Array> array(new CPDF_Array);
|
| + ScopedArray array(new CPDF_Array);
|
| CPDF_Object* pObj = new CPDF_Number(42);
|
| array->Add(pObj);
|
| array->ConvertToIndirectObjectAt(0, &objects_holder);
|
| @@ -779,7 +786,7 @@
|
|
|
| TEST(PDFDictionaryTest, CloneDirectObject) {
|
| CPDF_IndirectObjectHolder objects_holder;
|
| - std::unique_ptr<CPDF_Dictionary> dict(new CPDF_Dictionary());
|
| + ScopedDict dict(new CPDF_Dictionary());
|
| dict->SetReferenceFor("foo", &objects_holder, 1234);
|
| ASSERT_EQ(1U, dict->GetCount());
|
| CPDF_Object* obj = dict->GetObjectFor("foo");
|
| @@ -790,8 +797,7 @@
|
| ASSERT_TRUE(cloned_dict_object);
|
| ASSERT_TRUE(cloned_dict_object->IsDictionary());
|
|
|
| - std::unique_ptr<CPDF_Dictionary> cloned_dict(
|
| - cloned_dict_object->AsDictionary());
|
| + ScopedDict cloned_dict(cloned_dict_object->AsDictionary());
|
| ASSERT_EQ(1U, cloned_dict->GetCount());
|
| CPDF_Object* cloned_obj = cloned_dict->GetObjectFor("foo");
|
| EXPECT_FALSE(cloned_obj);
|
| @@ -801,12 +807,12 @@
|
| {
|
| // Create a dictionary/array pair with a reference loop.
|
| CPDF_Dictionary* dict_obj = new CPDF_Dictionary();
|
| - std::unique_ptr<CPDF_Array> arr_obj(new CPDF_Array);
|
| + ScopedArray arr_obj(new CPDF_Array);
|
| dict_obj->SetFor("arr", arr_obj.get());
|
| arr_obj->InsertAt(0, dict_obj);
|
|
|
| // Clone this object to see whether stack overflow will be triggered.
|
| - std::unique_ptr<CPDF_Array> cloned_array(arr_obj->Clone()->AsArray());
|
| + ScopedArray cloned_array(arr_obj->Clone()->AsArray());
|
| // Cloned object should be the same as the original.
|
| ASSERT_TRUE(cloned_array);
|
| EXPECT_EQ(1u, cloned_array->GetCount());
|
| @@ -819,12 +825,11 @@
|
| {
|
| // Create a dictionary/stream pair with a reference loop.
|
| CPDF_Dictionary* dict_obj = new CPDF_Dictionary();
|
| - std::unique_ptr<CPDF_Stream> stream_obj(
|
| - new CPDF_Stream(nullptr, 0, dict_obj));
|
| + ScopedStream stream_obj(new CPDF_Stream(nullptr, 0, dict_obj));
|
| dict_obj->SetFor("stream", stream_obj.get());
|
|
|
| // Clone this object to see whether stack overflow will be triggered.
|
| - std::unique_ptr<CPDF_Stream> cloned_stream(stream_obj->Clone()->AsStream());
|
| + ScopedStream cloned_stream(stream_obj->Clone()->AsStream());
|
| // Cloned object should be the same as the original.
|
| ASSERT_TRUE(cloned_stream);
|
| CPDF_Object* cloned_dict = cloned_stream->GetDict();
|
| @@ -850,8 +855,7 @@
|
| EXPECT_EQ(dict_obj, elem0->AsReference()->GetDirect());
|
|
|
| // Clone this object to see whether stack overflow will be triggered.
|
| - std::unique_ptr<CPDF_Dictionary> cloned_dict(
|
| - ToDictionary(dict_obj->CloneDirectObject()));
|
| + ScopedDict cloned_dict(ToDictionary(dict_obj->CloneDirectObject()));
|
| // Cloned object should be the same as the original.
|
| ASSERT_TRUE(cloned_dict);
|
| CPDF_Object* cloned_arr = cloned_dict->GetObjectFor("arr");
|
| @@ -865,7 +869,7 @@
|
|
|
| TEST(PDFDictionaryTest, ConvertIndirect) {
|
| CPDF_IndirectObjectHolder objects_holder;
|
| - std::unique_ptr<CPDF_Dictionary> dict(new CPDF_Dictionary);
|
| + ScopedDict dict(new CPDF_Dictionary);
|
| CPDF_Object* pObj = new CPDF_Number(42);
|
| dict->SetFor("clams", pObj);
|
| dict->ConvertToIndirectObjectFor("clams", &objects_holder);
|
|
|