Ensure setDateTime clears status also when short

Previously, if multiplication overflowed when trying to set the date
and time of a formerly short-form QDateTime, its status didn't get set
to reflect the failed validity check. Added a test that now correctly
detects that it's produced an invalid date-time on overflow, where
previously it produced a wrong valid date-time.

Change-Id: Id46ca34d1e32e9b9b0630f3723cefd1c13b5761e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Edward Welbourne 2021-03-01 14:17:00 +01:00
parent 5e89c1406e
commit 517745fc9d
2 changed files with 10 additions and 1 deletions

View File

@ -3050,7 +3050,9 @@ static void setDateTime(QDateTimeData &d, QDate date, QTime time)
if (mul_overflow(days, std::integral_constant<qint64, MSECS_PER_DAY>(), &msecs)
|| add_overflow(msecs, qint64(ds), &msecs)) {
newStatus = QDateTimePrivate::StatusFlags{};
} else if (d.isShort()) {
msecs = 0;
}
if (d.isShort()) {
// let's see if we can keep this short
if (msecsCanBeSmall(msecs)) {
// yes, we can

View File

@ -717,6 +717,13 @@ void tst_QDateTime::setMSecsSinceEpoch()
QDateTime reference(QDate(1970, 1, 1), QTime(0, 0), Qt::UTC);
QCOMPARE(dt, reference.addMSecs(msecs));
if ((localTimeType == LocalTimeAheadOfUtc && msecs == std::numeric_limits<qint64>::max())
|| (localTimeType == LocalTimeBehindUtc && msecs == std::numeric_limits<qint64>::min())) {
QDateTime curt = QDate(1970, 1, 1).startOfDay(); // initially in short-form
curt.setMSecsSinceEpoch(msecs); // Overflows due to offset
QVERIFY(!curt.isValid());
}
}
void tst_QDateTime::fromMSecsSinceEpoch_data()