Commit f699aacb83a2c5ba906771178f619664b6bc18b3

Authored by m-holger
1 parent 77bd34aa

Refactor `QPDFWriter`: simplify array handling, replace `getArrayNItems` with `s…

…ize`, and centralize `empty` checks.
libqpdf/QPDFWriter.cc
... ... @@ -1538,9 +1538,7 @@ QPDFWriter::unparseObject(
1538 1538 object.removeKey("/Length");
1539 1539  
1540 1540 // If /DecodeParms is an empty list, remove it.
1541   - if (object.getKey("/DecodeParms").isArray() &&
1542   - (0 == object.getKey("/DecodeParms").getArrayNItems())) {
1543   - QTC::TC("qpdf", "QPDFWriter remove empty DecodeParms");
  1541 + if (object.getKey("/DecodeParms").empty()) {
1544 1542 object.removeKey("/DecodeParms");
1545 1543 }
1546 1544  
... ... @@ -1558,22 +1556,19 @@ QPDFWriter::unparseObject(
1558 1556 object.removeKey("/Filter");
1559 1557 object.removeKey("/DecodeParms");
1560 1558 } else {
1561   - int idx = -1;
1562   - for (int i = 0; i < filter.getArrayNItems(); ++i) {
1563   - QPDFObjectHandle item = filter.getArrayItem(i);
  1559 + int idx = 0;
  1560 + for (auto const& item: filter.as_array()) {
1564 1561 if (item.isNameAndEquals("/Crypt")) {
1565   - idx = i;
  1562 + // If filter is an array, then the code in QPDF_Stream has already
  1563 + // verified that DecodeParms and Filters are arrays of the same
  1564 + // length, but if they weren't for some reason, eraseItem does type
  1565 + // and bounds checking. Fuzzing tells us that this can actually
  1566 + // happen.
  1567 + filter.eraseItem(idx);
  1568 + decode_parms.eraseItem(idx);
1566 1569 break;
1567 1570 }
1568   - }
1569   - if (idx >= 0) {
1570   - // If filter is an array, then the code in QPDF_Stream has already
1571   - // verified that DecodeParms and Filters are arrays of the same length,
1572   - // but if they weren't for some reason, eraseItem does type and bounds
1573   - // checking.
1574   - QTC::TC("qpdf", "QPDFWriter remove Crypt");
1575   - filter.eraseItem(idx);
1576   - decode_parms.eraseItem(idx);
  1571 + ++idx;
1577 1572 }
1578 1573 }
1579 1574 }
... ... @@ -1965,7 +1960,7 @@ QPDFWriter::initializeSpecialStreams()
1965 1960 QPDFObjectHandle contents = page.getKey("/Contents");
1966 1961 std::vector<QPDFObjGen> contents_objects;
1967 1962 if (contents.isArray()) {
1968   - int n = contents.getArrayNItems();
  1963 + int n = static_cast<int>(contents.size());
1969 1964 for (int i = 0; i < n; ++i) {
1970 1965 contents_objects.push_back(contents.getArrayItem(i).getObjGen());
1971 1966 }
... ...
qpdf/qpdf.testcov
... ... @@ -239,7 +239,6 @@ QPDFWriter preserve ADBE 0
239 239 QPDF_encryption skip 0x28 0
240 240 QPDF_encrypt crypt array 0
241 241 QPDF_encryption CFM AESV3 0
242   -QPDFWriter remove Crypt 0
243 242 qpdf-c called qpdf_get_pdf_extension_level 0
244 243 qpdf-c called qpdf_set_r5_encryption_parameters 0
245 244 qpdf-c called qpdf_set_r6_encryption_parameters 0
... ... @@ -423,7 +422,6 @@ QPDFPageObjectHelper externalize inline image 0
423 422 QPDFPageObjectHelper keep inline image 0
424 423 QPDFJob image optimize colorspace 0
425 424 QPDFJob image optimize bits per component 0
426   -QPDFWriter remove empty DecodeParms 0
427 425 QPDF xref skipped space 0
428 426 QPDF eof skipping spaces before xref 1
429 427 QPDF_encryption user matches owner V < 5 0
... ...