Make CPDF_{Array,Dictionary} methods returning pointers internal-only.
-- Make some methods return retained references.
-- Make other methods return void to prevent misuse.
-- Use `auto` when type already appears on the same line.
Change-Id: Ie02b2324cd111684350c8b59f863323031536f95
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98271
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_appstream.cpp b/fpdfsdk/cpdfsdk_appstream.cpp
index cffecbf..5b5550d 100644
--- a/fpdfsdk/cpdfsdk_appstream.cpp
+++ b/fpdfsdk/cpdfsdk_appstream.cpp
@@ -1816,8 +1816,7 @@
RetainPtr<CPDF_Dictionary> pStreamResList =
pStreamDict->GetOrCreateDictFor("Resources");
- CPDF_Dictionary* pXObject =
- pStreamResList->SetNewFor<CPDF_Dictionary>("XObject");
+ auto pXObject = pStreamResList->SetNewFor<CPDF_Dictionary>("XObject");
pXObject->SetNewFor<CPDF_Reference>(sImageAlias,
widget_->GetPageView()->GetPDFDocument(),
pImage->GetObjNum());
diff --git a/fpdfsdk/cpdfsdk_helpers.cpp b/fpdfsdk/cpdfsdk_helpers.cpp
index c066fe7..a61e430 100644
--- a/fpdfsdk/cpdfsdk_helpers.cpp
+++ b/fpdfsdk/cpdfsdk_helpers.cpp
@@ -236,7 +236,7 @@
const_cast<CPDF_Array*>(GetQuadPointsArrayFromDictionary(dict)));
}
-CPDF_Array* AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict) {
+RetainPtr<CPDF_Array> AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict) {
return dict->SetNewFor<CPDF_Array>(kQuadPoints);
}
diff --git a/fpdfsdk/cpdfsdk_helpers.h b/fpdfsdk/cpdfsdk_helpers.h
index be34833..d722ecc 100644
--- a/fpdfsdk/cpdfsdk_helpers.h
+++ b/fpdfsdk/cpdfsdk_helpers.h
@@ -251,7 +251,7 @@
const CPDF_Array* GetQuadPointsArrayFromDictionary(const CPDF_Dictionary* dict);
RetainPtr<CPDF_Array> GetMutableQuadPointsArrayFromDictionary(
CPDF_Dictionary* dict);
-CPDF_Array* AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict);
+RetainPtr<CPDF_Array> AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict);
bool IsValidQuadPointsIndex(const CPDF_Array* array, size_t index);
bool GetQuadPointsAtIndex(const CPDF_Array* array,
size_t quad_index,
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index 9861b81..dda378c 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -525,12 +525,11 @@
if (!safe_ink_size.IsValid<int32_t>())
return -1;
- CPDF_Array* ink_coord_list = inklist->AppendNew<CPDF_Array>();
+ auto ink_coord_list = inklist->AppendNew<CPDF_Array>();
for (size_t i = 0; i < point_count; i++) {
ink_coord_list->AppendNew<CPDF_Number>(points[i].x);
ink_coord_list->AppendNew<CPDF_Number>(points[i].y);
}
-
return static_cast<int>(inklist->size() - 1);
}
@@ -684,7 +683,7 @@
if (pColor)
pColor->Clear();
else
- pColor.Reset(pAnnotDict->SetNewFor<CPDF_Array>(key));
+ pColor = pAnnotDict->SetNewFor<CPDF_Array>(key);
pColor->AppendNew<CPDF_Number>(R / 255.f);
pColor->AppendNew<CPDF_Number>(G / 255.f);
@@ -797,7 +796,7 @@
RetainPtr<CPDF_Array> pQuadPointsArray =
GetMutableQuadPointsArrayFromDictionary(pAnnotDict.Get());
if (!pQuadPointsArray)
- pQuadPointsArray.Reset(AddQuadPointsArrayToDictionary(pAnnotDict.Get()));
+ pQuadPointsArray = AddQuadPointsArrayToDictionary(pAnnotDict.Get());
AppendQuadPoints(pQuadPointsArray.Get(), quad_points);
UpdateBBox(pAnnotDict.Get());
return true;
@@ -967,8 +966,7 @@
// not use the border values.
annot_dict->RemoveFor(pdfium::annotation::kAP);
- CPDF_Array* border =
- annot_dict->SetNewFor<CPDF_Array>(pdfium::annotation::kBorder);
+ auto border = annot_dict->SetNewFor<CPDF_Array>(pdfium::annotation::kBorder);
border->AppendNew<CPDF_Number>(horizontal_radius);
border->AppendNew<CPDF_Number>(vertical_radius);
border->AppendNew<CPDF_Number>(border_width);
@@ -1120,8 +1118,7 @@
// Storing reference to indirect object in annotation's AP
if (!pApDict) {
- pApDict.Reset(
- pAnnotDict->SetNewFor<CPDF_Dictionary>(pdfium::annotation::kAP));
+ pApDict = pAnnotDict->SetNewFor<CPDF_Dictionary>(pdfium::annotation::kAP);
}
pApDict->SetNewFor<CPDF_Reference>(modeKey, pDoc,
pNewIndirectStream->GetObjNum());
@@ -1477,7 +1474,7 @@
RetainPtr<CPDF_Dictionary> annot_dict =
GetMutableAnnotDictFromFPDFAnnotation(annot);
- CPDF_Dictionary* action = annot_dict->SetNewFor<CPDF_Dictionary>("A");
+ auto action = annot_dict->SetNewFor<CPDF_Dictionary>("A");
action->SetNewFor<CPDF_Name>("Type", "Action");
action->SetNewFor<CPDF_Name>("S", "URI");
action->SetNewFor<CPDF_String>("URI", uri, /*bHex=*/false);
diff --git a/fpdfsdk/fpdf_attachment.cpp b/fpdfsdk/fpdf_attachment.cpp
index b1461bb..de02785 100644
--- a/fpdfsdk/fpdf_attachment.cpp
+++ b/fpdfsdk/fpdf_attachment.cpp
@@ -216,8 +216,7 @@
// Create a dictionary for the new embedded file stream.
auto pFileStreamDict = pdfium::MakeRetain<CPDF_Dictionary>();
- CPDF_Dictionary* pParamsDict =
- pFileStreamDict->SetNewFor<CPDF_Dictionary>("Params");
+ auto pParamsDict = pFileStreamDict->SetNewFor<CPDF_Dictionary>("Params");
// Set the size of the new file in the dictionary.
pFileStreamDict->SetNewFor<CPDF_Number>(pdfium::stream::kDL,
@@ -244,8 +243,7 @@
memcpy(stream.get(), contents, len);
auto pFileStream = pDoc->NewIndirect<CPDF_Stream>(std::move(stream), len,
std::move(pFileStreamDict));
- CPDF_Dictionary* pEFDict =
- pFile->AsMutableDictionary()->SetNewFor<CPDF_Dictionary>("EF");
+ auto pEFDict = pFile->AsMutableDictionary()->SetNewFor<CPDF_Dictionary>("EF");
pEFDict->SetNewFor<CPDF_Reference>("F", pDoc, pFileStream->GetObjNum());
return true;
}
diff --git a/fpdfsdk/fpdf_catalog_unittest.cpp b/fpdfsdk/fpdf_catalog_unittest.cpp
index 690f012..dd765bf 100644
--- a/fpdfsdk/fpdf_catalog_unittest.cpp
+++ b/fpdfsdk/fpdf_catalog_unittest.cpp
@@ -56,8 +56,7 @@
EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
// Root with empty MarkInfo
- CPDF_Dictionary* markInfoDict =
- m_pRootObj->SetNewFor<CPDF_Dictionary>("MarkInfo");
+ auto markInfoDict = m_pRootObj->SetNewFor<CPDF_Dictionary>("MarkInfo");
EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
// MarkInfo present but Marked is 0
diff --git a/fpdfsdk/fpdf_editpage.cpp b/fpdfsdk/fpdf_editpage.cpp
index c88ecfb..0611a57 100644
--- a/fpdfsdk/fpdf_editpage.cpp
+++ b/fpdfsdk/fpdf_editpage.cpp
@@ -705,7 +705,7 @@
if (pRectArray)
pRectArray->Clear();
else
- pRectArray.Reset(pAnnotDict->SetNewFor<CPDF_Array>("Rect"));
+ pRectArray = pAnnotDict->SetNewFor<CPDF_Array>("Rect");
pRectArray->AppendNew<CPDF_Number>(rect.left);
pRectArray->AppendNew<CPDF_Number>(rect.bottom);
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp
index 2b61d1a..38b7cf8 100644
--- a/fpdfsdk/fpdf_edittext.cpp
+++ b/fpdfsdk/fpdf_edittext.cpp
@@ -441,7 +441,7 @@
// TODO(npm): Support vertical writing
- auto* pDescendant = pFontDict->SetNewFor<CPDF_Array>("DescendantFonts");
+ auto pDescendant = pFontDict->SetNewFor<CPDF_Array>("DescendantFonts");
pDescendant->AppendNew<CPDF_Reference>(pDoc, pCIDFont->GetObjNum());
CPDF_Stream* toUnicodeStream = LoadUnicode(pDoc, to_unicode);
diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp
index 2b0e18a..202ec82 100644
--- a/fpdfsdk/fpdf_flatten.cpp
+++ b/fpdfsdk/fpdf_flatten.cpp
@@ -317,7 +317,7 @@
SetPageContents(key, pPageDict.Get(), pDocument);
- CPDF_Dictionary* pNewXORes = nullptr;
+ RetainPtr<CPDF_Dictionary> pNewXORes;
if (!key.IsEmpty()) {
pPageXObject->SetNewFor<CPDF_Reference>(key, pDocument,
pNewXObject->GetObjNum());