Avoid overflow in QTime::addSecs with too big a number of seconds
authorThiago Macieira <thiago.macieira@intel.com>
Tue, 11 Aug 2015 16:59:10 +0000 (09:59 -0700)
committerThiago Macieira <thiago.macieira@intel.com>
Wed, 19 Aug 2015 22:13:40 +0000 (22:13 +0000)
QDateTime::addSecs needs to do something similar, but not identical
because it needs the number of days too. And then there are daylight
savings transitions...

Task-number: QTBUG-47717
Change-Id: I7de033f80b0e4431b7f1ffff13f976f4f5e5a059
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
src/corelib/tools/qdatetime.cpp
tests/auto/corelib/tools/qtime/tst_qtime.cpp

index e445055e1dd47e09f28fb6c776fcd5f837a72dde..eb4eff32b46988e119f77996ee3dbfeb6b61bd7b 100644 (file)
@@ -1681,6 +1681,7 @@ bool QTime::setHMS(int h, int m, int s, int ms)
 
 QTime QTime::addSecs(int s) const
 {
+    s %= SECS_PER_DAY;
     return addMSecs(s * 1000);
 }
 
index 9a9101b7e90c758b5dfcae9172d83450286d90bb..213b817c3d9775419990aa8ecf321a54138b13b3 100644 (file)
@@ -84,6 +84,8 @@ void tst_QTime::addSecs_data()
 
     QTest::newRow("Data0") << QTime(0,0,0) << 200 << QTime(0,3,20);
     QTest::newRow("Data1") << QTime(0,0,0) << 20 << QTime(0,0,20);
+    QTest::newRow("overflow") << QTime(0,0,0) << (INT_MAX / 1000 + 1)
+                              << QTime(0,0,0).addSecs((INT_MAX / 1000 + 1) % 86400);
 }
 
 void tst_QTime::addSecs()