2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-09-19 00:29:07 +00:00

Remove non-dictionary objects from pages tree

This commit is contained in:
m-holger 2024-07-16 14:35:32 +01:00
parent 01353e114f
commit 7f2d76b78d
2 changed files with 12 additions and 2 deletions

View File

@ -1515,6 +1515,7 @@ class QPDF
std::set<QPDFObjGen> resolving;
QPDFObjectHandle trailer;
std::vector<QPDFObjectHandle> all_pages;
bool invalid_page_found{false};
std::map<QPDFObjGen, int> pageobj_to_pages_pos;
bool pushed_inherited_attributes_to_pages{false};
bool ever_pushed_inherited_attributes_to_pages{false};

View File

@ -40,7 +40,7 @@ std::vector<QPDFObjectHandle> const&
QPDF::getAllPages()
{
// Note that pushInheritedAttributesToPage may also be used to initialize m->all_pages.
if (m->all_pages.empty()) {
if (m->all_pages.empty() && !m->invalid_page_found) {
m->ever_called_get_all_pages = true;
QPDFObjGen::set visited;
QPDFObjGen::set seen;
@ -70,6 +70,10 @@ QPDF::getAllPages()
// Ensure we actually found a /Pages object.
getAllPagesInternal(pages, visited, seen, false);
}
if (m->invalid_page_found) {
flattenPagesTree();
m->invalid_page_found = false;
}
}
return m->all_pages;
}
@ -100,6 +104,7 @@ QPDF::getAllPagesInternal(
auto kid = kids.getArrayItem(i);
if (!kid.isDictionary()) {
kid.warnIfPossible("Pages tree includes non-dictionary object; ignoring");
m->invalid_page_found = true;
continue;
}
if (kid.hasKey("/Kids")) {
@ -181,7 +186,11 @@ QPDF::flattenPagesTree()
pages.replaceKey("/Kids", QPDFObjectHandle::newArray(m->all_pages));
// /Count has not changed
if (pages.getKey("/Count").getUIntValue() != len) {
throw std::runtime_error("/Count is wrong after flattening pages tree");
if (m->invalid_page_found && pages.getKey("/Count").getUIntValue() > len) {
pages.replaceKey("/Count", QPDFObjectHandle::newInteger(toI(len)));
} else {
throw std::runtime_error("/Count is wrong after flattening pages tree");
}
}
}