Commit 720ce9e8f333ba3911fa8003f08fd8813c19181a

Authored by Jay Berkenbilt
1 parent ac17308c

Improve testing and error handling around operating before processing

include/qpdf/qpdf-c.h
... ... @@ -113,7 +113,9 @@ extern "C" {
113 113 char const* qpdf_get_qpdf_version();
114 114  
115 115 /* Returns dynamically allocated qpdf_data pointer; must be freed
116   - * by calling qpdf_cleanup.
  116 + * by calling qpdf_cleanup. You must call qpdf_read or one of the
  117 + * other qpdf_read_* functions before calling any function that
  118 + * would need to operate on the PDF file.
117 119 */
118 120 QPDF_DLL
119 121 qpdf_data qpdf_init();
... ...
libqpdf/QPDF.cc
... ... @@ -85,8 +85,10 @@ class InvalidInputSource: public InputSource
85 85 private:
86 86 void throwException()
87 87 {
88   - throw std::runtime_error(
89   - "QPDF operation attempted after closing input source");
  88 + throw std::logic_error(
  89 + "QPDF operation attempted on a QPDF object with no input source."
  90 + " QPDF operations are invalid before processFile (or another"
  91 + " process method) or after closeInputSource");
90 92 }
91 93 };
92 94  
... ...
qpdf/qtest/qpdf/test73.out
1   -WARNING: closed input source: object 1/0: error reading object: QPDF operation attempted after closing input source
  1 +getRoot: attempted to dereference an uninitialized QPDFObjectHandle
  2 +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
2 3 closed input source: unable to find /Root dictionary
... ...
qpdf/test_driver.cc
... ... @@ -2715,6 +2715,16 @@ void runtest(int n, char const* filename1, char const* arg2)
2715 2715 }
2716 2716 else if (n == 73)
2717 2717 {
  2718 + try
  2719 + {
  2720 + QPDF pdf2;
  2721 + pdf2.getRoot();
  2722 + }
  2723 + catch (std::exception& e)
  2724 + {
  2725 + std::cerr << "getRoot: " << e.what() << std::endl;
  2726 + }
  2727 +
2718 2728 pdf.closeInputSource();
2719 2729 pdf.getRoot().getKey("/Pages").unparseResolved();
2720 2730 }
... ...