Coverage Report

Created: 2025-12-05 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/QPDFParser.cc
Line
Count
Source
1
#include <qpdf/QPDFParser.hh>
2
3
#include <qpdf/QPDF.hh>
4
#include <qpdf/QPDFObjGen.hh>
5
#include <qpdf/QPDFObjectHandle.hh>
6
#include <qpdf/QPDFObject_private.hh>
7
#include <qpdf/QPDFTokenizer_private.hh>
8
#include <qpdf/QTC.hh>
9
#include <qpdf/QUtil.hh>
10
11
#include <memory>
12
13
using namespace std::literals;
14
using namespace qpdf;
15
16
using ObjectPtr = std::shared_ptr<QPDFObject>;
17
18
static uint32_t const& max_nesting{global::Limits::parser_max_nesting()};
19
20
// The ParseGuard class allows QPDFParser to detect re-entrant parsing. It also provides
21
// special access to allow the parser to create unresolved objects and dangling references.
22
class QPDF::Doc::ParseGuard
23
{
24
  public:
25
    ParseGuard(QPDF* qpdf) :
26
18.3k
        objects(qpdf ? &qpdf->m->objects : nullptr)
27
18.3k
    {
28
18.3k
        if (objects) {
29
7.78k
            objects->inParse(true);
30
7.78k
        }
31
18.3k
    }
32
33
    static std::shared_ptr<QPDFObject>
34
    getObject(QPDF* qpdf, int id, int gen, bool parse_pdf)
35
0
    {
36
0
        return qpdf->m->objects.getObjectForParser(id, gen, parse_pdf);
37
0
    }
38
39
    ~ParseGuard()
40
18.3k
    {
41
18.3k
        if (objects) {
42
7.78k
            objects->inParse(false);
43
7.78k
        }
44
18.3k
    }
45
    QPDF::Doc::Objects* objects;
46
};
47
48
using ParseGuard = QPDF::Doc::ParseGuard;
49
50
QPDFObjectHandle
51
QPDFParser::parse(InputSource& input, std::string const& object_description, QPDF* context)
52
10.5k
{
53
10.5k
    qpdf::Tokenizer tokenizer;
54
10.5k
    if (auto result = QPDFParser(
55
10.5k
                          input,
56
10.5k
                          make_description(input.getName(), object_description),
57
10.5k
                          object_description,
58
10.5k
                          tokenizer,
59
10.5k
                          nullptr,
60
10.5k
                          context,
61
10.5k
                          false)
62
10.5k
                          .parse()) {
63
10.2k
        return result;
64
10.2k
    }
65
321
    return {QPDFObject::create<QPDF_Null>()};
66
10.5k
}
67
68
QPDFObjectHandle
69
QPDFParser::parse_content(
70
    InputSource& input,
71
    std::shared_ptr<QPDFObject::Description> sp_description,
72
    qpdf::Tokenizer& tokenizer,
73
    QPDF* context)
74
0
{
75
0
    static const std::string content("content"); // GCC12 - make constexpr
76
0
    auto p = QPDFParser(
77
0
        input,
78
0
        std::move(sp_description),
79
0
        content,
80
0
        tokenizer,
81
0
        nullptr,
82
0
        context,
83
0
        true,
84
0
        0,
85
0
        0,
86
0
        context && context->doc().reconstructed_xref());
87
0
    auto result = p.parse(true);
88
0
    if (result || p.empty_) {
89
        // In content stream mode, leave object uninitialized to indicate EOF
90
0
        return result;
91
0
    }
92
0
    return {QPDFObject::create<QPDF_Null>()};
93
0
}
94
95
QPDFObjectHandle
96
QPDFParser::parse(
97
    InputSource& input,
98
    std::string const& object_description,
99
    QPDFTokenizer& tokenizer,
100
    bool& empty,
101
    QPDFObjectHandle::StringDecrypter* decrypter,
102
    QPDF* context)
