diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2020-04-23 16:03:59 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2020-05-08 09:51:24 +0200 |
commit | e325bd68fd6bdcbcb0c4e7478cb6371b60d89905 (patch) | |
tree | 5533ab5f22ea44b4d22c483f635c8b5a89c73e67 /src/corelib/time/qdatetime.cpp | |
parent | bd5fe6f385daebd0e9156c80508095e47f53a693 (diff) |
Catch overflow in QDateTime::fromSecsSinceEpoch()
It's documented to be undefined if the number of seconds is outside
the allowed range, but it doesn't hurt for that undefined behavior to
happen to be that the result is invalid. Added a simple test.
Change-Id: I20c3f680c7948b3904f213452272133be77e4d62
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/time/qdatetime.cpp')
-rw-r--r-- | src/corelib/time/qdatetime.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index 2e4757b525..248fd1d5ce 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -4608,6 +4608,10 @@ QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int of */ QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offsetSeconds) { + constexpr qint64 maxSeconds = std::numeric_limits<qint64>::max() / 1000; + constexpr qint64 minSeconds = std::numeric_limits<qint64>::min() / 1000; + if (secs > maxSeconds || secs < minSeconds) + return QDateTime(); // Would {und,ov}erflow return fromMSecsSinceEpoch(secs * 1000, spec, offsetSeconds); } @@ -4641,6 +4645,10 @@ QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone */ QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs, const QTimeZone &timeZone) { + constexpr qint64 maxSeconds = std::numeric_limits<qint64>::max() / 1000; + constexpr qint64 minSeconds = std::numeric_limits<qint64>::min() / 1000; + if (secs > maxSeconds || secs < minSeconds) + return QDateTime(); // Would {und,ov}erflow return fromMSecsSinceEpoch(secs * 1000, timeZone); } #endif |