Commit baa77f54f1870f346b4299edcc91b7446091158c
1 parent
26de2d5c
Refactor `NNTreeImpl::withinLimits`: use `const` references for parameters, simp…
…lify `withinLimits` logic, and improve code readability with subscript operators.
Showing
3 changed files
with
16 additions
and
20 deletions
libqpdf/NNTree.cc
| @@ -12,17 +12,17 @@ | @@ -12,17 +12,17 @@ | ||
| 12 | #include <utility> | 12 | #include <utility> |
| 13 | 13 | ||
| 14 | static std::string | 14 | static std::string |
| 15 | -get_description(QPDFObjectHandle& node) | 15 | +get_description(QPDFObjectHandle const& node) |
| 16 | { | 16 | { |
| 17 | std::string result("Name/Number tree node"); | 17 | std::string result("Name/Number tree node"); |
| 18 | - if (node.isIndirect()) { | 18 | + if (node.indirect()) { |
| 19 | result += " (object " + std::to_string(node.getObjectID()) + ")"; | 19 | result += " (object " + std::to_string(node.getObjectID()) + ")"; |
| 20 | } | 20 | } |
| 21 | return result; | 21 | return result; |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | void | 24 | void |
| 25 | -NNTreeImpl::warn(QPDFObjectHandle& node, std::string const& msg) | 25 | +NNTreeImpl::warn(QPDFObjectHandle const& node, std::string const& msg) |
| 26 | { | 26 | { |
| 27 | qpdf.warn(qpdf_e_damaged_pdf, get_description(node), 0, msg); | 27 | qpdf.warn(qpdf_e_damaged_pdf, get_description(node), 0, msg); |
| 28 | if (++error_count > 5 && qpdf.reconstructed_xref()) { | 28 | if (++error_count > 5 && qpdf.reconstructed_xref()) { |
| @@ -31,7 +31,7 @@ NNTreeImpl::warn(QPDFObjectHandle& node, std::string const& msg) | @@ -31,7 +31,7 @@ NNTreeImpl::warn(QPDFObjectHandle& node, std::string const& msg) | ||
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | void | 33 | void |
| 34 | -NNTreeImpl::error(QPDFObjectHandle& node, std::string const& msg) | 34 | +NNTreeImpl::error(QPDFObjectHandle const& node, std::string const& msg) |
| 35 | { | 35 | { |
| 36 | throw QPDFExc(qpdf_e_damaged_pdf, qpdf.getFilename(), get_description(node), 0, msg); | 36 | throw QPDFExc(qpdf_e_damaged_pdf, qpdf.getFilename(), get_description(node), 0, msg); |
| 37 | } | 37 | } |
| @@ -701,22 +701,19 @@ NNTreeImpl::last() | @@ -701,22 +701,19 @@ NNTreeImpl::last() | ||
| 701 | } | 701 | } |
| 702 | 702 | ||
| 703 | int | 703 | int |
| 704 | -NNTreeImpl::withinLimits(QPDFObjectHandle key, QPDFObjectHandle node) | 704 | +NNTreeImpl::withinLimits(QPDFObjectHandle const& key, QPDFObjectHandle const& node) |
| 705 | { | 705 | { |
| 706 | - int result = 0; | ||
| 707 | auto limits = node.getKey("/Limits"); | 706 | auto limits = node.getKey("/Limits"); |
| 708 | - if (limits.isArray() && (limits.getArrayNItems() >= 2) && | ||
| 709 | - details.keyValid(limits.getArrayItem(0)) && details.keyValid(limits.getArrayItem(1))) { | ||
| 710 | - if (details.compareKeys(key, limits.getArrayItem(0)) < 0) { | ||
| 711 | - result = -1; | ||
| 712 | - } else if (details.compareKeys(key, limits.getArrayItem(1)) > 0) { | ||
| 713 | - result = 1; | ||
| 714 | - } | ||
| 715 | - } else { | ||
| 716 | - QTC::TC("qpdf", "NNTree missing limits"); | 707 | + if (!(limits.size() >= 2 && details.keyValid(limits[0]) && details.keyValid(limits[1]))) { |
| 717 | error(node, "node is missing /Limits"); | 708 | error(node, "node is missing /Limits"); |
| 718 | } | 709 | } |
| 719 | - return result; | 710 | + if (details.compareKeys(key, limits[0]) < 0) { |
| 711 | + return -1; | ||
| 712 | + } | ||
| 713 | + if (details.compareKeys(key, limits[1]) > 0) { | ||
| 714 | + return 1; | ||
| 715 | + } | ||
| 716 | + return 0; | ||
| 720 | } | 717 | } |
| 721 | 718 | ||
| 722 | int | 719 | int |
libqpdf/qpdf/NNTree.hh
| @@ -111,7 +111,7 @@ class NNTreeImpl | @@ -111,7 +111,7 @@ class NNTreeImpl | ||
| 111 | private: | 111 | private: |
| 112 | void repair(); | 112 | void repair(); |
| 113 | iterator findInternal(QPDFObjectHandle const& key, bool return_prev_if_not_found = false); | 113 | iterator findInternal(QPDFObjectHandle const& key, bool return_prev_if_not_found = false); |
| 114 | - int withinLimits(QPDFObjectHandle key, QPDFObjectHandle node); | 114 | + int withinLimits(QPDFObjectHandle const& key, QPDFObjectHandle const& node); |
| 115 | int binarySearch( | 115 | int binarySearch( |
| 116 | QPDFObjectHandle key, | 116 | QPDFObjectHandle key, |
| 117 | QPDFObjectHandle items, | 117 | QPDFObjectHandle items, |
| @@ -120,8 +120,8 @@ class NNTreeImpl | @@ -120,8 +120,8 @@ class NNTreeImpl | ||
| 120 | int (NNTreeImpl::*compare)(QPDFObjectHandle& key, QPDFObjectHandle& arr, int item)); | 120 | int (NNTreeImpl::*compare)(QPDFObjectHandle& key, QPDFObjectHandle& arr, int item)); |
| 121 | int compareKeyItem(QPDFObjectHandle& key, QPDFObjectHandle& items, int idx); | 121 | int compareKeyItem(QPDFObjectHandle& key, QPDFObjectHandle& items, int idx); |
| 122 | int compareKeyKid(QPDFObjectHandle& key, QPDFObjectHandle& items, int idx); | 122 | int compareKeyKid(QPDFObjectHandle& key, QPDFObjectHandle& items, int idx); |
| 123 | - void warn(QPDFObjectHandle& node, std::string const& msg); | ||
| 124 | - void error(QPDFObjectHandle& node, std::string const& msg); | 123 | + void warn(QPDFObjectHandle const& node, std::string const& msg); |
| 124 | + void error(QPDFObjectHandle const& node, std::string const& msg); | ||
| 125 | 125 | ||
| 126 | NNTreeDetails const& details; | 126 | NNTreeDetails const& details; |
| 127 | QPDF& qpdf; | 127 | QPDF& qpdf; |
qpdf/qpdf.testcov
| @@ -536,7 +536,6 @@ NNTree split items 0 | @@ -536,7 +536,6 @@ NNTree split items 0 | ||
| 536 | NNTree split second half item 0 | 536 | NNTree split second half item 0 |
| 537 | NNTree split parent 0 | 537 | NNTree split parent 0 |
| 538 | NNTree split second half kid 0 | 538 | NNTree split second half kid 0 |
| 539 | -NNTree missing limits 0 | ||
| 540 | NNTree node is not a dictionary 0 | 539 | NNTree node is not a dictionary 0 |
| 541 | NNTree limits didn't change 0 | 540 | NNTree limits didn't change 0 |
| 542 | NNTree increment end() 0 | 541 | NNTree increment end() 0 |