Commit 2b4dcb33aa3cc130241894df78e2b67bbe5c99b8

Authored by Jay Berkenbilt
Committed by GitHub
2 parents fec75b44 b0c3ea2b

Merge pull request #1014 from m-holger/i1011

Change QPDF::copyForeignObject to return a null object when called wi…
libqpdf/QPDF.cc
@@ -2034,13 +2034,13 @@ QPDF::copyForeignObject(QPDFObjectHandle foreign) @@ -2034,13 +2034,13 @@ QPDF::copyForeignObject(QPDFObjectHandle foreign)
2034 } 2034 }
2035 obj_copier.to_copy.clear(); 2035 obj_copier.to_copy.clear();
2036 2036
2037 - auto& result = obj_copier.object_map[foreign.getObjGen()];  
2038 - if (!result.isInitialized()) {  
2039 - result = QPDFObjectHandle::newNull();  
2040 - warn(damagedPDF("Unexpected reference to /Pages object while copying foreign object. "  
2041 - "Replacing with Null object.")); 2037 + auto og = foreign.getObjGen();
  2038 + if (!obj_copier.object_map.count(og)) {
  2039 + warn(damagedPDF("unexpected reference to /Pages object while copying foreign object; "
  2040 + "replacing with null"));
  2041 + return QPDFObjectHandle::newNull();
2042 } 2042 }
2043 - return result; 2043 + return obj_copier.object_map[foreign.getObjGen()];
2044 } 2044 }
2045 2045
2046 void 2046 void
qpdf/qtest/qpdf/copy-foreign-objects-25.out
  1 +WARNING: minimal.pdf (object 6 0, offset 556): unexpected reference to /Pages object while copying foreign object; replacing with null
1 test 25 done 2 test 25 done
qpdf/test_driver.cc
@@ -954,6 +954,8 @@ test_25(QPDF& pdf, char const* arg2) @@ -954,6 +954,8 @@ test_25(QPDF& pdf, char const* arg2)
954 // Copy qtest without crossing page boundaries. Should get O1 954 // Copy qtest without crossing page boundaries. Should get O1
955 // and O2 and their streams but not O3 or any other pages. 955 // and O2 and their streams but not O3 or any other pages.
956 956
  957 + // Also verify that attempts to copy /Pages objects return null.
  958 +
957 assert(arg2 != nullptr); 959 assert(arg2 != nullptr);
958 { 960 {
959 // Make sure original PDF is out of scope when we write. 961 // Make sure original PDF is out of scope when we write.
@@ -961,6 +963,8 @@ test_25(QPDF& pdf, char const* arg2) @@ -961,6 +963,8 @@ test_25(QPDF& pdf, char const* arg2)
961 oldpdf.processFile(arg2); 963 oldpdf.processFile(arg2);
962 QPDFObjectHandle qtest = oldpdf.getTrailer().getKey("/QTest"); 964 QPDFObjectHandle qtest = oldpdf.getTrailer().getKey("/QTest");
963 pdf.getTrailer().replaceKey("/QTest", pdf.copyForeignObject(qtest)); 965 pdf.getTrailer().replaceKey("/QTest", pdf.copyForeignObject(qtest));
  966 +
  967 + assert(pdf.copyForeignObject(oldpdf.getRoot().getKey("/Pages")).isNull());
964 } 968 }
965 969
966 QPDFWriter w(pdf, "a.pdf"); 970 QPDFWriter w(pdf, "a.pdf");