From 9b28933647f0a3ed535dd488c4526b75d1c10fc6 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 4 Nov 2021 11:55:36 -0400 Subject: Check object ownership when adding When adding a QPDFObjectHandle to an array or dictionary, if possible, check if the new object belongs to the same QPDF. This makes it much easier to find incorrect code than waiting for the situation to be detected when the file is written. --- libqpdf/QPDFObjectHandle.cc | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'libqpdf') diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 27740f09..af862bd4 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -864,6 +864,7 @@ QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item) { if (isArray()) { + checkOwnership(item); dynamic_cast(obj.getPointer())->setItem(n, item); } else @@ -878,6 +879,10 @@ QPDFObjectHandle::setArrayFromVector(std::vector const& items) { if (isArray()) { + for (auto const& item: items) + { + checkOwnership(item); + } dynamic_cast(obj.getPointer())->setFromVector(items); } else @@ -906,6 +911,7 @@ QPDFObjectHandle::appendItem(QPDFObjectHandle const& item) { if (isArray()) { + checkOwnership(item); dynamic_cast(obj.getPointer())->appendItem(item); } else @@ -1283,6 +1289,7 @@ QPDFObjectHandle::replaceKey(std::string const& key, { if (isDictionary()) { + checkOwnership(value); dynamic_cast( obj.getPointer())->replaceKey(key, value); } @@ -1313,6 +1320,7 @@ QPDFObjectHandle::replaceOrRemoveKey(std::string const& key, { if (isDictionary()) { + checkOwnership(value); dynamic_cast( obj.getPointer())->replaceOrRemoveKey(key, value); } @@ -3269,6 +3277,20 @@ QPDFObjectHandle::isImage(bool exclude_imagemask) dict.getKey("/ImageMask").getBoolValue())))); } +void +QPDFObjectHandle::checkOwnership(QPDFObjectHandle const& item) const +{ + if ((this->qpdf != nullptr) && + (item.qpdf != nullptr) && + (this->qpdf != item.qpdf)) + { + QTC::TC("qpdf", "QPDFObjectHandle check ownership"); + throw std::logic_error( + "Attempting to add an object from a different QPDF." + " Use QPDF::copyForeignObject to add objects from another file."); + } +} + void QPDFObjectHandle::assertPageObject() { -- cgit v1.2.3-54-g00ecf