ProtobufScalarJsonSerializers: fix overflowed return value

Restructure the limits check to be more compact by separating the
invariant from the variant. The latter is extracted into a constexpr
lambda check.

Fix the overflow by checking the ok flag and return a default
initialized T.

Coverity-Id: 479421
Change-Id: Ia50fcba5de1335c2a26478d10f89b2d9c5696c4b
Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Dennis Oberst 2025-03-27 19:38:34 +01:00
parent f5b0365d35
commit f66f3daffb
1 changed files with 10 additions and 13 deletions

View File

@ -173,22 +173,19 @@ T deserialize(const QJsonValue &value, bool &ok)
// For types that "smaller" than qint64 we need to check if the value fits its limits range // For types that "smaller" than qint64 we need to check if the value fits its limits range
if constexpr (sizeof(T) != sizeof(qint64)) { if constexpr (sizeof(T) != sizeof(qint64)) {
if (ok) { constexpr auto Limits = []() {
if constexpr (std::is_same_v<T, QtProtobuf::sfixed32> if constexpr (std::is_same_v<T, QtProtobuf::sfixed32>
|| std::is_same_v<T, QtProtobuf::int32>) { || std::is_same_v<T, QtProtobuf::int32>)
using limits = std::numeric_limits<qint32>; return std::numeric_limits<qint32>{};
ok = raw >= limits::min() && raw <= limits::max(); else if constexpr (std::is_same_v<T, QtProtobuf::fixed32>)
} else if constexpr (std::is_same_v<T, QtProtobuf::fixed32>) { return std::numeric_limits<quint32>{};
using limits = std::numeric_limits<quint32>; else
ok = raw >= limits::min() && raw <= limits::max(); return std::numeric_limits<T>{};
} else { }();
using limits = std::numeric_limits<T>; ok = ok && (raw >= Limits.min() && raw <= Limits.max());
ok = raw >= limits::min() && raw <= limits::max();
}
}
} }
return T(raw); return ok ? T(raw) : T{};
} }
template <typename T, if_json_int64<T> = true> template <typename T, if_json_int64<T> = true>