Commit 182c2480df3d2dee3e430e02afe6bbee3e45eadd

Authored by m-holger
1 parent 4d37389b

Refactor QPDF_Array::setItem and rename to setAt

libqpdf/QPDFObjectHandle.cc
@@ -904,16 +904,16 @@ QPDFObjectHandle::getArrayAsVector() @@ -904,16 +904,16 @@ QPDFObjectHandle::getArrayAsVector()
904 void 904 void
905 QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item) 905 QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item)
906 { 906 {
907 - auto array = asArray();  
908 - if (array) {  
909 - checkOwnership(item);  
910 - array->setItem(n, item); 907 + if (auto array = asArray()) {
  908 + if (!array->setAt(n, item)) {
  909 + objectWarning("ignoring attempt to set out of bounds array item");
  910 + QTC::TC("qpdf", "QPDFObjectHandle set array bounds");
  911 + }
911 } else { 912 } else {
912 typeWarning("array", "ignoring attempt to set item"); 913 typeWarning("array", "ignoring attempt to set item");
913 QTC::TC("qpdf", "QPDFObjectHandle array ignoring set item"); 914 QTC::TC("qpdf", "QPDFObjectHandle array ignoring set item");
914 } 915 }
915 } 916 }
916 -  
917 void 917 void
918 QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items) 918 QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items)
919 { 919 {
libqpdf/QPDF_Array.cc
1 #include <qpdf/QPDF_Array.hh> 1 #include <qpdf/QPDF_Array.hh>
2 2
3 -#include <qpdf/QIntC.hh>  
4 #include <qpdf/QPDFObject_private.hh> 3 #include <qpdf/QPDFObject_private.hh>
5 -#include <qpdf/QUtil.hh>  
6 -#include <stdexcept>  
7 4
8 static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull(); 5 static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull();
9 6
@@ -188,18 +185,19 @@ QPDF_Array::getAsVector(std::vector&lt;QPDFObjectHandle&gt;&amp; v) const @@ -188,18 +185,19 @@ QPDF_Array::getAsVector(std::vector&lt;QPDFObjectHandle&gt;&amp; v) const
188 } 185 }
189 } 186 }
190 187
191 -void  
192 -QPDF_Array::setItem(int n, QPDFObjectHandle const& oh) 188 +bool
  189 +QPDF_Array::setAt(int at, QPDFObjectHandle const& oh)
193 { 190 {
  191 + if (at < 0 || at >= size()) {
  192 + return false;
  193 + }
  194 + checkOwnership(oh);
194 if (sparse) { 195 if (sparse) {
195 - sp_elements.setAt(n, oh); 196 + sp_elements.setAt(at, oh);
196 } else { 197 } else {
197 - size_t idx = size_t(n);  
198 - if (n < 0 || idx >= elements.size()) {  
199 - throw std::logic_error("bounds error setting item in QPDF_Array");  
200 - }  
201 - elements[idx] = oh.getObj(); 198 + elements[size_t(at)] = oh.getObj();
202 } 199 }
  200 + return true;
203 } 201 }
204 202
205 void 203 void
libqpdf/SparseOHArray.cc
@@ -31,19 +31,6 @@ SparseOHArray::disconnect() @@ -31,19 +31,6 @@ SparseOHArray::disconnect()
31 } 31 }
32 32
33 void 33 void
34 -SparseOHArray::setAt(int idx, QPDFObjectHandle oh)  
35 -{  
36 - if (idx >= this->n_elements) {  
37 - throw std::logic_error("bounds error setting item in SparseOHArray");  
38 - }  
39 - if (oh.isDirectNull()) {  
40 - this->elements.erase(idx);  
41 - } else {  
42 - this->elements[idx] = oh.getObj();  
43 - }  
44 -}  
45 -  
46 -void  
47 SparseOHArray::erase(int at) 34 SparseOHArray::erase(int at)
48 { 35 {
49 auto end = elements.end(); 36 auto end = elements.end();
libqpdf/qpdf/QPDF_Array.hh
@@ -28,9 +28,8 @@ class QPDF_Array: public QPDFValue @@ -28,9 +28,8 @@ class QPDF_Array: public QPDFValue
28 return sparse ? sp_elements.size() : int(elements.size()); 28 return sparse ? sp_elements.size() : int(elements.size());
29 } 29 }
30 QPDFObjectHandle at(int n) const noexcept; 30 QPDFObjectHandle at(int n) const noexcept;
  31 + bool setAt(int n, QPDFObjectHandle const& oh);
