2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-10 23:20:58 +00:00

Avoid inserting direct null objects into olist

This commit is contained in:
m-holger 2022-12-19 14:52:32 +00:00 committed by Jay Berkenbilt
parent 9da50ca360
commit 8391022416
3 changed files with 19 additions and 9 deletions

View File

@ -308,7 +308,7 @@ QPDFParser::parse(bool& empty, bool content_stream)
setDescription(object, input->getLastOffset()); setDescription(object, input->getLastOffset());
} }
set_offset = true; set_offset = true;
olist.push_back(is_null ? null_oh : object); olist.push_back(object);
break; break;
case st_top: case st_top:
@ -339,16 +339,19 @@ QPDFParser::parse(bool& empty, bool content_stream)
// Convert list to map. Alternating elements are keys. Attempt // Convert list to map. Alternating elements are keys. Attempt
// to recover more or less gracefully from invalid dictionaries. // to recover more or less gracefully from invalid dictionaries.
std::set<std::string> names; std::set<std::string> names;
size_t n_elements = olist.size(); for (auto& obj: olist) {
for (size_t i = 0; i < n_elements; ++i) { if (obj) {
QPDFObjectHandle oh = olist.at(i); if (obj->getTypeCode() == ::ot_name) {
if ((!oh.isIndirect()) && oh.isName()) { names.insert(obj->getStringValue());
names.insert(oh.getName()); }
} else {
obj = null_oh;
} }
} }
std::map<std::string, QPDFObjectHandle> dict; std::map<std::string, QPDFObjectHandle> dict;
int next_fake_key = 1; int next_fake_key = 1;
size_t n_elements = olist.size();
for (unsigned int i = 0; i < n_elements; ++i) { for (unsigned int i = 0; i < n_elements; ++i) {
QPDFObjectHandle key_obj = olist.at(i); QPDFObjectHandle key_obj = olist.at(i);
QPDFObjectHandle val; QPDFObjectHandle val;
@ -414,7 +417,7 @@ QPDFParser::parse(bool& empty, bool content_stream)
if (state_stack.back() == st_top) { if (state_stack.back() == st_top) {
done = true; done = true;
} else { } else {
stack.back().olist.push_back(is_null ? null_oh : object); stack.back().olist.push_back(object);
} }
} }
} }

View File

@ -123,8 +123,12 @@ void
QPDF_Array::setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& v) QPDF_Array::setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& v)
{ {
this->elements = SparseOHArray(); this->elements = SparseOHArray();
for (auto&& iter: v) { for (auto&& item: v) {
this->elements.append(iter); if (item) {
this->elements.append(item);
} else {
++this->elements.n_elements;
}
} }
} }

View File

@ -4,6 +4,8 @@
#include <qpdf/QPDFObjectHandle.hh> #include <qpdf/QPDFObjectHandle.hh>
#include <unordered_map> #include <unordered_map>
class QPDF_Array;
class SparseOHArray class SparseOHArray
{ {
public: public:
@ -25,6 +27,7 @@ class SparseOHArray
const_iterator end() const; const_iterator end() const;
private: private:
friend class QPDF_Array;
std::unordered_map<size_t, QPDFObjectHandle> elements; std::unordered_map<size_t, QPDFObjectHandle> elements;
size_t n_elements; size_t n_elements;
}; };