diff options
author | makuukka <qt-info@nokia.com> | 2009-05-29 07:08:27 (GMT) |
---|---|---|
committer | Jason Barron <jbarron@trolltech.com> | 2009-06-10 06:38:13 (GMT) |
commit | b97671a3b40623c1499c13c3d74c896ac6b9406f (patch) | |
tree | 8f68eb88a7449799ad245f484b6c358935c85805 | |
parent | dcaa6ab545e7532b5455734bd8d234cb76b152fa (diff) | |
download | Qt-b97671a3b40623c1499c13c3d74c896ac6b9406f.zip Qt-b97671a3b40623c1499c13c3d74c896ac6b9406f.tar.gz Qt-b97671a3b40623c1499c13c3d74c896ac6b9406f.tar.bz2 |
Force qRound64() to take a double on platforms where qreal is a float.
In Symbian qreal is defined as float. And in qround64 the parameter
was qreal. This qround64 is used (only) in qvariant::convert, when
converting from double to e.g. int. This caused overflow bug for some
(close to max int) values, because in convert chain double value
was casted to float.
Task-number: 250267
Reviewed-By: Samuel Rødal
Reviewed-By: Jason Barron
-rw-r--r-- | src/corelib/global/qglobal.h | 5 | ||||
-rw-r--r-- | tests/auto/qvariant/tst_qvariant.cpp | 26 |
2 files changed, 31 insertions, 0 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 27aaac1..b075db6 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -1055,8 +1055,13 @@ inline T qAbs(const T &t) { return t >= 0 ? t : -t; } inline int qRound(qreal d) { return d >= 0.0 ? int(d + 0.5) : int(d - int(d-1) + 0.5) + int(d-1); } +#if defined(QT_NO_FPU) || defined(QT_ARCH_ARM) || defined(QT_ARCH_WINDOWSCE) || defined(QT_ARCH_SYMBIAN) +inline qint64 qRound64(double d) +{ return d >= 0.0 ? qint64(d + 0.5) : qint64(d - qint64(d-1) + 0.5) + qint64(d-1); } +#else inline qint64 qRound64(qreal d) { return d >= 0.0 ? qint64(d + 0.5) : qint64(d - qint64(d-1) + 0.5) + qint64(d-1); } +#endif template <typename T> inline const T &qMin(const T &a, const T &b) { if (a < b) return a; return b; } diff --git a/tests/auto/qvariant/tst_qvariant.cpp b/tests/auto/qvariant/tst_qvariant.cpp index d15f9c8..9c9ff33 100644 --- a/tests/auto/qvariant/tst_qvariant.cpp +++ b/tests/auto/qvariant/tst_qvariant.cpp @@ -255,6 +255,7 @@ private slots: void convertByteArrayToBool() const; void convertByteArrayToBool_data() const; void toIntFromQString() const; + void toIntFromDouble() const; }; Q_DECLARE_METATYPE(QDate) @@ -2909,5 +2910,30 @@ void tst_QVariant::toIntFromQString() const QVERIFY(ok); } +/*! + We verify that: + 1. Conversion from (64 bit) double to int works (no overflow). + 2. Same conversion works for QVariant::convert. + + Rationale: if 2147483630 is set in float and then converted to int, + there will be overflow and the result will be -2147483648. + + See task 250267. + */ +void tst_QVariant::toIntFromDouble() const +{ + double d = 2147483630; // max int 2147483647 + QVERIFY((int)d == 2147483630); + + QVariant var(d); + QVERIFY( var.canConvert( QVariant::Int ) ); + + bool ok; + int result = var.toInt(&ok); + + QVERIFY( ok == true ); + QCOMPARE(result, 2147483630); +} + QTEST_MAIN(tst_QVariant) #include "tst_qvariant.moc" |