Commit cc628d9b5786bf84efbae3ed22b8cda99d935a73

Authored by m-holger
1 parent 9e358a32

Fix error handling for missing pages in QPDFJob::doCheck (fixes #1674)

libqpdf/QPDFJob.cc
@@ -745,13 +745,17 @@ QPDFJob::doCheck(QPDF& pdf) @@ -745,13 +745,17 @@ QPDFJob::doCheck(QPDF& pdf)
745 745
746 // Create all document helper to trigger any validations they carry out. 746 // Create all document helper to trigger any validations they carry out.
747 auto& doc = pdf.doc(); 747 auto& doc = pdf.doc();
748 - auto& pages = doc.page_dh(); 748 + auto pages = doc.page_dh().getAllPages();
749 (void)doc.acroform(); 749 (void)doc.acroform();
750 (void)doc.embedded_files(); 750 (void)doc.embedded_files();
751 (void)doc.page_labels(); 751 (void)doc.page_labels();
752 - (void)doc.outlines().resolveNamedDest(QPDFObjectHandle::newString("dummy"));  
753 - (void)doc.outlines().getOutlinesForPage(pages.getAllPages().at(0));  
754 - 752 + (void)doc.outlines().resolveNamedDest(String("dummy"));
  753 + if (pages.empty()) {
  754 + okay = false;
  755 + *m->log->getError() << "ERROR: file does not contain any pages\n";
  756 + } else {
  757 + (void)doc.outlines().getOutlinesForPage(pages.at(0));
  758 + }
755 // Write the file to nowhere, uncompressing streams. This causes full file traversal and 759 // Write the file to nowhere, uncompressing streams. This causes full file traversal and
756 // decoding of all streams we can decode. 760 // decoding of all streams we can decode.
757 Writer::Config cfg; 761 Writer::Config cfg;
@@ -766,7 +770,7 @@ QPDFJob::doCheck(QPDF&amp; pdf) @@ -766,7 +770,7 @@ QPDFJob::doCheck(QPDF&amp; pdf)
766 770
767 // Parse all content streams 771 // Parse all content streams
768 int pageno = 0; 772 int pageno = 0;
769 - for (auto& page: pages.getAllPages()) { 773 + for (auto& page: pages) {
770 ++pageno; 774 ++pageno;
771 try { 775 try {
772 page.parseContents(nullptr); 776 page.parseContents(nullptr);
manual/release-notes.rst
@@ -13,6 +13,11 @@ more detail. @@ -13,6 +13,11 @@ more detail.
13 13
14 .. x.y.z: not yet released 14 .. x.y.z: not yet released
15 15
  16 +12.3.3: not yet released
  17 + - Bug fixes
  18 +
  19 + - Fix error message when :qpdf:ref:`--check` encounters a file without any pages.
  20 +
16 12.3.2: January 24, 2026 21 12.3.2: January 24, 2026
17 - Bug fixes 22 - Bug fixes
18 23
qpdf/qtest/page-errors.test
@@ -14,7 +14,7 @@ cleanup(); @@ -14,7 +14,7 @@ cleanup();
14 14
15 my $td = new TestDriver('page-errors'); 15 my $td = new TestDriver('page-errors');
16 16
17 -my $n_tests = 9; 17 +my $n_tests = 10;
18 18
19 $td->runtest("handle page no with contents", 19 $td->runtest("handle page no with contents",
20 {$td->COMMAND => "qpdf --show-pages page-no-content.pdf"}, 20 {$td->COMMAND => "qpdf --show-pages page-no-content.pdf"},
@@ -49,6 +49,10 @@ $td-&gt;runtest(&quot;detect loops in pages structure&quot;, @@ -49,6 +49,10 @@ $td-&gt;runtest(&quot;detect loops in pages structure&quot;,
49 {$td->COMMAND => "qpdf --check pages-loop.pdf"}, 49 {$td->COMMAND => "qpdf --check pages-loop.pdf"},
50 {$td->FILE => "pages-loop.out", $td->EXIT_STATUS => 2}, 50 {$td->FILE => "pages-loop.out", $td->EXIT_STATUS => 2},
51 $td->NORMALIZE_NEWLINES); 51 $td->NORMALIZE_NEWLINES);
  52 +$td->runtest("file with zero pages",
  53 + {$td->COMMAND => "qpdf --check empty.pdf"},
  54 + {$td->FILE => "empty-check.out", $td->EXIT_STATUS => 2},
  55 + $td->NORMALIZE_NEWLINES);
52 56
53 cleanup(); 57 cleanup();
54 $td->report($n_tests); 58 $td->report($n_tests);
qpdf/qtest/qpdf/empty-check.out 0 → 100644
  1 +checking empty.pdf
  2 +PDF Version: 1.3
  3 +File is not encrypted
  4 +File is not linearized
  5 +ERROR: file does not contain any pages
  6 +qpdf: errors detected
qpdf/qtest/qpdf/empty.pdf 0 → 100644
  1 +%PDF-1.3
  2 +%¿÷¢þ
  3 +1 0 obj
  4 +<< /Pages 2 0 R /Type /Catalog >>
  5 +endobj
  6 +2 0 obj
  7 +<< /Count 0 /Kids [ ] /Type /Pages >>
  8 +endobj
  9 +xref
  10 +0 3
  11 +0000000000 65535 f
  12 +0000000015 00000 n
  13 +0000000064 00000 n
  14 +trailer << /Root 1 0 R /Size 3 /ID [<c340298302ab3c4e01d6f14c08b392dd><c340298302ab3c4e01d6f14c08b392dd>] >>
  15 +startxref
  16 +117
  17 +%%EOF