103
0
{
104
    // ABI: This parse overload is only used by the deprecated QPDFObjectHandle::parse. It is the
105
    // only user of the 'empty' member. When removing this overload also remove 'empty'.
106
0
    auto p = QPDFParser(
107
0
        input,
108
0
        make_description(input.getName(), object_description),
109
0
        object_description,
110
0
        *tokenizer.m,
111
0
        decrypter,
112
0
        context,
113
0
        false);
114
0
    auto result = p.parse();
115
0
    empty = p.empty_;
116
0
    if (result) {
117
0
        return result;
118
0
    }
119
0
    return {QPDFObject::create<QPDF_Null>()};
120
0
}
121
122
QPDFObjectHandle
123
QPDFParser::parse(
124
    InputSource& input,
125
    std::string const& object_description,
126
    qpdf::Tokenizer& tokenizer,
127
    QPDFObjectHandle::StringDecrypter* decrypter,
128
    QPDF& context,
129
    bool sanity_checks)
130
7.78k
{
131
7.78k
    return QPDFParser(
132
7.78k
               input,
133
7.78k
               make_description(input.getName(), object_description),
134
7.78k
               object_description,
135
7.78k
               tokenizer,
136
7.78k
               decrypter,
137
7.78k
               &context,
138
7.78k
               true,
139
7.78k
               0,
140
7.78k
               0,
141
7.78k
               sanity_checks)
142
7.78k
        .parse();
143
7.78k
}
144
145
QPDFObjectHandle
146
QPDFParser::parse(
147
    is::OffsetBuffer& input, int stream_id, int obj_id, qpdf::Tokenizer& tokenizer, QPDF& context)
