From c6179da9615057a14e74180f640e2e77fdcbf234 Mon Sep 17 00:00:00 2001 From: m-holger Date: Thu, 17 Nov 2022 17:35:13 +0000 Subject: Add new method QPDFValue::checkOwnership --- libqpdf/QPDF_Array.cc | 20 ++++++++++++++++++++ libqpdf/qpdf/QPDF_Array.hh | 3 +++ 2 files changed, 23 insertions(+) diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index e9d216a5..def40e9b 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -7,6 +7,26 @@ static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull(); +inline void +QPDF_Array::checkOwnership(QPDFObjectHandle const& item) const +{ + if (auto obj = item.getObjectPtr()) { + if (qpdf) { + if (auto item_qpdf = obj->getQPDF()) { + if (qpdf != item_qpdf) { + throw std::logic_error( + "Attempting to add an object from a different QPDF. " + "Use QPDF::copyForeignObject to add objects from " + "another file."); + } + } + } + } else { + throw std::logic_error( + "Attempting to add an uninitialized object to a QPDF_Array."); + } +} + QPDF_Array::QPDF_Array(std::vector const& v) : QPDFValue(::ot_array, "array") { diff --git a/libqpdf/qpdf/QPDF_Array.hh b/libqpdf/qpdf/QPDF_Array.hh index 4947d09c..b27ce122 100644 --- a/libqpdf/qpdf/QPDF_Array.hh +++ b/libqpdf/qpdf/QPDF_Array.hh @@ -42,6 +42,9 @@ class QPDF_Array: public QPDFValue QPDF_Array(std::vector>&& items, bool sparse); QPDF_Array(SparseOHArray const& items); QPDF_Array(std::vector> const& items); + + void checkOwnership(QPDFObjectHandle const& item) const; + bool sparse{false}; SparseOHArray sp_elements; std::vector> elements; -- cgit v1.2.3-54-g00ecf