Commit 4547613e568d2fe9355baf7b004d30352a0fd884

Authored by m-holger
1 parent f3063577

Refactor error handling in `Pipeline` and `QPDFXRefEntry` to use `util::assertio…

…n` and simplify logic

- Replace `std::logic_error` with `util::assertion` for consistency.
- Use `using namespace qpdf` for cleaner code.
- Update `test_62` to cover `Pipeline` output method.
libqpdf/Pipeline.cc
1 1 #include <qpdf/Pipeline.hh>
2 2  
  3 +#include <qpdf/Util.hh>
  4 +
3 5 #include <cstring>
4 6 #include <stdexcept>
5 7  
  8 +using namespace qpdf;
  9 +
6 10 Pipeline::Pipeline(char const* identifier, Pipeline* next) :
7 11 identifier(identifier),
8 12 next_(next)
... ... @@ -12,10 +16,8 @@ Pipeline::Pipeline(char const* identifier, Pipeline* next) :
12 16 Pipeline*
13 17 Pipeline::getNext(bool allow_null)
14 18 {
15   - if (!next_ && !allow_null) {
16   - throw std::logic_error(
17   - identifier + ": Pipeline::getNext() called on pipeline with no next");
18   - }
  19 + util::assertion(
  20 + next_ || allow_null, identifier + ": Pipeline::getNext() called on pipeline with no next");
19 21 return next_;
20 22 }
21 23  
... ...
libqpdf/QPDFXRefEntry.cc
... ... @@ -2,50 +2,43 @@
2 2  
3 3 #include <qpdf/QIntC.hh>
4 4 #include <qpdf/QPDFExc.hh>
  5 +#include <qpdf/Util.hh>
5 6  
6   -QPDFXRefEntry::QPDFXRefEntry() // NOLINT (modernize-use-equals-default)
7   -{
8   -}
  7 +using namespace qpdf;
  8 +
  9 +QPDFXRefEntry::QPDFXRefEntry() = default;
9 10  
10 11 QPDFXRefEntry::QPDFXRefEntry(int type, qpdf_offset_t field1, int field2) :
11 12 type(type),
12 13 field1(field1),
13 14 field2(field2)
14 15 {
15   - if ((type < 1) || (type > 2)) {
16   - throw std::logic_error("invalid xref type " + std::to_string(type));
17   - }
  16 + util::assertion(type == 1 || type == 2, "invalid xref type " + std::to_string(type));
18 17 }
19 18  
20 19 int
21 20 QPDFXRefEntry::getType() const
22 21 {
23   - return this->type;
  22 + return type;
24 23 }
25 24  
26 25 qpdf_offset_t
27 26 QPDFXRefEntry::getOffset() const
28 27 {
29   - if (this->type != 1) {
30   - throw std::logic_error("getOffset called for xref entry of type != 1");
31   - }
  28 + util::assertion(type == 1, "getOffset called for xref entry of type != 1");
32 29 return this->field1;
33 30 }
34 31  
35 32 int
36 33 QPDFXRefEntry::getObjStreamNumber() const
37 34 {
38   - if (this->type != 2) {
39   - throw std::logic_error("getObjStreamNumber called for xref entry of type != 2");
40   - }
41   - return QIntC::to_int(this->field1);
  35 + util::assertion(type == 2, "getObjStreamNumber called for xref entry of type != 2");
  36 + return QIntC::to_int(field1);
42 37 }
43 38  
44 39 int
45 40 QPDFXRefEntry::getObjStreamIndex() const
46 41 {
47   - if (this->type != 2) {
48   - throw std::logic_error("getObjStreamIndex called for xref entry of type != 2");
49   - }
50   - return this->field2;
  42 + util::assertion(type == 2, "getObjStreamIndex called for xref entry of type != 2");
  43 + return field2;
51 44 }
... ...
qpdf/qtest/misc.test 0 → 100644
  1 +#!/usr/bin/env perl
  2 +require 5.008;
  3 +use warnings;
  4 +use strict;
  5 +
  6 +unshift(@INC, '.');
  7 +require qpdf_test_helpers;
  8 +
  9 +chdir("qpdf") or die "chdir testdir failed: $!\n";
  10 +
  11 +require TestDriver;
  12 +
  13 +my $dev_null = File::Spec->devnull();
  14 +cleanup();
  15 +
  16 +my $td = new TestDriver('misc');
  17 +
  18 +my $n_tests = 1;
  19 +
  20 +$td->runtest("pipeline",
  21 + {$td->COMMAND => "test_driver 62 -"},
  22 + {$td->STRING => "test 62 done\n",
  23 + $td->EXIT_STATUS => 0},
  24 + $td->NORMALIZE_NEWLINES);
  25 +
  26 +cleanup();
  27 +$td->report($n_tests);
... ...
qpdf/test_driver.cc
... ... @@ -2279,8 +2279,17 @@ test_61(QPDF&amp; pdf, char const* arg2)
2279 2279 static void
2280 2280 test_62(QPDF& pdf, char const* arg2)
2281 2281 {
2282   - // Test int size checks. This test will fail if int and long long are the same size.
2283   - // Moved to libtests/objects
  2282 + // Was test int size checks - Moved to libtests/objects.
  2283 + // Test Pipeline methods
  2284 + std::string out;
  2285 + Pl_String pl("", nullptr, out);
  2286 + unsigned short us = 1;
  2287 + unsigned int ui = 2;
  2288 + unsigned long long ull = 3;
  2289 + long l = 4;
  2290 + short s = 5;
  2291 + pl << us << ui << ull << l << s;
  2292 + assert(out == "12345");
2284 2293 }
2285 2294  
2286 2295 static void
... ... @@ -3548,7 +3557,7 @@ runtest(int n, char const* filename1, char const* arg2)
3548 3557 // the test suite to see how the test is invoked to find the file
3549 3558 // that the test is supposed to operate on.
3550 3559  
3551   - std::set<int> ignore_filename = {61, 81, 83, 84, 85, 86, 87, 92, 95, 96};
  3560 + std::set<int> ignore_filename = {61, 62, 81, 83, 84, 85, 86, 87, 92, 95, 96};
3552 3561  
3553 3562 if (n == 0) {
3554 3563 // Throw in some random test cases that don't fit anywhere
... ...