Have QPDFWriter detect foreign objects while writing

Throw an exception that directs the user to QPDF::copyForeignObject.
This commit is contained in:
Jay Berkenbilt 2012-07-14 08:07:23 -04:00
parent 11b194a1d0
commit 1c944e4c89
6 changed files with 43 additions and 1 deletions

View File

@ -1,3 +1,9 @@
2012-07-14 Jay Berkenbilt <ejb@ql.org>
* QPDFWriter: detect if the user has inserted an indirect object
from another QPDF object and throw an exception directing the user
to copyForeignObject.
2012-07-11 Jay Berkenbilt <ejb@ql.org>
* Added new APIs to copy objects from one QPDF to another. This

View File

@ -761,6 +761,15 @@ QPDFWriter::enqueueObject(QPDFObjectHandle object)
{
if (object.isIndirect())
{
if (object.getOwningQPDF() != &(this->pdf))
{
QTC::TC("qpdf", "QPDFWriter foreign object");
throw std::logic_error(
"QPDFObjectHandle from different QPDF found while writing."
" Use QPDF::copyForeignObject to add objects from"
" another file.");
}
if (object.isNull())
{
// This is a place-holder object for an object stream

View File

@ -233,3 +233,4 @@ QPDF not crossing page boundary 0
QPDF replace foreign indirect with null 0
QPDF not copying pages object 0
QPDF insert foreign page 0
QPDFWriter foreign object 0

View File

@ -149,7 +149,7 @@ $td->runtest("remove page we don't have",
$td->NORMALIZE_NEWLINES);
# ----------
$td->notify("--- Miscellaneous Tests ---");
$n_tests += 43;
$n_tests += 44;
$td->runtest("qpdf version",
{$td->COMMAND => "qpdf --version"},
@ -365,6 +365,10 @@ $td->runtest("reserved objects",
$td->runtest("check output",
{$td->FILE => "a.pdf"},
{$td->FILE => "reserved-objects.pdf"});
$td->runtest("detect foreign object in write",
{$td->COMMAND => "test_driver 29 copy-foreign-objects-in.pdf"},
{$td->FILE => "foreign-in-write.out", $td->EXIT_STATUS => 0},
$td->NORMALIZE_NEWLINES);
show_ntests();
# ----------

View File

@ -0,0 +1,2 @@
logic error: QPDFObjectHandle from different QPDF found while writing. Use QPDF::copyForeignObject to add objects from another file.
test 29 done

View File

@ -999,6 +999,26 @@ void runtest(int n, char const* filename)
std::cout << "logic error: " << e.what() << std::endl;
}
}
else if (n == 29)
{
// Detect mixed objects in QPDFWriter
QPDF other;
other.processFile("minimal.pdf");
// Should use copyForeignObject instead
other.getTrailer().replaceKey(
"/QTest", pdf.getTrailer().getKey("/QTest"));
try
{
QPDFWriter w(other, "a.pdf");
w.write();
std::cout << "oops -- didn't throw" << std::endl;
}
catch (std::logic_error e)
{
std::cout << "logic error: " << e.what() << std::endl;
}
}
else
{
throw std::runtime_error(std::string("invalid test ") +