QDateTime: prevent signed integer overflow

While the code checks for the possibility that leftMillis-rightMillis
may overflow, it fails to take into account that the result may be
minimal ("INT64_MIN", so to speak) and passes it to qAbs, triggering UB
in there. Since it's just a range check and we don't need the result to
be signed, use qUnsignedAbs.

Change-Id: I7f1a4bbc521fdc164ca20fedf0e995359a67ea4d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit 45e90b73c75b2c7997a0f8d750f9927f64e05a0e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit df0d74dca49a5a2f858f5c6b62a4b9188fe1c9c8)
bb10
Giuseppe D'Angelo 2025-01-28 03:11:58 +01:00 committed by Qt Cherry-pick Bot
parent a4ab1ceb16
commit 77a12d3481
1 changed files with 3 additions and 3 deletions

View File

@ -3182,10 +3182,10 @@ static inline bool usesSameOffset(const QDateTimeData &a, const QDateTimeData &b
*/
bool areFarEnoughApart(qint64 leftMillis, qint64 rightMillis)
{
constexpr qint64 UtcOffsetMillisRange
= (QTimeZone::MaxUtcOffsetSecs - QTimeZone::MinUtcOffsetSecs) * MSECS_PER_SEC;
constexpr quint64 UtcOffsetMillisRange
= quint64(QTimeZone::MaxUtcOffsetSecs - QTimeZone::MinUtcOffsetSecs) * MSECS_PER_SEC;
qint64 gap = 0;
return qSubOverflow(leftMillis, rightMillis, &gap) || qAbs(gap) > UtcOffsetMillisRange;
return qSubOverflow(leftMillis, rightMillis, &gap) || QtPrivate::qUnsignedAbs(gap) > UtcOffsetMillisRange;
}
// Refresh the LocalTime or TimeZone validity and offset