Commit 26e0bf461041856ea8159c86556524e4b158efe7

Authored by m-holger
1 parent 37f7a734

In QPDFParser::parse refactor eof handling

libqpdf/QPDFParser.cc
... ... @@ -81,12 +81,16 @@ QPDFParser::parse(bool& empty, bool content_stream)
81 81  
82 82 switch (tokenizer.getType()) {
83 83 case QPDFTokenizer::tt_eof:
84   - if (!content_stream) {
85   - QTC::TC("qpdf", "QPDFParser eof in parse");
86   - warn("unexpected EOF");
  84 + if (state_stack.size() > 1) {
  85 + warn("parse error while reading object");
87 86 }
88   - state = st_eof;
89   - break;
  87 + if (content_stream) {
  88 + // In content stream mode, leave object uninitialized to indicate EOF
  89 + return {};
  90 + }
  91 + QTC::TC("qpdf", "QPDFParser eof in parse");
  92 + warn("unexpected EOF");
  93 + return {QPDF_Null::create()};
90 94  
91 95 case QPDFTokenizer::tt_bad:
92 96 QTC::TC("qpdf", "QPDFParser bad token in parse");
... ... @@ -259,24 +263,11 @@ QPDFParser::parse(bool& empty, bool content_stream)
259 263 break;
260 264 }
261 265  
262   - if (object == nullptr && !is_null &&
263   - (!((state == st_start) || (state == st_stop) || (state == st_eof)))) {
  266 + if (object == nullptr && !is_null && (!(state == st_start || state == st_stop))) {
264 267 throw std::logic_error("QPDFParser:parseInternal: unexpected uninitialized object");
265   - is_null = true;
266 268 }
267 269  
268 270 switch (state) {
269   - case st_eof:
270   - if (state_stack.size() > 1) {
271   - warn("parse error while reading object");
272   - }
273   - done = true;
274   - // In content stream mode, leave object uninitialized to indicate EOF
275   - if (!content_stream) {
276   - is_null = true;
277   - }
278   - break;
279   -
280 271 case st_dictionary:
281 272 case st_array:
282 273 if (is_null) {
... ...
libqpdf/qpdf/QPDFParser.hh
... ... @@ -31,7 +31,7 @@ class QPDFParser
31 31 QPDFObjectHandle parse(bool& empty, bool content_stream);
32 32  
33 33 private:
34   - enum parser_state_e { st_top, st_start, st_stop, st_eof, st_dictionary, st_array };
  34 + enum parser_state_e { st_top, st_start, st_stop, st_dictionary, st_array };
35 35  
36 36 bool tooManyBadTokens();
37 37 void warn(qpdf_offset_t offset, std::string const& msg) const;
... ...
qpdf/qtest/qpdf/bad16-recover.out
1 1 WARNING: bad16.pdf (trailer, offset 753): unexpected dictionary close token
2 2 WARNING: bad16.pdf (trailer, offset 756): unexpected dictionary close token
3 3 WARNING: bad16.pdf (trailer, offset 759): unknown token while reading object; treating as string
4   -WARNING: bad16.pdf (trailer, offset 779): unexpected EOF
5 4 WARNING: bad16.pdf (trailer, offset 779): parse error while reading object
  5 +WARNING: bad16.pdf (trailer, offset 779): unexpected EOF
6 6 WARNING: bad16.pdf: file is damaged
7 7 WARNING: bad16.pdf (offset 712): expected trailer dictionary
8 8 WARNING: bad16.pdf: Attempting to reconstruct cross-reference table
9 9 WARNING: bad16.pdf (trailer, offset 753): unexpected dictionary close token
10 10 WARNING: bad16.pdf (trailer, offset 756): unexpected dictionary close token
11 11 WARNING: bad16.pdf (trailer, offset 759): unknown token while reading object; treating as string
12   -WARNING: bad16.pdf (trailer, offset 779): unexpected EOF
13 12 WARNING: bad16.pdf (trailer, offset 779): parse error while reading object
  13 +WARNING: bad16.pdf (trailer, offset 779): unexpected EOF
14 14 bad16.pdf: unable to find trailer dictionary while recovering damaged file
... ...
qpdf/qtest/qpdf/bad16.out
1 1 WARNING: bad16.pdf (trailer, offset 753): unexpected dictionary close token
2 2 WARNING: bad16.pdf (trailer, offset 756): unexpected dictionary close token
3 3 WARNING: bad16.pdf (trailer, offset 759): unknown token while reading object; treating as string
4   -WARNING: bad16.pdf (trailer, offset 779): unexpected EOF
5 4 WARNING: bad16.pdf (trailer, offset 779): parse error while reading object
  5 +WARNING: bad16.pdf (trailer, offset 779): unexpected EOF
6 6 bad16.pdf (offset 712): expected trailer dictionary
... ...