diff --git a/libqpdf/NNTree.cc b/libqpdf/NNTree.cc index 2ef1d8e..c6dbfec 100644 --- a/libqpdf/NNTree.cc +++ b/libqpdf/NNTree.cc @@ -280,7 +280,7 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list::iterato auto first_node = impl.qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary()); first_node.replaceKey(key, first_half); - Array new_kids; + auto new_kids = Array::empty(); new_kids.push_back(first_node); to_split.removeKey("/Limits"); // already shouldn't be there for root to_split.removeKey(impl.itemsKey()); @@ -301,7 +301,7 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list::iterato // Create a second half array, and transfer the second half of the items into the second half // array. - Array second_half; + auto second_half = Array::empty(); auto start_idx = static_cast((n / 2) & ~1u); while (std::cmp_greater(first_half.size(), start_idx)) { second_half.push_back(first_half[start_idx]); @@ -323,6 +323,9 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list::iterato // this is a leaf, so that the kid/item number points to the right place. Array parent_kids = parent->node.getKey("/Kids"); + if (!parent_kids) { + impl.error(parent->node, "parent node has no /Kids array"); + } parent_kids.insert(parent->kid_number + 1, second_node); auto cur_elem = parent; ++cur_elem; // points to end() for leaf nodes @@ -466,7 +469,7 @@ NNTreeIterator::remove() if (parent == path.end()) { // We erased the very last item. Convert the root to an empty items array. element->node.removeKey("/Kids"); - element->node.replaceKey(impl.itemsKey(), Array()); + element->node.replaceKey(impl.itemsKey(), Array::empty()); path.clear(); setItemNumber(impl.oh, -1); return; @@ -676,7 +679,7 @@ void NNTreeImpl::repair() { auto new_node = QPDFObjectHandle::newDictionary(); - new_node.replaceKey(itemsKey(), Array()); + new_node.replaceKey(itemsKey(), Array::empty()); NNTreeImpl repl(qpdf, new_node, key_type, value_valid, false); for (auto const& [key, value]: *this) { if (key && value) { @@ -770,7 +773,7 @@ NNTreeImpl::iterator NNTreeImpl::insertFirst(QPDFObjectHandle const& key, QPDFObjectHandle const& value) { auto iter = begin(); - Array items(nullptr); + Array items; if (iter.node.isDictionary()) { items = iter.node.getKey(itemsKey()); } diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index 2e9ba73..37b2c60 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -61,11 +61,6 @@ Array::array() const return nullptr; // unreachable } -Array::Array(bool empty) : - BaseHandle(empty ? QPDFObject::create() : nullptr) -{ -} - Array::Array(std::vector const& items) : BaseHandle(QPDFObject::create(items)) { @@ -76,6 +71,12 @@ Array::Array(std::vector&& items) : { } +Array +Array::empty() +{ + return Array(std::vector()); +} + Array::iterator Array::begin() { diff --git a/libqpdf/qpdf/QPDFObjectHandle_private.hh b/libqpdf/qpdf/QPDFObjectHandle_private.hh index 183b792..b0d5a04 100644 --- a/libqpdf/qpdf/QPDFObjectHandle_private.hh +++ b/libqpdf/qpdf/QPDFObjectHandle_private.hh @@ -12,18 +12,19 @@ namespace qpdf class Array final: public BaseHandle { public: - // Create an empty Array. If 'empty' is false, create a null Array. - Array(bool empty = true); + Array() = default; - Array(std::vector const& items); + explicit Array(std::vector const& items); - Array(std::vector&& items); + explicit Array(std::vector&& items); Array(Array const& other) : BaseHandle(other.obj) { } + static Array empty(); + Array& operator=(Array const& other) {