summaryrefslogtreecommitdiffstats
path: root/src/corelib/time
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2018-06-13 19:12:15 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2020-10-09 01:11:20 +0200
commit92f0f99c23f981a551fde0d0242d414fac4dacbd (patch)
tree1ee66f4574837a813e83218c96bff93814fdab21 /src/corelib/time
parent2a6f2fe9ef9a5d0755443ba94183d97b2fac1a28 (diff)
Check validity before adding to a QDateTime
QDateTime's addDays(), addMonths() and addYears() neglected to check for validity before doing their job, with the result that they could produce "valid" (but wildly inappropriate) results if used on an invalid date-time. Added tests for this case (and the boundary). Change-Id: I7b0d638501cb5d875a678cde213547a83ed7529e Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/time')
-rw-r--r--src/corelib/time/qdatetime.cpp9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp
index 0be4f290be..98b5dc8212 100644
--- a/src/corelib/time/qdatetime.cpp
+++ b/src/corelib/time/qdatetime.cpp
@@ -4154,6 +4154,9 @@ static inline void massageAdjustedDateTime(QDateTimeData &d, QDate date, QTime t
QDateTime QDateTime::addDays(qint64 ndays) const
{
+ if (isNull())
+ return QDateTime();
+
QDateTime dt(*this);
QPair<QDate, QTime> p = getDateTime(d);
massageAdjustedDateTime(dt.d, p.first.addDays(ndays), p.second);
@@ -4176,6 +4179,9 @@ QDateTime QDateTime::addDays(qint64 ndays) const
QDateTime QDateTime::addMonths(int nmonths) const
{
+ if (isNull())
+ return QDateTime();
+
QDateTime dt(*this);
QPair<QDate, QTime> p = getDateTime(d);
massageAdjustedDateTime(dt.d, p.first.addMonths(nmonths), p.second);
@@ -4198,6 +4204,9 @@ QDateTime QDateTime::addMonths(int nmonths) const
QDateTime QDateTime::addYears(int nyears) const
{
+ if (isNull())
+ return QDateTime();
+
QDateTime dt(*this);
QPair<QDate, QTime> p = getDateTime(d);
massageAdjustedDateTime(dt.d, p.first.addYears(nyears), p.second);