From 336d7833250b9d8d8a28a6f232f659b765925593 Mon Sep 17 00:00:00 2001 From: m-holger Date: Mon, 7 Oct 2024 17:57:06 +0100 Subject: [PATCH] Move calculations from QPDF::getObjectCount to Objects::next_id --- include/qpdf/QPDF.hh | 2 +- libqpdf/QPDF.cc | 7 +------ libqpdf/QPDF_objects.cc | 7 ++++++- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh index 9e638fa8..756b22d7 100644 --- a/include/qpdf/QPDF.hh +++ b/include/qpdf/QPDF.hh @@ -645,7 +645,7 @@ class QPDF QPDF_DLL void fixDanglingReferences(bool force = false); - // Return the approximate number of indirect objects. It is/ approximate because not all objects + // Return the approximate number of indirect objects. It is approximate because not all objects // in the file are preserved in all cases, and gaps in object numbering are not preserved. QPDF_DLL size_t getObjectCount(); diff --git a/libqpdf/QPDF.cc b/libqpdf/QPDF.cc index e5bde7ea..0939c615 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -492,12 +492,7 @@ QPDF::getObjectCount() // This method returns the next available indirect object number. makeIndirectObject uses it for // this purpose. After fixDanglingReferences is called, all objects in the xref table will also // be in obj_cache. - fixDanglingReferences(); - QPDFObjGen og; - if (!m->objects.obj_cache.empty()) { - og = (*(m->objects.obj_cache.rbegin())).first; - } - return toS(og.getObj()); + return toS(m->objects.next_id().getObj() - 1); } std::vector diff --git a/libqpdf/QPDF_objects.cc b/libqpdf/QPDF_objects.cc index 69c444b0..49b2faa2 100644 --- a/libqpdf/QPDF_objects.cc +++ b/libqpdf/QPDF_objects.cc @@ -1712,7 +1712,12 @@ Objects::unresolved(QPDFObjGen og) QPDFObjGen Objects::next_id() { - int max_objid = toI(qpdf.getObjectCount()); + qpdf.fixDanglingReferences(); + QPDFObjGen og; + if (!obj_cache.empty()) { + og = (*(m->objects.obj_cache.rbegin())).first; + } + int max_objid = og.getObj(); if (max_objid == std::numeric_limits::max()) { throw std::range_error("max object id is too high to create new objects"); }