Commit 1e8ce4a77a4bdaf548f6169fb58647edf12f311c

Authored by m-holger
1 parent 47b55307

Refactor `getArrayItem`: simplify logic, replace bounds check with `std::cmp_les…

…s`, remove redundant trace, and improve null handling.
libqpdf/QPDF_Array.cc
... ... @@ -361,16 +361,18 @@ QPDFObjectHandle::getArrayNItems() const
361 361 QPDFObjectHandle
362 362 QPDFObjectHandle::getArrayItem(int n) const
363 363 {
364   - if (auto array = as_array(strict)) {
365   - if (auto const [success, oh] = array.at(n); success) {
366   - return oh;
367   - } else {
368   - objectWarning("returning null for out of bounds array access");
369   - QTC::TC("qpdf", "QPDFObjectHandle array bounds");
  364 + if (auto array = Array(*this)) {
  365 + if (auto result = array[n]) {
  366 + return result;
  367 + }
  368 + if (n >= 0 && std::cmp_less(n, array.size())) {
  369 + // sparse array null
  370 + return newNull();
370 371 }
  372 + objectWarning("returning null for out of bounds array access");
  373 +
371 374 } else {
372 375 typeWarning("array", "returning null");
373   - QTC::TC("qpdf", "QPDFObjectHandle array null for non-array");
374 376 }
375 377 static auto constexpr msg = " -> null returned from invalid array access"sv;
376 378 return QPDF_Null::create(obj, msg, "");
... ...
qpdf/qpdf.testcov
... ... @@ -290,7 +290,6 @@ QPDFParser bad token in parse 0
290 290 QPDFParser bad token in parseRemainder 0
291 291 QPDFParser eof in parse 0
292 292 QPDFParser eof in parseRemainder 0
293   -QPDFObjectHandle array bounds 0
294 293 QPDFObjectHandle boolean returning false 0
295 294 QPDFObjectHandle integer returning 0 0
296 295 QPDFObjectHandle real returning 0.0 0
... ... @@ -299,7 +298,6 @@ QPDFObjectHandle string returning empty string 0
299 298 QPDFObjectHandle string returning empty utf8 0
300 299 QPDFObjectHandle operator returning fake value 0
301 300 QPDFObjectHandle inlineimage returning empty data 0
302   -QPDFObjectHandle array null for non-array 0
303 301 QPDFObjectHandle array treating as empty vector 0
304 302 QPDFObjectHandle array ignoring set item 0
305 303 QPDFObjectHandle set array bounds 0
... ...