protobuf: fix deserializeList() warnings and improve clarity

Fixes a compiler warning from implicit conversion by introducing a safe
qsizetype cast. Also improves readability and efficiency by renaming
variables and pre-allocating list storage aswell as adding a missing
move.

Pick-to: 6.10 6.9 6.8
Change-Id: I5eb34621690893e06e68c7b821e93e3462388595
Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
This commit is contained in:
Dennis Oberst 2025-08-05 12:38:29 +02:00
parent c764a9b223
commit 66e4a79353
1 changed files with 14 additions and 6 deletions

View File

@ -338,23 +338,31 @@ template <typename V, if_length_delimited<V> = false>
template <typename V>
[[nodiscard]] bool deserializeList(QProtobufSelfcheckIterator &it, QVariant &previousValue)
{
QList<V> out;
auto opt = deserializeVarintCommon<QtProtobuf::uint64>(it);
static constexpr auto
MaxSafeCount = static_cast<quint64>(std::numeric_limits<qsizetype>::max());
const auto opt = deserializeVarintCommon<QtProtobuf::uint64>(it);
if (!opt)
return false;
quint64 count = *opt;
if (count > quint64(std::numeric_limits<qsizetype>::max()))
const quint64 count = *opt;
if (count > MaxSafeCount)
return false;
QProtobufSelfcheckIterator lastVarint = it + count;
const auto safeCount = static_cast<qsizetype>(count);
const QProtobufSelfcheckIterator lastVarint = it + safeCount;
if (!lastVarint.isValid())
return false;
QList<V> out;
out.reserve(safeCount);
while (it != lastVarint) {
QVariant variantValue;
if (!deserializeBasic<V>(it, variantValue))
return false;
out.append(variantValue.value<V>());
}
previousValue.setValue(out);
previousValue.setValue(std::move(out));
return true;
}