From 08ba21cf4935fec39d3454714c03d36ff6a6b836 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sun, 24 Apr 2022 10:08:32 -0400 Subject: Fix some bugs around null values in dictionaries Make it so that a key with a null value is always treated as not being present. This was inconsistent before. --- libqpdf/QPDF_Dictionary.cc | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'libqpdf/QPDF_Dictionary.cc') diff --git a/libqpdf/QPDF_Dictionary.cc b/libqpdf/QPDF_Dictionary.cc index 0423f41f..c3343b21 100644 --- a/libqpdf/QPDF_Dictionary.cc +++ b/libqpdf/QPDF_Dictionary.cc @@ -24,12 +24,11 @@ std::string QPDF_Dictionary::unparse() { std::string result = "<< "; - for (std::map::iterator iter = - this->items.begin(); - iter != this->items.end(); - ++iter) { - result += QPDF_Name::normalizeName((*iter).first) + " " + - (*iter).second.unparse() + " "; + for (auto& iter : this->items) { + if (!iter.second.isNull()) { + result += QPDF_Name::normalizeName(iter.first) + " " + + iter.second.unparse() + " "; + } } result += ">>"; return result; @@ -39,12 +38,11 @@ JSON QPDF_Dictionary::getJSON() { JSON j = JSON::makeDictionary(); - for (std::map::iterator iter = - this->items.begin(); - iter != this->items.end(); - ++iter) { - j.addDictionaryMember( - QPDF_Name::normalizeName((*iter).first), (*iter).second.getJSON()); + for (auto& iter : this->items) { + if (!iter.second.isNull()) { + j.addDictionaryMember( + QPDF_Name::normalizeName(iter.first), iter.second.getJSON()); + } } return j; } @@ -78,9 +76,10 @@ QPDF_Dictionary::getKey(std::string const& key) { // PDF spec says fetching a non-existent key from a dictionary // returns the null object. - if (this->items.count(key)) { + auto item = this->items.find(key); + if (item != this->items.end()) { // May be a null object - return (*(this->items.find(key))).second; + return item->second; } else { QPDFObjectHandle null = QPDFObjectHandle::newNull(); QPDF* qpdf = 0; @@ -97,12 +96,9 @@ std::set QPDF_Dictionary::getKeys() { std::set result; - for (std::map::const_iterator iter = - this->items.begin(); - iter != this->items.end(); - ++iter) { - if (hasKey((*iter).first)) { - result.insert((*iter).first); + for (auto& iter : this->items) { + if (!iter.second.isNull()) { + result.insert(iter.first); } } return result; -- cgit v1.2.3-54-g00ecf