diff --git a/libqpdf/QPDFParser.cc b/libqpdf/QPDFParser.cc index faeba7e..d305031 100644 --- a/libqpdf/QPDFParser.cc +++ b/libqpdf/QPDFParser.cc @@ -324,6 +324,12 @@ QPDFParser::parseRemainder(bool content_stream) add(std::move(object)); } else { QTC::TC("qpdf", "QPDFParser bad array close in parseRemainder"); + if (sanity_checks) { + // During sanity checks, assume nesting of containers is corrupt and object is + // unusable. + warn("unexpected array close token; giving up on reading object"); + return {QPDFObject::create()}; + } warn("treating unexpected array close token as null"); if (tooManyBadTokens()) { return {QPDFObject::create()}; @@ -374,6 +380,12 @@ QPDFParser::parseRemainder(bool content_stream) add(std::move(object)); } else { QTC::TC("qpdf", "QPDFParser bad dictionary close in parseRemainder"); + if (sanity_checks) { + // During sanity checks, assume nesting of containers is corrupt and object is + // unusable. + warn("unexpected dictionary close token; giving up on reading object"); + return {QPDFObject::create()}; + } warn("unexpected dictionary close token"); if (tooManyBadTokens()) { return {QPDFObject::create()}; @@ -436,6 +448,15 @@ QPDFParser::parseRemainder(bool content_stream) if (content_stream) { addScalar(tokenizer.getValue()); } else { + if (sanity_checks && + (tokenizer.getValue() == "endobj" || tokenizer.getValue() == "endstream")) { + // During sanity checks, assume an unexpected endobj or endstream indicates that + // we are parsing past the end of the object. + warn( + "unexpected 'endobj' or 'endstream' while reading object; giving up on " + "reading object"); + return {QPDFObject::create()}; + } QTC::TC("qpdf", "QPDFParser treat word as string in parseRemainder"); warn("unknown token while reading object; treating as string"); if (tooManyBadTokens()) { diff --git a/qpdf/qtest/qpdf/bad16-recover.out b/qpdf/qtest/qpdf/bad16-recover.out index ad88d10..a278777 100644 --- a/qpdf/qtest/qpdf/bad16-recover.out +++ b/qpdf/qtest/qpdf/bad16-recover.out @@ -6,11 +6,7 @@ WARNING: bad16.pdf (trailer, offset 779): unexpected EOF WARNING: bad16.pdf: file is damaged WARNING: bad16.pdf (offset 712): expected trailer dictionary WARNING: bad16.pdf: Attempting to reconstruct cross-reference table -WARNING: bad16.pdf (trailer, offset 753): unexpected dictionary close token -WARNING: bad16.pdf (trailer, offset 756): unexpected dictionary close token -WARNING: bad16.pdf (trailer, offset 759): unknown token while reading object; treating as string -WARNING: bad16.pdf (trailer, offset 779): parse error while reading object -WARNING: bad16.pdf (trailer, offset 779): unexpected EOF +WARNING: bad16.pdf (trailer, offset 753): unexpected dictionary close token; giving up on reading object WARNING: bad16.pdf: unable to find trailer dictionary while recovering damaged file /QTest is implicit /QTest is direct and has type null (2) diff --git a/qpdf/qtest/qpdf/issue-335a.out b/qpdf/qtest/qpdf/issue-335a.out index b550bba..f34b3ff 100644 --- a/qpdf/qtest/qpdf/issue-335a.out +++ b/qpdf/qtest/qpdf/issue-335a.out @@ -144,28 +144,10 @@ WARNING: issue-335a.pdf (trailer, offset 21452): invalid character (-) in hexstr WARNING: issue-335a.pdf (trailer, offset 21436): stream keyword found in trailer WARNING: issue-335a.pdf (trailer, offset 21407): recovered trailer has no /Root entry WARNING: issue-335a.pdf (trailer, offset 21287): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21389): unexpected dictionary close token -WARNING: issue-335a.pdf (trailer, offset 21392): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21400): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21430): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21438): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21441): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21444): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21452): invalid character (-) in hexstring -WARNING: issue-335a.pdf (trailer, offset 21819): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21819): too many errors; giving up on reading object +WARNING: issue-335a.pdf (trailer, offset 21389): unexpected dictionary close token; giving up on reading object WARNING: issue-335a.pdf (trailer, offset 21277): unknown token while reading object; treating as string WARNING: issue-335a.pdf (trailer, offset 21287): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21389): unexpected dictionary close token -WARNING: issue-335a.pdf (trailer, offset 21392): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21400): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21430): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21438): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21441): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21444): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21452): invalid character (-) in hexstring -WARNING: issue-335a.pdf (trailer, offset 21819): unknown token while reading object; treating as string -WARNING: issue-335a.pdf (trailer, offset 21819): too many errors; giving up on reading object +WARNING: issue-335a.pdf (trailer, offset 21389): unexpected dictionary close token; giving up on reading object WARNING: issue-335a.pdf (trailer, offset 21228): treating unexpected brace token as null WARNING: issue-335a.pdf (trailer, offset 21229): unexpected ) WARNING: issue-335a.pdf (trailer, offset 21230): unknown token while reading object; treating as string @@ -1003,4 +985,20 @@ WARNING: issue-335a.pdf (trailer, offset 499): unexpected ) WARNING: issue-335a.pdf (trailer, offset 563): unexpected ) WARNING: issue-335a.pdf (trailer, offset 596): unexpected ) WARNING: issue-335a.pdf (trailer, offset 596): too many errors; giving up on reading object +WARNING: issue-335a.pdf (trailer, offset 461): invalid character (<) in hexstring +WARNING: issue-335a.pdf (trailer, offset 447): unknown token while reading object; treating as string +WARNING: issue-335a.pdf (trailer, offset 450): unknown token while reading object; treating as string +WARNING: issue-335a.pdf (trailer, offset 461): invalid character (<) in hexstring +WARNING: issue-335a.pdf (trailer, offset 464): invalid character (¨) in hexstring +WARNING: issue-335a.pdf (trailer, offset 499): unexpected ) +WARNING: issue-335a.pdf (trailer, offset 563): unexpected ) +WARNING: issue-335a.pdf (trailer, offset 563): too many errors; giving up on reading object +WARNING: issue-335a.pdf (trailer, offset 431): treating unexpected brace token as null +WARNING: issue-335a.pdf (trailer, offset 432): unexpected ) +WARNING: issue-335a.pdf (trailer, offset 433): unexpected ) +WARNING: issue-335a.pdf (trailer, offset 563): unexpected ) +WARNING: issue-335a.pdf (trailer, offset 596): unexpected ) +WARNING: issue-335a.pdf (trailer, offset 597): name with stray # will not work with PDF >= 1.2 +WARNING: issue-335a.pdf (trailer, offset 600): unexpected ) +WARNING: issue-335a.pdf (trailer, offset 600): too many errors; giving up on reading object qpdf: issue-335a.pdf: too many errors while reconstructing cross-reference table diff --git a/qpdf/qtest/qpdf/issue-99.out b/qpdf/qtest/qpdf/issue-99.out index 59abe38..d0554d2 100644 --- a/qpdf/qtest/qpdf/issue-99.out +++ b/qpdf/qtest/qpdf/issue-99.out @@ -11,28 +11,14 @@ WARNING: issue-99.pdf (object 1 0, offset 855): too many errors; giving up on re WARNING: issue-99.pdf (object 1 0, offset 858): expected endobj WARNING: issue-99.pdf (object 2 0, offset 64): expected endobj WARNING: issue-99.pdf (object 5 0, offset 2452): unknown token while reading object; treating as string -WARNING: issue-99.pdf (object 6 0, offset 2506): treating unexpected array close token as null -WARNING: issue-99.pdf (object 6 0, offset 2507): unknown token while reading object; treating as string -WARNING: issue-99.pdf (object 6 0, offset 2551): unknown token while reading object; treating as string -WARNING: issue-99.pdf (object 6 0, offset 2475): dictionary has duplicated key /Type; last occurrence overrides earlier ones -WARNING: issue-99.pdf (object 6 0, offset 2475): dictionary has duplicated key /Parent; last occurrence overrides earlier ones -WARNING: issue-99.pdf (object 6 0, offset 2475): dictionary has duplicated key /Group; last occurrence overrides earlier ones -WARNING: issue-99.pdf (object 6 0, offset 2475): expected dictionary key but found non-name object; inserting key /QPDFFake1 -WARNING: issue-99.pdf (object 6 0, offset 2475): expected dictionary key but found non-name object; inserting key /QPDFFake2 -WARNING: issue-99.pdf (object 6 0, offset 2475): expected dictionary key but found non-name object; inserting key /QPDFFake3 +WARNING: issue-99.pdf (object 6 0, offset 2506): unexpected array close token; giving up on reading object +WARNING: issue-99.pdf (object 6 0, offset 2507): expected endobj WARNING: issue-99.pdf (object 10 0, offset 3708): expected dictionary key but found non-name object; inserting key /QPDFFake1 WARNING: issue-99.pdf (object 11 0, offset 4485): unknown token while reading object; treating as string -WARNING: issue-99.pdf (object 11 0, offset 4497): treating unexpected array close token as null -WARNING: issue-99.pdf (object 11 0, offset 4502): unexpected ) -WARNING: issue-99.pdf (object 11 0, offset 4504): unexpected > -WARNING: issue-99.pdf (object 11 0, offset 4508): unknown token while reading object; treating as string -WARNING: issue-99.pdf (object 11 0, offset 4520): unknown token while reading object; treating as string -WARNING: issue-99.pdf (object 11 0, offset 4520): too many errors; giving up on reading object -WARNING: issue-99.pdf (object 11 0, offset 4524): expected endobj +WARNING: issue-99.pdf (object 11 0, offset 4497): unexpected array close token; giving up on reading object +WARNING: issue-99.pdf (object 11 0, offset 4499): expected endobj WARNING: issue-99.pdf: unable to find trailer dictionary while recovering damaged file WARNING: object 1 0: Pages tree includes non-dictionary object; ignoring WARNING: object 1 0: operation for dictionary attempted on object of type null: returning false for a key containment request WARNING: object 1 0: operation for dictionary attempted on object of type null: ignoring key replacement request -WARNING: object 1 0: operation for dictionary attempted on object of type null: returning false for a key containment request -WARNING: object 1 0: operation for dictionary attempted on object of type null: ignoring key replacement request qpdf: issue-99.pdf: unable to find any pages while recovering damaged file