Fix safe conversion

Ensure the resulting QScFixed values are no larger than can
be safely returned to int after shifting the fixed factor
away.

Fixes: QTBUG-88683
Change-Id: Id0754b021e5fa9a3cf0d15e37ac643cfc1509993
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
(cherry picked from commit a31484302d71c29c0e0e62f9941f9d5f0c87f9aa)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Allan Sandfeld Jensen 2020-11-20 15:58:04 +01:00 committed by Qt Cherry-pick Bot
parent 28b419c733
commit c0b0ade622

View File

@ -725,10 +725,17 @@ static inline qreal qSafeDivide(qreal x, qreal y)
static inline QScFixed qSafeFloatToQScFixed(qreal x)
{
qreal tmp = x * QScFixedFactor;
if (tmp > qreal(std::numeric_limits<QScFixed>::max()))
return std::numeric_limits<QScFixed>::max();
else if (tmp < qreal(std::numeric_limits<QScFixed>::min()))
return std::numeric_limits<QScFixed>::min();
#if Q_PROCESSOR_WORDSIZE == 8
if (tmp > qreal(INT_MAX) * QScFixedFactor)
return QScFixed(INT_MAX) * QScFixedFactor;
else if (tmp < qreal(INT_MIN) * QScFixedFactor)
return QScFixed(INT_MIN) * QScFixedFactor;
#else
if (tmp > qreal(INT_MAX))
return INT_MAX;
else if (tmp < qreal(INT_MIN))
return -INT_MAX;
#endif
return QScFixed(tmp);
}