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,16 +361,18 @@ QPDFObjectHandle::getArrayNItems() const
361 QPDFObjectHandle 361 QPDFObjectHandle
362 QPDFObjectHandle::getArrayItem(int n) const 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 } else { 374 } else {
372 typeWarning("array", "returning null"); 375 typeWarning("array", "returning null");
373 - QTC::TC("qpdf", "QPDFObjectHandle array null for non-array");  
374 } 376 }
375 static auto constexpr msg = " -> null returned from invalid array access"sv; 377 static auto constexpr msg = " -> null returned from invalid array access"sv;
376 return QPDF_Null::create(obj, msg, ""); 378 return QPDF_Null::create(obj, msg, "");
qpdf/qpdf.testcov
@@ -290,7 +290,6 @@ QPDFParser bad token in parse 0 @@ -290,7 +290,6 @@ QPDFParser bad token in parse 0
290 QPDFParser bad token in parseRemainder 0 290 QPDFParser bad token in parseRemainder 0
291 QPDFParser eof in parse 0 291 QPDFParser eof in parse 0
292 QPDFParser eof in parseRemainder 0 292 QPDFParser eof in parseRemainder 0
293 -QPDFObjectHandle array bounds 0  
294 QPDFObjectHandle boolean returning false 0 293 QPDFObjectHandle boolean returning false 0
295 QPDFObjectHandle integer returning 0 0 294 QPDFObjectHandle integer returning 0 0
296 QPDFObjectHandle real returning 0.0 0 295 QPDFObjectHandle real returning 0.0 0
@@ -299,7 +298,6 @@ QPDFObjectHandle string returning empty string 0 @@ -299,7 +298,6 @@ QPDFObjectHandle string returning empty string 0
299 QPDFObjectHandle string returning empty utf8 0 298 QPDFObjectHandle string returning empty utf8 0
300 QPDFObjectHandle operator returning fake value 0 299 QPDFObjectHandle operator returning fake value 0
301 QPDFObjectHandle inlineimage returning empty data 0 300 QPDFObjectHandle inlineimage returning empty data 0
302 -QPDFObjectHandle array null for non-array 0  
303 QPDFObjectHandle array treating as empty vector 0 301 QPDFObjectHandle array treating as empty vector 0
304 QPDFObjectHandle array ignoring set item 0 302 QPDFObjectHandle array ignoring set item 0
305 QPDFObjectHandle set array bounds 0 303 QPDFObjectHandle set array bounds 0