Commit e3cc171d0210cc3230754d50285233ccb8759081

Authored by Jay Berkenbilt
1 parent bef2c222

C API: qpdf_oh_is_initialized

ChangeLog
1 1 2021-12-02 Jay Berkenbilt <ejb@ql.org>
2 2  
  3 + * C API: Add qpdf_oh_is_initialized.
  4 +
3 5 * C API: Add qpdf_get_last_string_length to return the length of
4 6 the last string returned. This is necessary in order to fully
5 7 retrieve values of strings that may contain embedded null characters.
... ...
include/qpdf/qpdf-c.h
... ... @@ -582,6 +582,8 @@ extern &quot;C&quot; {
582 582 */
583 583  
584 584 QPDF_DLL
  585 + QPDF_BOOL qpdf_oh_is_initialized(qpdf_data data, qpdf_oh oh);
  586 + QPDF_DLL
585 587 QPDF_BOOL qpdf_oh_is_bool(qpdf_data data, qpdf_oh oh);
586 588 QPDF_DLL
587 589 QPDF_BOOL qpdf_oh_is_null(qpdf_data data, qpdf_oh oh);
... ...
libqpdf/qpdf-c.cc
... ... @@ -881,8 +881,7 @@ qpdf_oh_valid_internal(qpdf_data qpdf, qpdf_oh oh)
881 881 {
882 882 auto i = qpdf->oh_cache.find(oh);
883 883 bool result = ((i != qpdf->oh_cache.end()) &&
884   - (i->second).getPointer() &&
885   - (i->second)->isInitialized());
  884 + (i->second).getPointer());
886 885 if (! result)
887 886 {
888 887 QTC::TC("qpdf", "qpdf-c invalid object handle");
... ... @@ -892,11 +891,18 @@ qpdf_oh_valid_internal(qpdf_data qpdf, qpdf_oh oh)
892 891 qpdf->qpdf->getFilename(),
893 892 std::string("C API object handle ") +
894 893 QUtil::uint_to_string(oh),
895   - 0, "attempted access to unknown/uninitialized object handle"));
  894 + 0, "attempted access to unknown object handle"));
896 895 }
897 896 return result;
898 897 }
899 898  
  899 +QPDF_BOOL qpdf_oh_is_initialized(qpdf_data qpdf, qpdf_oh oh)
  900 +{
  901 + QTC::TC("qpdf", "qpdf-c called qpdf_oh_is_initialized");
  902 + return (qpdf_oh_valid_internal(qpdf, oh) &&
  903 + qpdf->oh_cache[oh]->isInitialized());
  904 +}
  905 +
900 906 QPDF_BOOL qpdf_oh_is_bool(qpdf_data qpdf, qpdf_oh oh)
901 907 {
902 908 QTC::TC("qpdf", "qpdf-c called qpdf_oh_is_bool");
... ...
manual/qpdf-manual.xml
... ... @@ -5133,6 +5133,13 @@ print &quot;\n&quot;;
5133 5133 embedded null characters.
5134 5134 </para>
5135 5135 </listitem>
  5136 + <listitem>
  5137 + <para>
  5138 + Add <function>qpdf_oh_is_initialized</function> to the
  5139 + C API. While you can't directly create uninitialized objects
  5140 + from the C API, you still have to be able to detect them.
  5141 + </para>
  5142 + </listitem>
5136 5143 </itemizedlist>
5137 5144 </listitem>
5138 5145 </itemizedlist>
... ...
qpdf/qpdf-ctest.c
... ... @@ -512,6 +512,7 @@ static void test24(char const* infile,
512 512 qpdf_oh_get_object_id(qpdf, root_from_trailer));
513 513 qpdf_oh pages = qpdf_oh_get_key(qpdf, root, "/Pages");
514 514 assert(qpdf_oh_is_dictionary(qpdf, pages));
  515 + assert(qpdf_oh_is_initialized(qpdf, pages));
515 516 qpdf_oh kids = qpdf_oh_get_key(qpdf, pages, "/Kids");
516 517 assert(qpdf_oh_is_array(qpdf, kids));
517 518 assert(qpdf_oh_get_array_n_items(qpdf, kids) == 1);
... ... @@ -687,6 +688,12 @@ static void test24(char const* infile,
687 688 qpdf_set_suppress_original_object_IDs(qpdf, QPDF_TRUE);
688 689 qpdf_write(qpdf);
689 690 report_errors();
  691 +
  692 + /* Make sure we detect uninitialized objects */
  693 + qpdf_data qpdf2 = qpdf_init();
  694 + trailer = qpdf_get_trailer(qpdf2);
  695 + assert(! qpdf_oh_is_initialized(qpdf2, trailer));
  696 + qpdf_cleanup(&qpdf2);
690 697 }
691 698  
692 699 int main(int argc, char* argv[])
... ...
qpdf/qpdf.testcov
... ... @@ -600,3 +600,4 @@ QPDF_pages findPage not found 0
600 600 qpdf overlay page with no resources 0
601 601 QPDFObjectHandle check ownership 0
602 602 qpdf weak crypto warning 0
  603 +qpdf-c called qpdf_oh_is_initialized 0
... ...
qpdf/qtest/qpdf/c-object-handles.out
... ... @@ -7,18 +7,18 @@ item 0: 0 0.00
7 7 item 1: 0 0.00
8 8 item 2: 612 612.00
9 9 item 3: 792 792.00
10   -warning: minimal.pdf (C API object handle 6): attempted access to unknown/uninitialized object handle
  10 +warning: minimal.pdf (C API object handle 6): attempted access to unknown object handle
11 11 code: 5
12 12 file: minimal.pdf
13 13 pos : 0
14   - text: attempted access to unknown/uninitialized object handle
15   -warning: minimal.pdf (C API object handle 9): attempted access to unknown/uninitialized object handle
  14 + text: attempted access to unknown object handle
  15 +warning: minimal.pdf (C API object handle 9): attempted access to unknown object handle
16 16 code: 5
17 17 file: minimal.pdf
18 18 pos : 0
19   - text: attempted access to unknown/uninitialized object handle
20   -warning: minimal.pdf (C API object handle 9): attempted access to unknown/uninitialized object handle
  19 + text: attempted access to unknown object handle
  20 +warning: minimal.pdf (C API object handle 9): attempted access to unknown object handle
21 21 code: 5
22 22 file: minimal.pdf
23 23 pos : 0
24   - text: attempted access to unknown/uninitialized object handle
  24 + text: attempted access to unknown object handle
... ...