Add test for GCC bug #43247
authorJoão Abecasis <joao.abecasis@nokia.com>
Thu, 1 Dec 2011 11:54:50 +0000 (12:54 +0100)
committerQt by Nokia <qt-info@nokia.com>
Sun, 11 Dec 2011 11:07:21 +0000 (12:07 +0100)
A bug has been reported against GCC 4.4.3 (present in other version as
well), where the use of an array of size 1 to implement dynamic arrays
(such as QVector) leads to incorrect results in optimized builds as the
compiler assumes the index to be 0.

This test tries to ensure QArrayDataHeader is not affected by this bug,
as QVector currently is.

Change-Id: Id701496bae4d74170de43399c1062da40eb078e7

Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp

index 8bbef9b..f7a2fa7 100644 (file)
@@ -59,6 +59,7 @@ private slots:
     void alignment_data();
     void alignment();
     void typedData();
+    void gccBug43247();
 };
 
 void tst_QArrayData::referenceCounting()
@@ -522,5 +523,32 @@ void tst_QArrayData::typedData()
     }
 }
 
+void tst_QArrayData::gccBug43247()
+{
+    // This test tries to verify QArrayData is not affected by GCC optimizer
+    // bug #43247.
+    // Reported on GCC 4.4.3, Linux, affects QVector
+
+    QTest::ignoreMessage(QtDebugMsg, "GCC Optimization bug #43247 not triggered (3)");
+    QTest::ignoreMessage(QtDebugMsg, "GCC Optimization bug #43247 not triggered (4)");
+    QTest::ignoreMessage(QtDebugMsg, "GCC Optimization bug #43247 not triggered (5)");
+    QTest::ignoreMessage(QtDebugMsg, "GCC Optimization bug #43247 not triggered (6)");
+    QTest::ignoreMessage(QtDebugMsg, "GCC Optimization bug #43247 not triggered (7)");
+
+    SimpleVector<int> array(10, 0);
+    // QVector<int> vector(10, 0);
+
+    for (int i = 0; i < 10; ++i) {
+        if (i >= 3 && i < 8)
+            qDebug("GCC Optimization bug #43247 not triggered (%i)", i);
+
+        // When access to data is implemented through an array of size 1, this
+        // line lets the compiler assume i == 0, and the conditional above is
+        // skipped.
+        QVERIFY(array.at(i) == 0);
+        // QVERIFY(vector.at(i) == 0);
+    }
+}
+
 QTEST_APPLESS_MAIN(tst_QArrayData)
 #include "tst_qarraydata.moc"