From 720ce9e8f333ba3911fa8003f08fd8813c19181a Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Mon, 29 Nov 2021 07:31:05 -0500 Subject: Improve testing and error handling around operating before processing --- include/qpdf/qpdf-c.h | 4 +++- libqpdf/QPDF.cc | 6 ++++-- qpdf/qtest/qpdf/test73.out | 3 ++- qpdf/test_driver.cc | 10 ++++++++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/qpdf/qpdf-c.h b/include/qpdf/qpdf-c.h index cf32976f..886e521f 100644 --- a/include/qpdf/qpdf-c.h +++ b/include/qpdf/qpdf-c.h @@ -113,7 +113,9 @@ extern "C" { char const* qpdf_get_qpdf_version(); /* Returns dynamically allocated qpdf_data pointer; must be freed - * by calling qpdf_cleanup. + * by calling qpdf_cleanup. You must call qpdf_read or one of the + * other qpdf_read_* functions before calling any function that + * would need to operate on the PDF file. */ QPDF_DLL qpdf_data qpdf_init(); diff --git a/libqpdf/QPDF.cc b/libqpdf/QPDF.cc index 4c9296ea..c81aede0 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -85,8 +85,10 @@ class InvalidInputSource: public InputSource private: void throwException() { - throw std::runtime_error( - "QPDF operation attempted after closing input source"); + throw std::logic_error( + "QPDF operation attempted on a QPDF object with no input source." + " QPDF operations are invalid before processFile (or another" + " process method) or after closeInputSource"); } }; diff --git a/qpdf/qtest/qpdf/test73.out b/qpdf/qtest/qpdf/test73.out index 435189fd..b23c4c00 100644 --- a/qpdf/qtest/qpdf/test73.out +++ b/qpdf/qtest/qpdf/test73.out @@ -1,2 +1,3 @@ -WARNING: closed input source: object 1/0: error reading object: QPDF operation attempted after closing input source +getRoot: attempted to dereference an uninitialized QPDFObjectHandle +WARNING: closed input source: object 1/0: error reading object: QPDF operation attempted on a QPDF object with no input source. QPDF operations are invalid before processFile (or another process method) or after closeInputSource closed input source: unable to find /Root dictionary diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc index 1d995bb7..7d5b2ece 100644 --- a/qpdf/test_driver.cc +++ b/qpdf/test_driver.cc @@ -2715,6 +2715,16 @@ void runtest(int n, char const* filename1, char const* arg2) } else if (n == 73) { + try + { + QPDF pdf2; + pdf2.getRoot(); + } + catch (std::exception& e) + { + std::cerr << "getRoot: " << e.what() << std::endl; + } + pdf.closeInputSource(); pdf.getRoot().getKey("/Pages").unparseResolved(); } -- cgit v1.2.3-54-g00ecf