From 4422588d7d51e226e6aeecfa9e53382aeb54d7c4 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Wed, 7 Sep 2022 14:11:24 -0400 Subject: Remove unneeded owning_qpdf from QPDFValue The qpdf member was already sufficient. Removing this actually fixed a few pre-existing issues around detecting foreign ownership and allowing certain conditions to be warnings rather than exceptions. --- libqpdf/QPDF_Unresolved.cc | 2 +- libqpdf/qpdf/QPDFValue.hh | 13 ++++++------- libqpdf/qpdf/QPDFValueProxy.hh | 2 +- qpdf/qtest/qpdf/split-tokens-split.out | 3 ++- qpdf/test_driver.cc | 12 ++++++++++-- 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/libqpdf/QPDF_Unresolved.cc b/libqpdf/QPDF_Unresolved.cc index f72281c0..503e5b84 100644 --- a/libqpdf/QPDF_Unresolved.cc +++ b/libqpdf/QPDF_Unresolved.cc @@ -18,7 +18,7 @@ QPDF_Unresolved::shallowCopy() { throw std::logic_error( "attempted to shallow copy unresolved QPDFObjectHandle"); - return create(qpdf, og); + return nullptr; } std::string diff --git a/libqpdf/qpdf/QPDFValue.hh b/libqpdf/qpdf/QPDFValue.hh index bac573d6..69f7eeda 100644 --- a/libqpdf/qpdf/QPDFValue.hh +++ b/libqpdf/qpdf/QPDFValue.hh @@ -24,22 +24,22 @@ class QPDFValue virtual std::string unparse() = 0; virtual JSON getJSON(int json_version) = 0; virtual void - setDescription(QPDF* qpdf, std::string const& description) + setDescription(QPDF* qpdf_p, std::string const& description) { - owning_qpdf = qpdf; + qpdf = qpdf_p; object_description = description; } bool - getDescription(QPDF*& qpdf, std::string& description) + getDescription(QPDF*& qpdf_p, std::string& description) { - qpdf = owning_qpdf; + qpdf_p = qpdf; description = object_description; - return owning_qpdf != nullptr; + return qpdf != nullptr; } bool hasDescription() { - return owning_qpdf != nullptr; + return qpdf != nullptr && !object_description.empty(); } void setParsedOffset(qpdf_offset_t offset) @@ -92,7 +92,6 @@ class QPDFValue private: QPDFValue(QPDFValue const&) = delete; QPDFValue& operator=(QPDFValue const&) = delete; - QPDF* owning_qpdf{nullptr}; std::string object_description; qpdf_offset_t parsed_offset{-1}; const qpdf_object_type_e type_code; diff --git a/libqpdf/qpdf/QPDFValueProxy.hh b/libqpdf/qpdf/QPDFValueProxy.hh index e190318c..992ad115 100644 --- a/libqpdf/qpdf/QPDFValueProxy.hh +++ b/libqpdf/qpdf/QPDFValueProxy.hh @@ -49,7 +49,7 @@ class QPDFValueProxy { return value->type_name; } - // Returns nullptr for direct objects + QPDF* getQPDF() const { diff --git a/qpdf/qtest/qpdf/split-tokens-split.out b/qpdf/qtest/qpdf/split-tokens-split.out index 828abb5b..8e1003be 100644 --- a/qpdf/qtest/qpdf/split-tokens-split.out +++ b/qpdf/qtest/qpdf/split-tokens-split.out @@ -1,4 +1,5 @@ -WARNING: split-tokens.pdf, object 3 0 at offset 181: Unable to parse content stream: page object 3 0 stream 5 0, stream 7 0, stream 9 0, stream 11 0 (content, offset 375): null character not allowed in name token; not attempting to remove unreferenced objects from this object +WARNING: page object 3 0 stream 5 0, stream 7 0, stream 9 0, stream 11 0 (content, offset 375): null character not allowed in name token +WARNING: split-tokens.pdf, object 3 0 at offset 181: Bad token found while scanning content stream; not attempting to remove unreferenced objects from this object WARNING: empty PDF: content normalization encountered bad tokens WARNING: empty PDF: normalized content ended with a bad token; you may be able to resolve this by coalescing content streams in combination with normalizing content. From the command line, specify --coalesce-contents WARNING: empty PDF: Resulting stream data may be corrupted but is may still useful for manual inspection. For more information on this warning, search for content normalization in the manual. diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc index 81cefce6..4440bfae 100644 --- a/qpdf/test_driver.cc +++ b/qpdf/test_driver.cc @@ -1137,8 +1137,16 @@ test_29(QPDF& pdf, char const* arg2) assert(arg2 != 0); QPDF other; other.processFile(arg2); - // Should use copyForeignObject instead - other.getTrailer().replaceKey("/QTest", pdf.getTrailer().getKey("/QTest")); + // We need to create a QPDF with mixed ownership to exercise + // QPDFWriter's ownership check. To do this, we have to sneak the + // foreign object inside an ownerless direct object to avoid + // detection prior to calling QPDFWriter. Maybe a future version + // of qpdf will be able prevent creating mixed ownership. Another + // way to fake it out would be to call setDescription to + // explicitly change the ownership to the wrong value. + auto dict = QPDFObjectHandle::newDictionary(); + dict.replaceKey("/QTest", pdf.getTrailer().getKey("/QTest")); + other.getTrailer().replaceKey("/QTest", dict); try { QPDFWriter w(other, "a.pdf"); -- cgit v1.2.3-54-g00ecf