Commit e3cc171d0210cc3230754d50285233ccb8759081

Authored by Jay Berkenbilt
1 parent bef2c222

C API: qpdf_oh_is_initialized

ChangeLog
1 2021-12-02 Jay Berkenbilt <ejb@ql.org> 1 2021-12-02 Jay Berkenbilt <ejb@ql.org>
2 2
  3 + * C API: Add qpdf_oh_is_initialized.
  4 +
3 * C API: Add qpdf_get_last_string_length to return the length of 5 * C API: Add qpdf_get_last_string_length to return the length of
4 the last string returned. This is necessary in order to fully 6 the last string returned. This is necessary in order to fully
5 retrieve values of strings that may contain embedded null characters. 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,6 +582,8 @@ extern &quot;C&quot; {
582 */ 582 */
583 583
584 QPDF_DLL 584 QPDF_DLL
  585 + QPDF_BOOL qpdf_oh_is_initialized(qpdf_data data, qpdf_oh oh);
  586 + QPDF_DLL
585 QPDF_BOOL qpdf_oh_is_bool(qpdf_data data, qpdf_oh oh); 587 QPDF_BOOL qpdf_oh_is_bool(qpdf_data data, qpdf_oh oh);
586 QPDF_DLL 588 QPDF_DLL
587 QPDF_BOOL qpdf_oh_is_null(qpdf_data data, qpdf_oh oh); 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,8 +881,7 @@ qpdf_oh_valid_internal(qpdf_data qpdf, qpdf_oh oh)
881 { 881 {
882 auto i = qpdf->oh_cache.find(oh); 882 auto i = qpdf->oh_cache.find(oh);
883 bool result = ((i != qpdf->oh_cache.end()) && 883 bool result = ((i != qpdf->oh_cache.end()) &&
884 - (i->second).getPointer() &&  
885 - (i->second)->isInitialized()); 884 + (i->second).getPointer());
886 if (! result) 885 if (! result)
887 { 886 {
888 QTC::TC("qpdf", "qpdf-c invalid object handle"); 887 QTC::TC("qpdf", "qpdf-c invalid object handle");
@@ -892,11 +891,18 @@ qpdf_oh_valid_internal(qpdf_data qpdf, qpdf_oh oh) @@ -892,11 +891,18 @@ qpdf_oh_valid_internal(qpdf_data qpdf, qpdf_oh oh)
892 qpdf->qpdf->getFilename(), 891 qpdf->qpdf->getFilename(),
893 std::string("C API object handle ") + 892 std::string("C API object handle ") +
894 QUtil::uint_to_string(oh), 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 return result; 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 QPDF_BOOL qpdf_oh_is_bool(qpdf_data qpdf, qpdf_oh oh) 906 QPDF_BOOL qpdf_oh_is_bool(qpdf_data qpdf, qpdf_oh oh)
901 { 907 {
902 QTC::TC("qpdf", "qpdf-c called qpdf_oh_is_bool"); 908 QTC::TC("qpdf", "qpdf-c called qpdf_oh_is_bool");
manual/qpdf-manual.xml
@@ -5133,6 +5133,13 @@ print &quot;\n&quot;; @@ -5133,6 +5133,13 @@ print &quot;\n&quot;;
5133 embedded null characters. 5133 embedded null characters.
5134 </para> 5134 </para>
5135 </listitem> 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 </itemizedlist> 5143 </itemizedlist>
5137 </listitem> 5144 </listitem>
5138 </itemizedlist> 5145 </itemizedlist>
qpdf/qpdf-ctest.c
@@ -512,6 +512,7 @@ static void test24(char const* infile, @@ -512,6 +512,7 @@ static void test24(char const* infile,
512 qpdf_oh_get_object_id(qpdf, root_from_trailer)); 512 qpdf_oh_get_object_id(qpdf, root_from_trailer));
513 qpdf_oh pages = qpdf_oh_get_key(qpdf, root, "/Pages"); 513 qpdf_oh pages = qpdf_oh_get_key(qpdf, root, "/Pages");
514 assert(qpdf_oh_is_dictionary(qpdf, pages)); 514 assert(qpdf_oh_is_dictionary(qpdf, pages));
  515 + assert(qpdf_oh_is_initialized(qpdf, pages));
515 qpdf_oh kids = qpdf_oh_get_key(qpdf, pages, "/Kids"); 516 qpdf_oh kids = qpdf_oh_get_key(qpdf, pages, "/Kids");
516 assert(qpdf_oh_is_array(qpdf, kids)); 517 assert(qpdf_oh_is_array(qpdf, kids));
517 assert(qpdf_oh_get_array_n_items(qpdf, kids) == 1); 518 assert(qpdf_oh_get_array_n_items(qpdf, kids) == 1);
@@ -687,6 +688,12 @@ static void test24(char const* infile, @@ -687,6 +688,12 @@ static void test24(char const* infile,
687 qpdf_set_suppress_original_object_IDs(qpdf, QPDF_TRUE); 688 qpdf_set_suppress_original_object_IDs(qpdf, QPDF_TRUE);
688 qpdf_write(qpdf); 689 qpdf_write(qpdf);
689 report_errors(); 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 int main(int argc, char* argv[]) 699 int main(int argc, char* argv[])
qpdf/qpdf.testcov
@@ -600,3 +600,4 @@ QPDF_pages findPage not found 0 @@ -600,3 +600,4 @@ QPDF_pages findPage not found 0
600 qpdf overlay page with no resources 0 600 qpdf overlay page with no resources 0
601 QPDFObjectHandle check ownership 0 601 QPDFObjectHandle check ownership 0
602 qpdf weak crypto warning 0 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,18 +7,18 @@ item 0: 0 0.00
7 item 1: 0 0.00 7 item 1: 0 0.00
8 item 2: 612 612.00 8 item 2: 612 612.00
9 item 3: 792 792.00 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 code: 5 11 code: 5
12 file: minimal.pdf 12 file: minimal.pdf
13 pos : 0 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 code: 5 16 code: 5
17 file: minimal.pdf 17 file: minimal.pdf
18 pos : 0 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 code: 5 21 code: 5
22 file: minimal.pdf 22 file: minimal.pdf
23 pos : 0 23 pos : 0
24 - text: attempted access to unknown/uninitialized object handle 24 + text: attempted access to unknown object handle