diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2020-08-13 10:40:11 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2022-12-09 20:42:12 +0100 |
commit | ba2a5d693bad6d9bc9b0722fcdf8fc824eabceb4 (patch) | |
tree | ce82966f4b4ed963b26094f0ff26511d86a2fe10 | |
parent | ee29fbb859b681c154485dd5b012a20b953a0b41 (diff) |
Fix crash on serializing default-constructed QTimeZone
The serialization code neglected to check against null. Since zones
are saved either by IANA ID or in our special OffsetFromUtc format,
representing an invalid zone by a string that cannot possibly be a
valid IANA ID will do.
Fixes: QTBUG-86019
Change-Id: I6882026403d00f8b254aab34c645f1cf8f9fcc2d
Reviewed-by: Taylor Braun-Jones <taylor@braun-jones.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 14f3f419b0864944d75283a850dc0ce141feaf0e)
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
-rw-r--r-- | src/corelib/time/qtimezone.cpp | 14 | ||||
-rw-r--r-- | tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp | 28 |
2 files changed, 39 insertions, 3 deletions
diff --git a/src/corelib/time/qtimezone.cpp b/src/corelib/time/qtimezone.cpp index 6720ede9ed..c3e8947bdc 100644 --- a/src/corelib/time/qtimezone.cpp +++ b/src/corelib/time/qtimezone.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 John Layt <jlayt@kde.org> +** Copyright (C) 2020 John Layt <jlayt@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -975,9 +975,15 @@ QList<QByteArray> QTimeZone::windowsIdToIanaIds(const QByteArray &windowsId, } #ifndef QT_NO_DATASTREAM +// Invalid, as an IANA ID: too long, starts with - and has other invalid characters in it +static inline QString invalidId() { return QStringLiteral("-No Time Zone Specified!"); } + QDataStream &operator<<(QDataStream &ds, const QTimeZone &tz) { - tz.d->serialize(ds); + if (tz.isValid()) + tz.d->serialize(ds); + else + ds << invalidId(); return ds; } @@ -985,7 +991,9 @@ QDataStream &operator>>(QDataStream &ds, QTimeZone &tz) { QString ianaId; ds >> ianaId; - if (ianaId == QLatin1String("OffsetFromUtc")) { + if (ianaId == invalidId()) { + tz = QTimeZone(); + } else if (ianaId == QLatin1String("OffsetFromUtc")) { int utcOffset; QString name; QString abbreviation; diff --git a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp index 412f092377..59b94179f4 100644 --- a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp +++ b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp @@ -65,6 +65,7 @@ private slots: void isValidId_data(); void isValidId(); void malformed(); + void serialize(); // Backend tests void utcTest(); void icuTest(); @@ -951,6 +952,33 @@ void tst_QTimeZone::malformed() barf.offsetFromUtc(now); } +void tst_QTimeZone::serialize() +{ + int parts = 0; +#ifndef QT_NO_DEBUG_STREAM + qDebug() << QTimeZone(); // to verify no crash + parts++; +#endif +#ifndef QT_NO_DATASTREAM + QByteArray blob; + { + QDataStream stream(&blob, QIODevice::WriteOnly); + stream << QTimeZone("Europe/Oslo") << QTimeZone(420) << QTimeZone() << qint64(-1); + } + QDataStream stream(&blob, QIODevice::ReadOnly); + QTimeZone invalid, offset, oslo; + qint64 minusone; + stream >> oslo >> offset >> invalid >> minusone; + QCOMPARE(oslo, QTimeZone("Europe/Oslo")); + QCOMPARE(offset, QTimeZone(420)); + QVERIFY(!invalid.isValid()); + QCOMPARE(minusone, qint64(-1)); + parts++; +#endif + if (!parts) + QSKIP("No serialization enabled"); +} + void tst_QTimeZone::utcTest() { #ifdef QT_BUILD_INTERNAL |