148
0
{
149
0
    return QPDFParser(
150
0
               input,
151
0
               std::make_shared<QPDFObject::Description>(
152
0
                   QPDFObject::ObjStreamDescr(stream_id, obj_id)),
153
0
               "",
154
0
               tokenizer,
155
0
               nullptr,
156
0
               &context,
157
0
               true,
158
0
               stream_id,
159
0
               obj_id)
160
0
        .parse();
161
0
}
162
163
QPDFObjectHandle
164
QPDFParser::parse(bool content_stream)
165
18.3k
{
166
18.3k
    try {
167
18.3k
        return parse_first(content_stream);
168
18.3k
    } catch (Error&) {
169
0
        return {};
170
321
    } catch (QPDFExc& e) {
171
321
        throw e;
172
321
    } catch (std::logic_error& e) {
173
0
        throw e;
174
0
    } catch (std::exception& e) {
175
0
        warn("treating object as null because of error during parsing : "s + e.what());
176
0
        return {};
177
0
    }
178
18.3k
}
179
180
QPDFObjectHandle
181
QPDFParser::parse_first(bool content_stream)
182
18.3k
{
183
    // This method must take care not to resolve any objects. Don't check the type of any object
184
    // without first ensuring that it is a direct object. Otherwise, doing so may have the side
185
    // effect of reading the object and changing the file pointer. If you do this, it will cause a
186
    // logic error to be thrown from QPDF::inParse().
187
188
18.3k
    QPDF::Doc::ParseGuard pg(context);
189
18.3k
    start = input.tell();
190
18.3k
    if (!tokenizer.nextToken(input, object_description)) {
191
321
        warn(tokenizer.getErrorMessage());
192
321
    }
193
194
18.3k
    switch (tokenizer.getType()) {
195
0
    case QPDFTokenizer::tt_eof:
196
0
        if (content_stream) {
197
            // In content stream mode, leave object uninitialized to indicate EOF
198
0
            empty_ = true;
199
0
            return {};
200
0
        }
201
0
        warn("unexpected EOF");
202
0
        return {};
203
204
0
    case QPDFTokenizer::tt_bad:
205
0
        return {};
206
207
0
    case QPDFTokenizer::tt_brace_open:
208
0
    case QPDFTokenizer::tt_brace_close:
209
0
        warn("treating unexpected brace token as null");
210
0
        return {};
211
212
0
    case QPDFTokenizer::tt_array_close:
213
0
        warn("treating unexpected array close token as null");
214
0
        return {};
215
216
0
    case QPDFTokenizer::tt_dict_close:
217
0
        warn("unexpected dictionary close token");
218
0
        return {};
219
220
0
    case QPDFTokenizer::tt_array_open:
221
7.78k
    case QPDFTokenizer::tt_dict_open:
222
7.78k
        stack.clear();
223
7.78k
        stack.emplace_back(
224
7.78k
            input,
225
7.78k
            (tokenizer.getType() == QPDFTokenizer::tt_array_open) ? st_array : st_dictionary_key);
226
7.78k
        frame = &stack.back();
227
7.78k
        return parseRemainder(content_stream);
228
229
0
    case QPDFTokenizer::tt_bool:
230
0
        return withDescription<QPDF_Bool>(tokenizer.getValue() == "true");
231
232
0
    case QPDFTokenizer::tt_null:
233
0
        return {QPDFObject::create<QPDF_Null>()};
234
235
0
    case QPDFTokenizer::tt_integer:
236
0
        return withDescription<QPDF_Integer>(QUtil::string_to_ll(tokenizer.getValue().c_str()));
237
238
0
    case QPDFTokenizer::tt_real:
239
0
        return withDescription<QPDF_Real>(tokenizer.getValue());
240
241
10.2k
    case QPDFTokenizer::tt_name:
242
10.2k
        return withDescription<QPDF_Name>(tokenizer.getValue());
243
244
0
    case QPDFTokenizer::tt_word:
245
0
        {
246
0
            auto const& value = tokenizer.getValue();
247
0
            if (content_stream) {
248
0
                return withDescription<QPDF_Operator>(value);
249
0
            } else if (value == "endobj") {
250
                // We just saw endobj without having read anything. Nothing in the PDF spec appears
251
                // to allow empty objects, but they have been encountered in actual PDF files and
252
                // Adobe Reader appears to ignore them. Treat this as a null and do not move the
253
                // input source's offset.
254
0
                empty_ = true;
255
0
                input.seek(input.getLastOffset(), SEEK_SET);
256
0
                if (!content_stream) {
257
0
                    warn("empty object treated as null");
258
0
                }
259
0
                return {};
260
0
            } else {
261
0
                warn("unknown token while reading object; treating as string");
262
0
                return withDescription<QPDF_String>(value);
263
0
            }
264
0
        }
265
266
0
    case QPDFTokenizer::tt_string:
267
0
        if (decrypter) {
268
0
            std::string s{tokenizer.getValue()};
269
0
            decrypter->decryptString(s);
270
0
            return withDescription<QPDF_String>(s);
271
0
        } else {
272
0
            return withDescription<QPDF_String>(tokenizer.getValue());
273
0
        }
274
275
0
    default:
276
0
        warn("treating unknown token type as null while reading object");
277
0
        return {};
278
18.3k
    }
279
18.3k
}
280
281
QPDFObjectHandle
282
QPDFParser::parseRemainder(bool content_stream)
283
7.78k
{
284
    // This method must take care not to resolve any objects. Don't check the type of any object
285
    // without first ensuring that it is a direct object. Otherwise, doing so may have the side
286
    // effect of reading the object and changing the file pointer. If you do this, it will cause a
287
    // logic error to be thrown from QPDF::inParse().
288
289
7.78k
    bad_count = 0;
290
7.78k
    bool b_contents = false;
291
292
23.3k
    while (true) {
293
23.3k
        if (!tokenizer.nextToken(input, object_description)) {
294
0
            warn(tokenizer.getErrorMessage());
295
0
        }
296
23.3k
        ++good_count; // optimistically
297
298
23.3k
        if (int_count != 0) {
299
            // Special handling of indirect references. Treat integer tokens as part of an indirect
300
            // reference until proven otherwise.
301
7.78k
            if (tokenizer.getType() == QPDFTokenizer::tt_integer) {
302
0
                if (++int_count > 2) {
303
                    // Process the oldest buffered integer.
304
0
                    addInt(int_count);
305
0
                }
306
0
                last_offset_buffer[int_count % 2] = input.getLastOffset();
307
0
                int_buffer[int_count % 2] = QUtil::string_to_ll(tokenizer.getValue().c_str());
308
0
                continue;
309
310
7.78k
            } else if (
311
7.78k
                int_count >= 2 && tokenizer.getType() == QPDFTokenizer::tt_word &&
312
0
                tokenizer.getValue() == "R") {
313
0
                if (!context) {
314
0
                    throw std::logic_error(
315
0
                        "QPDFParser::parse called without context on an object with indirect "
316
0
                        "references");
317
0
                }
318
0
                auto id = QIntC::to_int(int_buffer[(int_count - 1) % 2]);
319
0
                auto gen = QIntC::to_int(int_buffer[(int_count) % 2]);
320
0
                if (!(id < 1 || gen < 0 || gen >= 65535)) {
321
0
                    add(ParseGuard::getObject(context, id, gen, parse_pdf));
322
0
                } else {
323
0
                    add_bad_null(
324
0
                        "treating bad indirect reference (" + std::to_string(id) + " " +
325
0
                        std::to_string(gen) + " R) as null");
326
0
                }
327
0
                int_count = 0;
328
0
                continue;
329
330
7.78k
            } else if (int_count > 0) {
331
                // Process the buffered integers before processing the current token.
332
7.78k
                if (int_count > 1) {
333
0
                    addInt(int_count - 1);
334
0
                }
335
7.78k
                addInt(int_count);
336
7.78k
                int_count = 0;
337
7.78k
            }
338
7.78k
        }
339
340
23.3k
        switch (tokenizer.getType()) {
341
0
        case QPDFTokenizer::tt_eof:
342
0
            warn("parse error while reading object");
343
0
            if (content_stream) {
344
                // In content stream mode, leave object uninitialized to indicate EOF
345
0
                return {};
346
0
            }
347
0
            warn("unexpected EOF");
348
0
            return {};
349
350
0
        case QPDFTokenizer::tt_bad:
351
0
            check_too_many_bad_tokens();
352
0
            addNull();
353
0
            continue;
354
355
0
        case QPDFTokenizer::tt_brace_open:
356
0
        case QPDFTokenizer::tt_brace_close:
357
0
            add_bad_null("treating unexpected brace token as null");
358
0
            continue;
359
360
0
        case QPDFTokenizer::tt_array_close:
361
0
            if (frame->state == st_array) {
362
0
                auto object = frame->null_count > 100
363
0
                    ? QPDFObject::create<QPDF_Array>(std::move(frame->olist), true)
364
0
                    : QPDFObject::create<QPDF_Array>(std::move(frame->olist));
365
0
                setDescription(object, frame->offset - 1);
366
                // The `offset` points to the next of "[".  Set the rewind offset to point to the
367
                // beginning of "[". This has been explicitly tested with whitespace surrounding the
368
                // array start delimiter. getLastOffset points to the array end token and therefore
369
                // can't be used here.
370
0
                if (stack.size() <= 1) {
371
0
                    return object;
372
0
                }
373
0
                stack.pop_back();
374
0
                frame = &stack.back();
375
0
                add(std::move(object));
376
0
            } else {
377
0
                if (sanity_checks) {
378
                    // During sanity checks, assume nesting of containers is corrupt and object is
379
                    // unusable.
380
0
                    warn("unexpected array close token; giving up on reading object");
381
0
                    return {};
382
0
                }
383
0
                add_bad_null("treating unexpected array close token as null");
384
0
            }
385
0
            continue;
386
387
7.78k
        case QPDFTokenizer::tt_dict_close:
388
7.78k
            if (frame->state <= st_dictionary_value) {
389
                // Attempt to recover more or less gracefully from invalid dictionaries.
390
7.78k
                auto& dict = frame->dict;
391
392
7.78k
                if (frame->state == st_dictionary_value) {
393
0
                    warn(
394
0
                        frame->offset,
395
0
                        "dictionary ended prematurely; using null as value for last key");
396
0
                    dict[frame->key] = QPDFObject::create<QPDF_Null>();
397
0
                }
398
7.78k
                if (!frame->olist.empty()) {
399
0
                    if (sanity_checks) {
400
0
                        warn(
401
0
                            frame->offset,
402
0
                            "expected dictionary keys but found non-name objects; ignoring");
403
0
                    } else {
404
0
                        fixMissingKeys();
405
0
                    }
406
0
                }
407
408
7.78k
                if (!frame->contents_string.empty() && dict.contains("/Type") &&
409
0
                    dict["/Type"].isNameAndEquals("/Sig") && dict.contains("/ByteRange") &&
410
0
                    dict.contains("/Contents") && dict["/Contents"].isString()) {
411
0
                    dict["/Contents"] = QPDFObjectHandle::newString(frame->contents_string);
412
0
                    dict["/Contents"].setParsedOffset(frame->contents_offset);
413
0
                }
414
7.78k
                auto object = QPDFObject::create<QPDF_Dictionary>(std::move(dict));
415
7.78k
                setDescription(object, frame->offset - 2);
416
                // The `offset` points to the next of "<<". Set the rewind offset to point to the
417
                // beginning of "<<". This has been explicitly tested with whitespace surrounding
418
                // the dictionary start delimiter. getLastOffset points to the dictionary end token
419
                // and therefore can't be used here.
420
7.78k
                if (stack.size() <= 1) {
421
7.78k
                    return object;
422
7.78k
                }
423
0
                stack.pop_back();
424
0
                frame = &stack.back();
425
0
                add(std::move(object));
426
0
            } else {
427
0
                if (sanity_checks) {
428
                    // During sanity checks, assume nesting of containers is corrupt and object is
429
                    // unusable.
430
0
                    warn("unexpected dictionary close token; giving up on reading object");
431
0
                    return {};
432
0
                }
433
0
                add_bad_null("unexpected dictionary close token");
434
0
            }
435
0
            continue;
436
437
0
        case QPDFTokenizer::tt_array_open:
438
0
        case QPDFTokenizer::tt_dict_open:
439
0
            if (stack.size() > max_nesting) {
440
0
                limits_error(
441
0
                    "parser-max-nesting", "ignoring excessively deeply nested data structure");
442
0
            }
443
0
            b_contents = false;
444
0
            stack.emplace_back(
445
0
                input,
446
0
                (tokenizer.getType() == QPDFTokenizer::tt_array_open) ? st_array
447
0
                                                                      : st_dictionary_key);
448
0
            frame = &stack.back();
449
0
            continue;
450
451
0
        case QPDFTokenizer::tt_bool:
452
0
            addScalar<QPDF_Bool>(tokenizer.getValue() == "true");
453
0
            continue;
454
455
0
        case QPDFTokenizer::tt_null:
456
0
            addNull();
457
0
            continue;
458
459
7.78k
        case QPDFTokenizer::tt_integer:
460
7.78k
            if (!content_stream) {
461
                // Buffer token in case it is part of an indirect reference.
462
7.78k
                last_offset_buffer[1] = input.getLastOffset();
463
7.78k
                int_buffer[1] = QUtil::string_to_ll(tokenizer.getValue().c_str());
464
7.78k
                int_count = 1;
465
7.78k
            } else {
466
0
                addScalar<QPDF_Integer>(QUtil::string_to_ll(tokenizer.getValue().c_str()));
467
0
            }
468
7.78k
            continue;
469
470
0
        case QPDFTokenizer::tt_real:
471
0
            addScalar<QPDF_Real>(tokenizer.getValue());
472
0
            continue;
473
474
7.78k
        case QPDFTokenizer::tt_name:
475
7.78k
            if (frame->state == st_dictionary_key) {
476
7.78k
                frame->key = tokenizer.getValue();
477
7.78k
                frame->state = st_dictionary_value;
478
7.78k
                b_contents = decrypter && frame->key == "/Contents";
479
7.78k
                continue;
480
7.78k
            } else {
481
0
                addScalar<QPDF_Name>(tokenizer.getValue());
482
0
            }
483
0
            continue;
484
485
0
        case QPDFTokenizer::tt_word:
486
0
            if (content_stream) {
487
0
                addScalar<QPDF_Operator>(tokenizer.getValue());
488
0
                continue;
489
0
            }
490
491
0
            if (sanity_checks) {
492
0
                if (tokenizer.getValue() == "endobj" || tokenizer.getValue() == "endstream") {
493
                    // During sanity checks, assume an unexpected endobj or endstream indicates that
494
                    // we are parsing past the end of the object.
495
0
                    warn(
496
0
                        "unexpected 'endobj' or 'endstream' while reading object; giving up on "
497
0
                        "reading object");
498
0
                    return {};
499
0
                }
500
501
0
                add_bad_null("unknown token while reading object; treating as null");
502
0
                continue;
503
0
            }
504
505
0
            warn("unknown token while reading object; treating as string");
506
0
            check_too_many_bad_tokens();
507
0
            addScalar<QPDF_String>(tokenizer.getValue());
508
509
0
            continue;
510
511
0
        case QPDFTokenizer::tt_string:
512
0
            {
513
0
                auto const& val = tokenizer.getValue();
514
0
                if (decrypter) {
515
0
                    if (b_contents) {
516
0
                        frame->contents_string = val;
517
0
                        frame->contents_offset = input.getLastOffset();
518
0
                        b_contents = false;
519
0
                    }
520
0
                    std::string s{val};
521
0
                    decrypter->decryptString(s);
522
0
                    addScalar<QPDF_String>(s);
523
0
                } else {
524
0
                    addScalar<QPDF_String>(val);
525
0
                }
526
0
            }
527
0
            continue;
528
529
0
        default:
530
0
            add_bad_null("treating unknown token type as null while reading object");
531
23.3k
        }
532
23.3k
    }
533
7.78k
}
534
535
void
536
QPDFParser::add(std::shared_ptr<QPDFObject>&& obj)
537
7.78k
{
538
7.78k
    if (frame->state != st_dictionary_value) {
539
        // If state is st_dictionary_key then there is a missing key. Push onto olist for
540
        // processing once the tt_dict_close token has been found.
541
0
        frame->olist.emplace_back(std::move(obj));
542
7.78k
    } else {
543
7.78k
        if (auto res = frame->dict.insert_or_assign(frame->key, std::move(obj)); !res.second) {
544
0
            warnDuplicateKey();
545
0
        }
546
7.78k
        frame->state = st_dictionary_key;
547
7.78k
    }
548
7.78k
}
549
550
void
551
QPDFParser::addNull()
552
0
{
553
0
    const static ObjectPtr null_obj = QPDFObject::create<QPDF_Null>();
554
555
0
    if (frame->state != st_dictionary_value) {
556
        // If state is st_dictionary_key then there is a missing key. Push onto olist for
557
        // processing once the tt_dict_close token has been found.
558
0
        frame->olist.emplace_back(null_obj);
559
0
    } else {
560
0
        if (auto res = frame->dict.insert_or_assign(frame->key, null_obj); !res.second) {
561
0
            warnDuplicateKey();
562
0
        }
563
0
        frame->state = st_dictionary_key;
564
0
    }
565
0
    ++frame->null_count;
566
0
}
567
568
void
569
QPDFParser::add_bad_null(std::string const& msg)
570
0
{
571
0
    warn(msg);
572
0
    check_too_many_bad_tokens();
573
0
    addNull();
574
0
}
575
576
void
577
QPDFParser::addInt(int count)
578
7.78k
{
579
7.78k
    auto obj = QPDFObject::create<QPDF_Integer>(int_buffer[count % 2]);
580
7.78k
    obj->setDescription(context, description, last_offset_buffer[count % 2]);
581
7.78k
    add(std::move(obj));
582
7.78k
}
583
584
template <typename T, typename... Args>
585
void
586
QPDFParser::addScalar(Args&&... args)
587
0
{
588
0
    auto limit = Limits::parser_max_container_size(bad_count || sanity_checks);
589
0
    if (frame->olist.size() >= limit || frame->dict.size() >= limit) {
590
        // Stop adding scalars. We are going to abort when the close token or a bad token is
591
        // encountered.
592
0
        max_bad_count = 1;
593
0
        check_too_many_bad_tokens(); // always throws Error()
594
0
    }
595
0
    auto obj = QPDFObject::create<T>(std::forward<Args>(args)...);
596
0
    obj->setDescription(context, description, input.getLastOffset());
597
0
    add(std::move(obj));
598
0
}
Unexecuted instantiation: void QPDFParser::addScalar<QPDF_Bool, bool>(bool&&)
Unexecuted instantiation: void QPDFParser::addScalar<QPDF_Integer, long long>(long long&&)
Unexecuted instantiation: void QPDFParser::addScalar<QPDF_Real, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: void QPDFParser::addScalar<QPDF_Name, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: void QPDFParser::addScalar<QPDF_Operator, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: void QPDFParser::addScalar<QPDF_String, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: void QPDFParser::addScalar<QPDF_String, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
599
600
template <typename T, typename... Args>
601
QPDFObjectHandle
602
QPDFParser::withDescription(Args&&... args)
603
10.2k
{
604
10.2k
    auto obj = QPDFObject::create<T>(std::forward<Args>(args)...);
605
10.2k
    obj->setDescription(context, description, start);
606
10.2k
    return {obj};
607
10.2k
}
Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_Bool, bool>(bool&&)
Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_Integer, long long>(long long&&)
Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_Real, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
QPDFObjectHandle QPDFParser::withDescription<QPDF_Name, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
603
10.2k
{
604
10.2k
    auto obj = QPDFObject::create<T>(std::forward<Args>(args)...);
605
10.2k
    obj->setDescription(context, description, start);
606
10.2k
    return {obj};
607
10.2k
}
Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_Operator, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_String, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: QPDFObjectHandle QPDFParser::withDescription<QPDF_String, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)
608
609
void
610
QPDFParser::setDescription(ObjectPtr& obj, qpdf_offset_t parsed_offset)
611
7.78k
{
612
7.78k
    if (obj) {
613
7.78k
        obj->setDescription(context, description, parsed_offset);
614
7.78k
    }
615
7.78k
}
616
617
void
618
QPDFParser::fixMissingKeys()
619
0
{
620
0
    std::set<std::string> names;
621
0
    for (auto& obj: frame->olist) {
622
0
        if (obj.raw_type_code() == ::ot_name) {
623
0
            names.insert(obj.obj_sp()->getStringValue());
624
0
        }
625
0
    }
626
0
    int next_fake_key = 1;
627
0
    for (auto const& item: frame->olist) {
628
0
        while (true) {
629
0
            const std::string key = "/QPDFFake" + std::to_string(next_fake_key++);
630
0
            const bool found_fake = !frame->dict.contains(key) && !names.contains(key);
631
0
            QTC::TC("qpdf", "QPDFParser found fake", (found_fake ? 0 : 1));
632
0
            if (found_fake) {
633
0
                warn(
634
0
                    frame->offset,
635
0
                    "expected dictionary key but found non-name object; inserting key " + key);
636
0
                frame->dict[key] = item;
637
0
                break;
638
0
            }
639
0
        }
640
0
    }
641
0
}
642
643
void
644
QPDFParser::check_too_many_bad_tokens()
645
0
{
646
0
    auto limit = Limits::parser_max_container_size(bad_count || sanity_checks);
647
0
    if (frame->olist.size() >= limit || frame->dict.size() >= limit) {
648
0
        if (bad_count) {
649
0
            limits_error(
650
0
                "parser-max-container-size-damaged",
651
0
                "encountered errors while parsing an array or dictionary with more than " +
652
0
                    std::to_string(limit) + " elements; giving up on reading object");
653
0
        }
654
0
        limits_error(
655
0
            "parser-max-container-size",
656
0
            "encountered an array or dictionary with more than " + std::to_string(limit) +
657
0
                " elements during xref recovery; giving up on reading object");
658
0
    }
659
0
    if (max_bad_count && --max_bad_count == 0) {
660
0
        limits_error(
661
0
            "parser-max-errors", "too many errors during parsing; treating object as null");
662
0
    }
663
0
    if (good_count > 4) {
664
0
        good_count = 0;
665
0
        bad_count = 1;
666
0
        return;
667
0
    }
668
0
    if (++bad_count > 5 ||
669
0
        (frame->state != st_array && std::cmp_less(max_bad_count, frame->olist.size()))) {
670
        // Give up after 5 errors in close proximity or if the number of missing dictionary keys
671
        // exceeds the remaining number of allowable total errors.
672
0
        warn("too many errors; giving up on reading object");
673
0
        throw Error();
674
0
    }
675
0
    good_count = 0;
676
0
}
677
678
void
679
QPDFParser::limits_error(std::string const& limit, std::string const& msg)
680
0
{
681
0
    Limits::error();
682
0
    warn("limits error("s + limit + "): " + msg);
683
0
    throw Error();
684
0
}
685
686
void
687
QPDFParser::warn(QPDFExc const& e) const
688
321
{
689
    // If parsing on behalf of a QPDF object and want to give a warning, we can warn through the
690
    // object. If parsing for some other reason, such as an explicit creation of an object from a
691
    // string, then just throw the exception.
692
321
    if (context) {
693
0
        context->warn(e);
694
321
    } else {
695
321
        throw e;
696
321
    }
697
321
}
698
699
void
700
QPDFParser::warnDuplicateKey()
701
0
{
702
0
    warn(
703
0
        frame->offset,
704
0
        "dictionary has duplicated key " + frame->key + "; last occurrence overrides earlier ones");
705
0
}
706
707
void
708
QPDFParser::warn(qpdf_offset_t offset, std::string const& msg) const
709
321
{
710
321
    if (stream_id) {
711
0
        std::string descr = "object "s + std::to_string(obj_id) + " 0";
712
0
        std::string name = context->getFilename() + " object stream " + std::to_string(stream_id);
713
0
        warn(QPDFExc(qpdf_e_damaged_pdf, name, descr, offset, msg));
714
321
    } else {
715
321
        warn(QPDFExc(qpdf_e_damaged_pdf, input.getName(), object_description, offset, msg));
716
321
    }
717
321
}
718
719
void
720
QPDFParser::warn(std::string const& msg) const
721
321
{
722
321
    warn(input.getLastOffset(), msg);
723
321
}