summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2017-04-14 12:39:20 +0200
committerMarc Mutz <marc.mutz@kdab.com>2017-04-14 17:24:53 +0000
commit98170e4d7bc2fae438b649a8a5afc23705dd2593 (patch)
tree3ba5f69a6872712d77b8539e6eada6466e91fbc6
parent1936d0cf5bac6cd3bf62f3e8995cbfed98650155 (diff)
QMatrix4x4: fix aliasing problem in operator*=
When multiplying a QMatrix4x4 by itself, we were clobbering the very matrix we read from. Employ read-caching to avoid this aliasing problem. [ChangeLog][QtGui][QMatrix4x4] operator*=() now calculates the correct result even if the RHS and LHS are the same object. Change-Id: I8534d56cfdd62c336577125127f05173fcec2873 Reviewed-by: Sean Harmer <sean.harmer@kdab.com> (cherry picked from commit 5662234afaf23d88e1f3fa4bee2a59b61bd0c267)
-rw-r--r--src/gui/math3d/qmatrix4x4.h3
-rw-r--r--tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp4
2 files changed, 6 insertions, 1 deletions
diff --git a/src/gui/math3d/qmatrix4x4.h b/src/gui/math3d/qmatrix4x4.h
index cd5686be94..10de9ae3ec 100644
--- a/src/gui/math3d/qmatrix4x4.h
+++ b/src/gui/math3d/qmatrix4x4.h
@@ -413,8 +413,9 @@ inline QMatrix4x4& QMatrix4x4::operator-=(const QMatrix4x4& other)
return *this;
}
-inline QMatrix4x4& QMatrix4x4::operator*=(const QMatrix4x4& other)
+inline QMatrix4x4& QMatrix4x4::operator*=(const QMatrix4x4& o)
{
+ const QMatrix4x4 other = o; // prevent aliasing when &o == this ### Qt 6: take o by value
flagBits |= other.flagBits;
if (flagBits < Rotation2D) {
diff --git a/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp b/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp
index 463322ff2a..4c932a78f7 100644
--- a/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp
+++ b/tests/auto/gui/math3d/qmatrixnxn/tst_qmatrixnxn.cpp
@@ -1232,6 +1232,10 @@ void tst_QMatrixNxN::multiply4x4()
QMatrix4x4 m5;
m5 = m1 * m2;
QVERIFY(isSame(m5, (const float *)m3Values));
+
+ QMatrix4x4 m1xm1 = m1 * m1;
+ m1 *= m1;
+ QCOMPARE(m1, m1xm1);
}
// Test matrix multiplication for 4x3 matrices.