Fix an assertion failure in massageAdjustedDateTime()

The QDateTimeData &d it's passed is a copy that's about to be
modified; before we do so, we haven't detached so its internals have a
ref-count of two, contradicting an assertion in the non-const
Data::operator->(); so just directly access d.d->m_timezone, since we
know that spec == TimeZone implies !isShort().
Added test that triggered the assertion and now doesn't.

Fixes: QTBUG-99668
Pick-to: 6.3 6.2 6.2.3 5.15
Change-Id: I07321ad91be5adce524be18e4ab82eee7110dc6a
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Edward Welbourne 2022-01-11 15:01:22 +01:00
parent b84f4a350d
commit 5d228beb52
2 changed files with 16 additions and 4 deletions

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Copyright (C) 2022 The Qt Company Ltd.
** Copyright (C) 2021 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
@ -4373,13 +4373,13 @@ static inline void massageAdjustedDateTime(QDateTimeData &d, QDate date, QTime t
if (spec == Qt::LocalTime)
utc = QDateTimePrivate::localMSecsToEpochMSecs(local, &dst, &date, &time);
#if QT_CONFIG(timezone)
else if (spec == Qt::TimeZone && d->m_timeZone.isValid())
utc = QDateTimePrivate::zoneMSecsToEpochMSecs(local, d->m_timeZone, &dst, &date, &time);
else if (spec == Qt::TimeZone && d.d->m_timeZone.isValid())
utc = QDateTimePrivate::zoneMSecsToEpochMSecs(local, d.d->m_timeZone, &dst, &date, &time);
#endif // timezone
else
dst = QDateTimePrivate::UnknownDaylightTime;
setDateTime(d, date, time);
setDateTime(d, date, time); // Detaches d, if needed.
status = getStatus(d); // Updated by setDateTime()
const bool ok = (dst != QDateTimePrivate::UnknownDaylightTime
&& (status & QDateTimePrivate::ValidDate)

View File

@ -1134,6 +1134,18 @@ void tst_QDateTime::addDays()
QCOMPARE(dt2.timeSpec(), Qt::OffsetFromUTC);
QCOMPARE(dt2.offsetFromUtc(), 60 * 60);
#if QT_CONFIG(timezone)
const QTimeZone cet("Europe/Oslo");
if (cet.isValid()) {
dt1 = QDate(2022, 1, 10).startOfDay(cet);
dt2 = dt1.addDays(2); // QTBUG-99668: should not assert
QCOMPARE(dt2.date(), QDate(2022, 1, 12));
QCOMPARE(dt2.time(), QTime(0, 0));
QCOMPARE(dt2.timeSpec(), Qt::TimeZone);
QCOMPARE(dt2.timeZone(), cet);
}
#endif
// Test last UTC second of 1969 *is* valid (despite being time_t(-1))
dt1 = QDateTime(QDate(1969, 12, 30), QTime(23, 59, 59), Qt::UTC).toLocalTime().addDays(1);
QVERIFY(dt1.isValid());