aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDFObjectHandle.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2021-11-04 16:55:36 +0100
committerJay Berkenbilt <ejb@ql.org>2021-11-04 17:29:42 +0100
commit9b28933647f0a3ed535dd488c4526b75d1c10fc6 (patch)
tree6cd87c2f661653c3afac8ba95a37e338e14e3f36 /libqpdf/QPDFObjectHandle.cc
parent73752683c936af0f5a556982c2c59516d2914ee3 (diff)
downloadqpdf-9b28933647f0a3ed535dd488c4526b75d1c10fc6.tar.zst
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.
Diffstat (limited to 'libqpdf/QPDFObjectHandle.cc')
-rw-r--r--libqpdf/QPDFObjectHandle.cc22
1 files changed, 22 insertions, 0 deletions
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<QPDF_Array*>(obj.getPointer())->setItem(n, item);
}
else
@@ -878,6 +879,10 @@ QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items)
{
if (isArray())
{
+ for (auto const& item: items)
+ {
+ checkOwnership(item);
+ }
dynamic_cast<QPDF_Array*>(obj.getPointer())->setFromVector(items);
}
else
@@ -906,6 +911,7 @@ QPDFObjectHandle::appendItem(QPDFObjectHandle const& item)
{
if (isArray())
{
+ checkOwnership(item);
dynamic_cast<QPDF_Array*>(obj.getPointer())->appendItem(item);
}
else
@@ -1283,6 +1289,7 @@ QPDFObjectHandle::replaceKey(std::string const& key,
{
if (isDictionary())
{
+ checkOwnership(value);
dynamic_cast<QPDF_Dictionary*>(
obj.getPointer())->replaceKey(key, value);
}
@@ -1313,6 +1320,7 @@ QPDFObjectHandle::replaceOrRemoveKey(std::string const& key,
{
if (isDictionary())
{
+ checkOwnership(value);
dynamic_cast<QPDF_Dictionary*>(
obj.getPointer())->replaceOrRemoveKey(key, value);
}
@@ -3270,6 +3278,20 @@ QPDFObjectHandle::isImage(bool exclude_imagemask)
}
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()
{
if (! isPageObject())