From ca79fcb26e1e1fa94bd1119119eac17b5140c196 Mon Sep 17 00:00:00 2001 From: m-holger Date: Sun, 3 Sep 2023 10:50:06 +0100 Subject: [PATCH 1/2] Add test for attempts to copy foreign /Pages object --- qpdf/qtest/qpdf/copy-foreign-objects-25.out | 1 + qpdf/test_driver.cc | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/qpdf/qtest/qpdf/copy-foreign-objects-25.out b/qpdf/qtest/qpdf/copy-foreign-objects-25.out index da8906a7..a34184ac 100644 --- a/qpdf/qtest/qpdf/copy-foreign-objects-25.out +++ b/qpdf/qtest/qpdf/copy-foreign-objects-25.out @@ -1 +1,2 @@ +WARNING: minimal.pdf (object 6 0, offset 556): Unexpected reference to /Pages object while copying foreign object. Replacing with Null object. test 25 done diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc index b2ed825d..2e661fdb 100644 --- a/qpdf/test_driver.cc +++ b/qpdf/test_driver.cc @@ -954,6 +954,8 @@ test_25(QPDF& pdf, char const* arg2) // Copy qtest without crossing page boundaries. Should get O1 // and O2 and their streams but not O3 or any other pages. + // Also verify that attempts to copy /Pages objects return null. + assert(arg2 != nullptr); { // Make sure original PDF is out of scope when we write. @@ -961,6 +963,8 @@ test_25(QPDF& pdf, char const* arg2) oldpdf.processFile(arg2); QPDFObjectHandle qtest = oldpdf.getTrailer().getKey("/QTest"); pdf.getTrailer().replaceKey("/QTest", pdf.copyForeignObject(qtest)); + + assert(pdf.copyForeignObject(oldpdf.getRoot().getKey("/Pages")).isNull()); } QPDFWriter w(pdf, "a.pdf"); From b0c3ea2bb6e0cab1d09726f3e66742eaf45c5745 Mon Sep 17 00:00:00 2001 From: m-holger Date: Sun, 3 Sep 2023 11:03:24 +0100 Subject: [PATCH 2/2] Refactor test for /Pages object in QPDF::copyForeignObject Test for missing object before accessing obj_copier.object_map. --- libqpdf/QPDF.cc | 12 ++++++------ qpdf/qtest/qpdf/copy-foreign-objects-25.out | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/libqpdf/QPDF.cc b/libqpdf/QPDF.cc index e39f9b7a..95ed39ec 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -2034,13 +2034,13 @@ QPDF::copyForeignObject(QPDFObjectHandle foreign) } obj_copier.to_copy.clear(); - auto& result = obj_copier.object_map[foreign.getObjGen()]; - if (!result.isInitialized()) { - result = QPDFObjectHandle::newNull(); - warn(damagedPDF("Unexpected reference to /Pages object while copying foreign object. " - "Replacing with Null object.")); + auto og = foreign.getObjGen(); + if (!obj_copier.object_map.count(og)) { + warn(damagedPDF("unexpected reference to /Pages object while copying foreign object; " + "replacing with null")); + return QPDFObjectHandle::newNull(); } - return result; + return obj_copier.object_map[foreign.getObjGen()]; } void diff --git a/qpdf/qtest/qpdf/copy-foreign-objects-25.out b/qpdf/qtest/qpdf/copy-foreign-objects-25.out index a34184ac..36b5bb0d 100644 --- a/qpdf/qtest/qpdf/copy-foreign-objects-25.out +++ b/qpdf/qtest/qpdf/copy-foreign-objects-25.out @@ -1,2 +1,2 @@ -WARNING: minimal.pdf (object 6 0, offset 556): Unexpected reference to /Pages object while copying foreign object. Replacing with Null object. +WARNING: minimal.pdf (object 6 0, offset 556): unexpected reference to /Pages object while copying foreign object; replacing with null test 25 done