Commit 1674b67cd117d740976b02dbf37b2d36cb653325
1 parent
13d55d88
Refactor `NNTreeIterator::updateIValue`: replace `getArrayNItems` and `getArrayI…
…tem` with `size` and subscript operators, simplify logic, and improve error handling.
Showing
1 changed file
with
8 additions
and
12 deletions
libqpdf/NNTree.cc
| ... | ... | @@ -57,25 +57,21 @@ NNTreeIterator::updateIValue(bool allow_invalid) |
| 57 | 57 | // we must call updateIValue as well. These cases are handled, and for good measure, we also |
| 58 | 58 | // call updateIValue in operator* and operator->. |
| 59 | 59 | |
| 60 | - bool okay = false; | |
| 61 | - if (item_number >= 0 && node.isDictionary()) { | |
| 62 | - auto items = node.getKey(impl.details.itemsKey()); | |
| 63 | - if (item_number + 1 < items.getArrayNItems()) { | |
| 64 | - okay = true; | |
| 65 | - ivalue.first = items.getArrayItem(item_number); | |
| 66 | - ivalue.second = items.getArrayItem(1 + item_number); | |
| 67 | - } else { | |
| 68 | - impl.error(node, "update ivalue: items array is too short"); | |
| 69 | - } | |
| 70 | - } | |
| 71 | - if (!okay) { | |
| 60 | + if (item_number < 0 || !node.isDictionary()) { | |
| 72 | 61 | if (!allow_invalid) { |
| 73 | 62 | throw std::logic_error( |
| 74 | 63 | "attempt made to dereference an invalid name/number tree iterator"); |
| 75 | 64 | } |
| 76 | 65 | ivalue.first = QPDFObjectHandle(); |
| 77 | 66 | ivalue.second = QPDFObjectHandle(); |
| 67 | + return; | |
| 68 | + } | |
| 69 | + auto items = node.getKey(impl.details.itemsKey()); | |
| 70 | + if (!std::cmp_less(item_number + 1, items.size())) { | |
| 71 | + impl.error(node, "update ivalue: items array is too short"); | |
| 78 | 72 | } |
| 73 | + ivalue.first = items[item_number]; | |
| 74 | + ivalue.second = items[1 + item_number]; | |
| 79 | 75 | } |
| 80 | 76 | |
| 81 | 77 | NNTreeIterator::PathElement::PathElement(QPDFObjectHandle const& node, int kid_number) : | ... | ... |