Commit a94a729fee946947db7a056f02b326dfce681128

Authored by Jay Berkenbilt
1 parent ede1255a

Explicitly check root dictionary type

Very badly corrupted files may not have a retrievable root dictionary.
Handle that as a special case so that a more helpful error message can
be provided.
libqpdf/QPDF.cc
@@ -2028,7 +2028,14 @@ QPDF::getTrailer() @@ -2028,7 +2028,14 @@ QPDF::getTrailer()
2028 QPDFObjectHandle 2028 QPDFObjectHandle
2029 QPDF::getRoot() 2029 QPDF::getRoot()
2030 { 2030 {
2031 - return this->trailer.getKey("/Root"); 2031 + QPDFObjectHandle root = this->trailer.getKey("/Root");
  2032 + if (! root.isDictionary())
  2033 + {
  2034 + throw QPDFExc(qpdf_e_damaged_pdf, file->getName(),
  2035 + "", file->getLastOffset(),
  2036 + "unable to find /Root dictionary");
  2037 + }
  2038 + return root;
2032 } 2039 }
2033 2040
2034 void 2041 void
qpdf/qtest/qpdf/bad35-recover.out
1 WARNING: bad35.pdf (object 1 0, file position 521): supposed object stream 1 has wrong type 1 WARNING: bad35.pdf (object 1 0, file position 521): supposed object stream 1 has wrong type
2 -operation for Dictionary object attempted on object of wrong type 2 +bad35.pdf (file position 521): unable to find /Root dictionary
qpdf/qtest/qpdf/bad35.out
1 WARNING: bad35.pdf (object 1 0, file position 521): supposed object stream 1 has wrong type 1 WARNING: bad35.pdf (object 1 0, file position 521): supposed object stream 1 has wrong type
2 -operation for Dictionary object attempted on object of wrong type 2 +bad35.pdf (file position 521): unable to find /Root dictionary
qpdf/qtest/qpdf/issue-100.out
@@ -12,4 +12,4 @@ WARNING: issue-100.pdf (object 5 0, file position 418): /Length key in stream di @@ -12,4 +12,4 @@ WARNING: issue-100.pdf (object 5 0, file position 418): /Length key in stream di
12 WARNING: issue-100.pdf (object 5 0, file position 489): attempting to recover stream length 12 WARNING: issue-100.pdf (object 5 0, file position 489): attempting to recover stream length
13 WARNING: issue-100.pdf (trailer, file position 953): expected dictionary key but found non-name object; inserting key /QPDFFake1 13 WARNING: issue-100.pdf (trailer, file position 953): expected dictionary key but found non-name object; inserting key /QPDFFake1
14 WARNING: issue-100.pdf (trailer, file position 953): dictionary ended prematurely; using null as value for last key 14 WARNING: issue-100.pdf (trailer, file position 953): dictionary ended prematurely; using null as value for last key
15 -operation for Dictionary object attempted on object of wrong type 15 +issue-100.pdf (file position 1138): unable to find /Root dictionary
qpdf/qtest/qpdf/issue-118.out
1 WARNING: issue-118.pdf (file position 732): loop detected resolving object 2 0 1 WARNING: issue-118.pdf (file position 732): loop detected resolving object 2 0
2 WARNING: issue-118.pdf (xref stream: object 8 0, file position 732): supposed object stream 2 is not a stream 2 WARNING: issue-118.pdf (xref stream: object 8 0, file position 732): supposed object stream 2 is not a stream
3 -operation for Dictionary object attempted on object of wrong type 3 +issue-118.pdf (file position 732): unable to find /Root dictionary
qpdf/qtest/qpdf/issue-99.out
1 WARNING: issue-99.pdf: file is damaged 1 WARNING: issue-99.pdf: file is damaged
2 WARNING: issue-99.pdf (file position 3526): xref not found 2 WARNING: issue-99.pdf (file position 3526): xref not found
3 WARNING: issue-99.pdf: Attempting to reconstruct cross-reference table 3 WARNING: issue-99.pdf: Attempting to reconstruct cross-reference table
4 -operation for Dictionary object attempted on object of wrong type 4 +issue-99.pdf (file position 4793): unable to find /Root dictionary
qpdf/qtest/qpdf/issue-99b.out
@@ -2,4 +2,4 @@ WARNING: issue-99b.pdf: file is damaged @@ -2,4 +2,4 @@ WARNING: issue-99b.pdf: file is damaged
2 WARNING: issue-99b.pdf (object 1 0, file position 9): object with ID 0 2 WARNING: issue-99b.pdf (object 1 0, file position 9): object with ID 0
3 WARNING: issue-99b.pdf: Attempting to reconstruct cross-reference table 3 WARNING: issue-99b.pdf: Attempting to reconstruct cross-reference table
4 WARNING: issue-99b.pdf: object 1 0 not found in file after regenerating cross reference table 4 WARNING: issue-99b.pdf: object 1 0 not found in file after regenerating cross reference table
5 -operation for Dictionary object attempted on object of wrong type 5 +issue-99b.pdf (file position 757): unable to find /Root dictionary