From e3cc171d0210cc3230754d50285233ccb8759081 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 2 Dec 2021 08:04:59 -0500 Subject: [PATCH] C API: qpdf_oh_is_initialized --- ChangeLog | 2 ++ include/qpdf/qpdf-c.h | 2 ++ libqpdf/qpdf-c.cc | 12 +++++++++--- manual/qpdf-manual.xml | 7 +++++++ qpdf/qpdf-ctest.c | 7 +++++++ qpdf/qpdf.testcov | 1 + qpdf/qtest/qpdf/c-object-handles.out | 12 ++++++------ 7 files changed, 34 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index 201b5465..0024dbe7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 2021-12-02 Jay Berkenbilt + * C API: Add qpdf_oh_is_initialized. + * C API: Add qpdf_get_last_string_length to return the length of the last string returned. This is necessary in order to fully retrieve values of strings that may contain embedded null characters. diff --git a/include/qpdf/qpdf-c.h b/include/qpdf/qpdf-c.h index 16d1ba64..294166ac 100644 --- a/include/qpdf/qpdf-c.h +++ b/include/qpdf/qpdf-c.h @@ -581,6 +581,8 @@ extern "C" { * to. */ + QPDF_DLL + QPDF_BOOL qpdf_oh_is_initialized(qpdf_data data, qpdf_oh oh); QPDF_DLL QPDF_BOOL qpdf_oh_is_bool(qpdf_data data, qpdf_oh oh); QPDF_DLL diff --git a/libqpdf/qpdf-c.cc b/libqpdf/qpdf-c.cc index 618b926d..85dda3b9 100644 --- a/libqpdf/qpdf-c.cc +++ b/libqpdf/qpdf-c.cc @@ -881,8 +881,7 @@ qpdf_oh_valid_internal(qpdf_data qpdf, qpdf_oh oh) { auto i = qpdf->oh_cache.find(oh); bool result = ((i != qpdf->oh_cache.end()) && - (i->second).getPointer() && - (i->second)->isInitialized()); + (i->second).getPointer()); if (! result) { QTC::TC("qpdf", "qpdf-c invalid object handle"); @@ -892,11 +891,18 @@ qpdf_oh_valid_internal(qpdf_data qpdf, qpdf_oh oh) qpdf->qpdf->getFilename(), std::string("C API object handle ") + QUtil::uint_to_string(oh), - 0, "attempted access to unknown/uninitialized object handle")); + 0, "attempted access to unknown object handle")); } return result; } +QPDF_BOOL qpdf_oh_is_initialized(qpdf_data qpdf, qpdf_oh oh) +{ + QTC::TC("qpdf", "qpdf-c called qpdf_oh_is_initialized"); + return (qpdf_oh_valid_internal(qpdf, oh) && + qpdf->oh_cache[oh]->isInitialized()); +} + QPDF_BOOL qpdf_oh_is_bool(qpdf_data qpdf, qpdf_oh oh) { QTC::TC("qpdf", "qpdf-c called qpdf_oh_is_bool"); diff --git a/manual/qpdf-manual.xml b/manual/qpdf-manual.xml index 217542b8..f721d234 100644 --- a/manual/qpdf-manual.xml +++ b/manual/qpdf-manual.xml @@ -5133,6 +5133,13 @@ print "\n"; embedded null characters. + + + Add qpdf_oh_is_initialized to the + C API. While you can't directly create uninitialized objects + from the C API, you still have to be able to detect them. + + diff --git a/qpdf/qpdf-ctest.c b/qpdf/qpdf-ctest.c index a57ea44f..c5d6d199 100644 --- a/qpdf/qpdf-ctest.c +++ b/qpdf/qpdf-ctest.c @@ -512,6 +512,7 @@ static void test24(char const* infile, qpdf_oh_get_object_id(qpdf, root_from_trailer)); qpdf_oh pages = qpdf_oh_get_key(qpdf, root, "/Pages"); assert(qpdf_oh_is_dictionary(qpdf, pages)); + assert(qpdf_oh_is_initialized(qpdf, pages)); qpdf_oh kids = qpdf_oh_get_key(qpdf, pages, "/Kids"); assert(qpdf_oh_is_array(qpdf, kids)); assert(qpdf_oh_get_array_n_items(qpdf, kids) == 1); @@ -687,6 +688,12 @@ static void test24(char const* infile, qpdf_set_suppress_original_object_IDs(qpdf, QPDF_TRUE); qpdf_write(qpdf); report_errors(); + + /* Make sure we detect uninitialized objects */ + qpdf_data qpdf2 = qpdf_init(); + trailer = qpdf_get_trailer(qpdf2); + assert(! qpdf_oh_is_initialized(qpdf2, trailer)); + qpdf_cleanup(&qpdf2); } int main(int argc, char* argv[]) diff --git a/qpdf/qpdf.testcov b/qpdf/qpdf.testcov index 277bf8df..b974af63 100644 --- a/qpdf/qpdf.testcov +++ b/qpdf/qpdf.testcov @@ -600,3 +600,4 @@ QPDF_pages findPage not found 0 qpdf overlay page with no resources 0 QPDFObjectHandle check ownership 0 qpdf weak crypto warning 0 +qpdf-c called qpdf_oh_is_initialized 0 diff --git a/qpdf/qtest/qpdf/c-object-handles.out b/qpdf/qtest/qpdf/c-object-handles.out index 7fef9dad..675bacac 100644 --- a/qpdf/qtest/qpdf/c-object-handles.out +++ b/qpdf/qtest/qpdf/c-object-handles.out @@ -7,18 +7,18 @@ item 0: 0 0.00 item 1: 0 0.00 item 2: 612 612.00 item 3: 792 792.00 -warning: minimal.pdf (C API object handle 6): attempted access to unknown/uninitialized object handle +warning: minimal.pdf (C API object handle 6): attempted access to unknown object handle code: 5 file: minimal.pdf pos : 0 - text: attempted access to unknown/uninitialized object handle -warning: minimal.pdf (C API object handle 9): attempted access to unknown/uninitialized object handle + text: attempted access to unknown object handle +warning: minimal.pdf (C API object handle 9): attempted access to unknown object handle code: 5 file: minimal.pdf pos : 0 - text: attempted access to unknown/uninitialized object handle -warning: minimal.pdf (C API object handle 9): attempted access to unknown/uninitialized object handle + text: attempted access to unknown object handle +warning: minimal.pdf (C API object handle 9): attempted access to unknown object handle code: 5 file: minimal.pdf pos : 0 - text: attempted access to unknown/uninitialized object handle + text: attempted access to unknown object handle