From c3aa4cf95257416bb6bcfb43c9e0e54b9af36b4b Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 13 Apr 2021 15:26:35 +0200 Subject: [PATCH] Catch invalid offset returns from QTimeZone backends If the backends run into an error in computing the offset, they return INT_MIN; but they are valled via the front-end, which returns zero when the zone is invalid. So treat INT_MIN returns from the backend the same as the case of being invalid. Change-Id: Ic3c4dfe964dbfba4030c770213eca8a63e84736d Reviewed-by: Thiago Macieira --- src/corelib/time/qtimezone.cpp | 24 ++++++++++++------- src/corelib/time/qtimezoneprivate.cpp | 5 +++- .../corelib/time/qtimezone/tst_qtimezone.cpp | 4 ++-- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/corelib/time/qtimezone.cpp b/src/corelib/time/qtimezone.cpp index b9c6d727c8..9ccb0e5f02 100644 --- a/src/corelib/time/qtimezone.cpp +++ b/src/corelib/time/qtimezone.cpp @@ -607,9 +607,11 @@ QString QTimeZone::abbreviation(const QDateTime &atDateTime) const int QTimeZone::offsetFromUtc(const QDateTime &atDateTime) const { - if (isValid()) - return d->offsetFromUtc(atDateTime.toMSecsSinceEpoch()); - + if (isValid()) { + const int offset = d->offsetFromUtc(atDateTime.toMSecsSinceEpoch()); + if (offset != QTimeZonePrivate::invalidSeconds()) + return offset; + } return 0; } @@ -627,9 +629,11 @@ int QTimeZone::offsetFromUtc(const QDateTime &atDateTime) const int QTimeZone::standardTimeOffset(const QDateTime &atDateTime) const { - if (isValid()) - return d->standardTimeOffset(atDateTime.toMSecsSinceEpoch()); - + if (isValid()) { + const int offset = d->standardTimeOffset(atDateTime.toMSecsSinceEpoch()); + if (offset != QTimeZonePrivate::invalidSeconds()) + return offset; + } return 0; } @@ -647,9 +651,11 @@ int QTimeZone::standardTimeOffset(const QDateTime &atDateTime) const int QTimeZone::daylightTimeOffset(const QDateTime &atDateTime) const { - if (hasDaylightTime()) - return d->daylightTimeOffset(atDateTime.toMSecsSinceEpoch()); - + if (hasDaylightTime()) { + const int offset = d->daylightTimeOffset(atDateTime.toMSecsSinceEpoch()); + if (offset != QTimeZonePrivate::invalidSeconds()) + return offset; + } return 0; } diff --git a/src/corelib/time/qtimezoneprivate.cpp b/src/corelib/time/qtimezoneprivate.cpp index be00f658bc..9053d12f01 100644 --- a/src/corelib/time/qtimezoneprivate.cpp +++ b/src/corelib/time/qtimezoneprivate.cpp @@ -224,7 +224,10 @@ QString QTimeZonePrivate::abbreviation(qint64 atMSecsSinceEpoch) const int QTimeZonePrivate::offsetFromUtc(qint64 atMSecsSinceEpoch) const { - return standardTimeOffset(atMSecsSinceEpoch) + daylightTimeOffset(atMSecsSinceEpoch); + const int std = standardTimeOffset(atMSecsSinceEpoch); + const int dst = daylightTimeOffset(atMSecsSinceEpoch); + const int bad = invalidSeconds(); + return std == bad || dst == bad ? bad : std + dst; } int QTimeZonePrivate::standardTimeOffset(qint64 atMSecsSinceEpoch) const diff --git a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp index 92676108e0..9c505e5baa 100644 --- a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp +++ b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp @@ -996,10 +996,10 @@ void tst_QTimeZone::malformed() const QDateTime now = QDateTime::currentDateTime(); QTimeZone barf("QUT4tCZ0 , /"); if (barf.isValid()) - barf.offsetFromUtc(now); + QCOMPARE(barf.offsetFromUtc(now), 0); barf = QTimeZone("QtC+09,,MA"); if (barf.isValid()) - barf.offsetFromUtc(now); + QCOMPARE(barf.offsetFromUtc(now), 0); } void tst_QTimeZone::utcTest()