mirror of https://github.com/qt/qtgrpc.git
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:
parent
f5b0365d35
commit
f66f3daffb
|
@ -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
|
||||
if constexpr (sizeof(T) != sizeof(qint64)) {
|
||||
if (ok) {
|
||||
constexpr auto Limits = []() {
|
||||
if constexpr (std::is_same_v<T, QtProtobuf::sfixed32>
|
||||
|| std::is_same_v<T, QtProtobuf::int32>) {
|
||||
using limits = std::numeric_limits<qint32>;
|
||||
ok = raw >= limits::min() && raw <= limits::max();
|
||||
} else if constexpr (std::is_same_v<T, QtProtobuf::fixed32>) {
|
||||
using limits = std::numeric_limits<quint32>;
|
||||
ok = raw >= limits::min() && raw <= limits::max();
|
||||
} else {
|
||||
using limits = std::numeric_limits<T>;
|
||||
ok = raw >= limits::min() && raw <= limits::max();
|
||||
}
|
||||
}
|
||||
|| std::is_same_v<T, QtProtobuf::int32>)
|
||||
return std::numeric_limits<qint32>{};
|
||||
else if constexpr (std::is_same_v<T, QtProtobuf::fixed32>)
|
||||
return std::numeric_limits<quint32>{};
|
||||
else
|
||||
return std::numeric_limits<T>{};
|
||||
}();
|
||||
ok = ok && (raw >= Limits.min() && raw <= Limits.max());
|
||||
}
|
||||
|
||||
return T(raw);
|
||||
return ok ? T(raw) : T{};
|
||||
}
|
||||
|
||||
template <typename T, if_json_int64<T> = true>
|
||||
|
|
Loading…
Reference in New Issue