diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index 75a1083..f6e41cc 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -361,16 +361,18 @@ QPDFObjectHandle::getArrayNItems() const QPDFObjectHandle QPDFObjectHandle::getArrayItem(int n) const { - if (auto array = as_array(strict)) { - if (auto const [success, oh] = array.at(n); success) { - return oh; - } else { - objectWarning("returning null for out of bounds array access"); - QTC::TC("qpdf", "QPDFObjectHandle array bounds"); + if (auto array = Array(*this)) { + if (auto result = array[n]) { + return result; + } + if (n >= 0 && std::cmp_less(n, array.size())) { + // sparse array null + return newNull(); } + objectWarning("returning null for out of bounds array access"); + } else { typeWarning("array", "returning null"); - QTC::TC("qpdf", "QPDFObjectHandle array null for non-array"); } static auto constexpr msg = " -> null returned from invalid array access"sv; return QPDF_Null::create(obj, msg, ""); diff --git a/qpdf/qpdf.testcov b/qpdf/qpdf.testcov index f1de4b6..237d052 100644 --- a/qpdf/qpdf.testcov +++ b/qpdf/qpdf.testcov @@ -290,7 +290,6 @@ QPDFParser bad token in parse 0 QPDFParser bad token in parseRemainder 0 QPDFParser eof in parse 0 QPDFParser eof in parseRemainder 0 -QPDFObjectHandle array bounds 0 QPDFObjectHandle boolean returning false 0 QPDFObjectHandle integer returning 0 0 QPDFObjectHandle real returning 0.0 0 @@ -299,7 +298,6 @@ QPDFObjectHandle string returning empty string 0 QPDFObjectHandle string returning empty utf8 0 QPDFObjectHandle operator returning fake value 0 QPDFObjectHandle inlineimage returning empty data 0 -QPDFObjectHandle array null for non-array 0 QPDFObjectHandle array treating as empty vector 0 QPDFObjectHandle array ignoring set item 0 QPDFObjectHandle set array bounds 0