Use pdfium::Contains() to check CPDF_Form membership.
Avoid a complicated expression involving std::find_if(). Instead, use
FakeUniquePtr<> to get an object suitable for Contains().
-- Add MakeFakeUniquePtr() type-deducing wrapper.
Change-Id: I8421583febc9602cd2bc156fb65a97f0df64b254
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/99372
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/stl_util.h b/core/fxcrt/stl_util.h
index 45a980e..9bfd90c 100644
--- a/core/fxcrt/stl_util.h
+++ b/core/fxcrt/stl_util.h
@@ -20,6 +20,12 @@
~FakeUniquePtr() { std::unique_ptr<T>::release(); }
};
+// Type-deducing wrapper for FakeUniquePtr<T>.
+template <class T>
+FakeUniquePtr<T> MakeFakeUniquePtr(T* arg) {
+ return FakeUniquePtr<T>(arg);
+}
+
// Convenience routine for "int-fected" code, so that the stl collection
// size_t size() method return values will be checked.
template <typename ResultType, typename Collection>
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index 28597dd..723aa3b 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -38,6 +38,7 @@
#include "fpdfsdk/cpdfsdk_helpers.h"
#include "fpdfsdk/cpdfsdk_interactiveform.h"
#include "third_party/base/check.h"
+#include "third_party/base/containers/contains.h"
#include "third_party/base/numerics/safe_conversions.h"
#include "third_party/base/ptr_util.h"
@@ -490,12 +491,7 @@
// Check that the object is already in this annotation's object list.
CPDF_Form* pForm = pAnnot->GetForm();
- auto it =
- std::find_if(pForm->begin(), pForm->end(),
- [pObj](const std::unique_ptr<CPDF_PageObject>& candidate) {
- return candidate.get() == pObj;
- });
- if (it == pForm->end())
+ if (!pdfium::Contains(*pForm, fxcrt::MakeFakeUniquePtr(pObj)))
return false;
// Update the content stream data in the annotation's AP stream.
@@ -572,12 +568,7 @@
// Note that an object that came from a different annotation must not be
// passed here, since an object cannot belong to more than one annotation.
CPDF_Form* pForm = pAnnot->GetForm();
- auto it =
- std::find_if(pForm->begin(), pForm->end(),
- [pObj](const std::unique_ptr<CPDF_PageObject>& candidate) {
- return candidate.get() == pObj;
- });
- if (it != pForm->end())
+ if (pdfium::Contains(*pForm, fxcrt::MakeFakeUniquePtr(pObj)))
return false;
// Append the object to the object list.