31 void getAsVector(std::vector<QPDFObjectHandle>&) const; 32 void getAsVector(std::vector<QPDFObjectHandle>&) const;
32 -  
33 - void setItem(int, QPDFObjectHandle const&);  
34 void setFromVector(std::vector<QPDFObjectHandle> const& items); 33 void setFromVector(std::vector<QPDFObjectHandle> const& items);
35 void setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& items); 34 void setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& items);
36 bool insert(int at, QPDFObjectHandle const& item); 35 bool insert(int at, QPDFObjectHandle const& item);
libqpdf/qpdf/SparseOHArray.hh
@@ -28,7 +28,11 @@ class SparseOHArray @@ -28,7 +28,11 @@ class SparseOHArray
28 } 28 }
29 QPDFObjectHandle at(int idx) const; 29 QPDFObjectHandle at(int idx) const;
30 void remove_last(); 30 void remove_last();
31 - void setAt(int idx, QPDFObjectHandle oh); 31 + void
  32 + setAt(int idx, QPDFObjectHandle oh)
  33 + {
  34 + elements[idx] = oh.getObj();
  35 + }
32 void erase(int idx); 36 void erase(int idx);
33 void insert(int idx, QPDFObjectHandle oh); 37 void insert(int idx, QPDFObjectHandle oh);
34 SparseOHArray copy(); 38 SparseOHArray copy();
qpdf/qpdf.testcov
@@ -303,6 +303,7 @@ QPDFObjectHandle array treating as empty 0 @@ -303,6 +303,7 @@ QPDFObjectHandle array treating as empty 0
303 QPDFObjectHandle array null for non-array 0 303 QPDFObjectHandle array null for non-array 0
304 QPDFObjectHandle array treating as empty vector 0 304 QPDFObjectHandle array treating as empty vector 0
305 QPDFObjectHandle array ignoring set item 0 305 QPDFObjectHandle array ignoring set item 0
  306 +QPDFObjectHandle set array bounds 0
306 QPDFObjectHandle array ignoring replace items 0 307 QPDFObjectHandle array ignoring replace items 0
307 QPDFObjectHandle array ignoring insert item 0 308 QPDFObjectHandle array ignoring insert item 0
308 QPDFObjectHandle insert array bounds 0 309 QPDFObjectHandle insert array bounds 0
qpdf/qtest/qpdf/object-types-os.out
@@ -6,6 +6,7 @@ WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operatio @@ -6,6 +6,7 @@ WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operatio
6 WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 384: ignoring attempt to erase out of bounds array item 6 WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 384: ignoring attempt to erase out of bounds array item
7 WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 384: ignoring attempt to erase out of bounds array item 7 WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 384: ignoring attempt to erase out of bounds array item
8 WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 384: ignoring attempt to insert out of bounds array item 8 WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 384: ignoring attempt to insert out of bounds array item
  9 +WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 384: ignoring attempt to set out of bounds array item
9 WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to erase item 10 WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to erase item
10 WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to insert item 11 WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to insert item
11 WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to replace items 12 WARNING: object-types-os.pdf object stream 1, object 7 0 at offset 429: operation for array attempted on object of type integer: ignoring attempt to replace items
qpdf/qtest/qpdf/object-types.out
@@ -6,6 +6,7 @@ WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempt @@ -6,6 +6,7 @@ WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempt
6 WARNING: object-types.pdf, object 8 0 at offset 717: ignoring attempt to erase out of bounds array item 6 WARNING: object-types.pdf, object 8 0 at offset 717: ignoring attempt to erase out of bounds array item
7 WARNING: object-types.pdf, object 8 0 at offset 717: ignoring attempt to erase out of bounds array item 7 WARNING: object-types.pdf, object 8 0 at offset 717: ignoring attempt to erase out of bounds array item
8 WARNING: object-types.pdf, object 8 0 at offset 717: ignoring attempt to insert out of bounds array item 8 WARNING: object-types.pdf, object 8 0 at offset 717: ignoring attempt to insert out of bounds array item
  9 +WARNING: object-types.pdf, object 8 0 at offset 717: ignoring attempt to set out of bounds array item
9 WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to erase item 10 WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to erase item
10 WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to insert item 11 WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to insert item
11 WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to replace items 12 WARNING: object-types.pdf, object 8 0 at offset 669: operation for array attempted on object of type integer: ignoring attempt to replace items
qpdf/test_driver.cc
@@ -1507,6 +1507,7 @@ test_42(QPDF&amp; pdf, char const* arg2) @@ -1507,6 +1507,7 @@ test_42(QPDF&amp; pdf, char const* arg2)
1507 array.eraseItem(-1); 1507 array.eraseItem(-1);
1508 array.eraseItem(16059); 1508 array.eraseItem(16059);
1509 array.insertItem(42, "/Dontpanic"_qpdf); 1509 array.insertItem(42, "/Dontpanic"_qpdf);
  1510 + array.setArrayItem(42, "/Dontpanic"_qpdf);
1510 integer.eraseItem(0); 1511 integer.eraseItem(0);
1511 integer.insertItem(0, null); 1512 integer.insertItem(0, null);
1512 integer.setArrayFromVector(std::vector<QPDFObjectHandle>()); 1513 integer.setArrayFromVector(std::vector<QPDFObjectHandle>());