Commit 4547613e568d2fe9355baf7b004d30352a0fd884
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.
Showing
4 changed files
with
56 additions
and
25 deletions
libqpdf/Pipeline.cc
| 1 | #include <qpdf/Pipeline.hh> | 1 | #include <qpdf/Pipeline.hh> |
| 2 | 2 | ||
| 3 | +#include <qpdf/Util.hh> | ||
| 4 | + | ||
| 3 | #include <cstring> | 5 | #include <cstring> |
| 4 | #include <stdexcept> | 6 | #include <stdexcept> |
| 5 | 7 | ||
| 8 | +using namespace qpdf; | ||
| 9 | + | ||
| 6 | Pipeline::Pipeline(char const* identifier, Pipeline* next) : | 10 | Pipeline::Pipeline(char const* identifier, Pipeline* next) : |
| 7 | identifier(identifier), | 11 | identifier(identifier), |
| 8 | next_(next) | 12 | next_(next) |
| @@ -12,10 +16,8 @@ Pipeline::Pipeline(char const* identifier, Pipeline* next) : | @@ -12,10 +16,8 @@ Pipeline::Pipeline(char const* identifier, Pipeline* next) : | ||
| 12 | Pipeline* | 16 | Pipeline* |
| 13 | Pipeline::getNext(bool allow_null) | 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 | return next_; | 21 | return next_; |
| 20 | } | 22 | } |
| 21 | 23 |
libqpdf/QPDFXRefEntry.cc
| @@ -2,50 +2,43 @@ | @@ -2,50 +2,43 @@ | ||
| 2 | 2 | ||
| 3 | #include <qpdf/QIntC.hh> | 3 | #include <qpdf/QIntC.hh> |
| 4 | #include <qpdf/QPDFExc.hh> | 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 | QPDFXRefEntry::QPDFXRefEntry(int type, qpdf_offset_t field1, int field2) : | 11 | QPDFXRefEntry::QPDFXRefEntry(int type, qpdf_offset_t field1, int field2) : |
| 11 | type(type), | 12 | type(type), |
| 12 | field1(field1), | 13 | field1(field1), |
| 13 | field2(field2) | 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 | int | 19 | int |
| 21 | QPDFXRefEntry::getType() const | 20 | QPDFXRefEntry::getType() const |
| 22 | { | 21 | { |
| 23 | - return this->type; | 22 | + return type; |
| 24 | } | 23 | } |
| 25 | 24 | ||
| 26 | qpdf_offset_t | 25 | qpdf_offset_t |
| 27 | QPDFXRefEntry::getOffset() const | 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 | return this->field1; | 29 | return this->field1; |
| 33 | } | 30 | } |
| 34 | 31 | ||
| 35 | int | 32 | int |
| 36 | QPDFXRefEntry::getObjStreamNumber() const | 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 | int | 39 | int |
| 45 | QPDFXRefEntry::getObjStreamIndex() const | 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& pdf, char const* arg2) | @@ -2279,8 +2279,17 @@ test_61(QPDF& pdf, char const* arg2) | ||
| 2279 | static void | 2279 | static void |
| 2280 | test_62(QPDF& pdf, char const* arg2) | 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 | static void | 2295 | static void |
| @@ -3548,7 +3557,7 @@ runtest(int n, char const* filename1, char const* arg2) | @@ -3548,7 +3557,7 @@ runtest(int n, char const* filename1, char const* arg2) | ||
| 3548 | // the test suite to see how the test is invoked to find the file | 3557 | // the test suite to see how the test is invoked to find the file |
| 3549 | // that the test is supposed to operate on. | 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 | if (n == 0) { | 3562 | if (n == 0) { |
| 3554 | // Throw in some random test cases that don't fit anywhere | 3563 | // Throw in some random test cases that don't fit anywhere |