From d44ca1ed0b70a64113cce270d4dad45a1fef7235 Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Mon, 27 Jul 2015 10:59:39 +0200 Subject: Remove type punning from QRgba64. In C++, type punning with a union is not allowed. It will result in compiler defined behavior at best, and undefined behavior at worst. Specifically, if QRgba64 is passed to a function by value, the different members of a struct might be passed in separate registers. This means that any write to the quint64 might not blank out the values of the struct whenever the compiler looses track with TBAA. Change-Id: I991b5492fe4bb13a14bb670fef5bf13dacbe6c0a Reviewed-by: Allan Sandfeld Jensen --- tests/auto/gui/painting/qcolor/tst_qcolor.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'tests') diff --git a/tests/auto/gui/painting/qcolor/tst_qcolor.cpp b/tests/auto/gui/painting/qcolor/tst_qcolor.cpp index b9d17bae62..b81a4e2c4c 100644 --- a/tests/auto/gui/painting/qcolor/tst_qcolor.cpp +++ b/tests/auto/gui/painting/qcolor/tst_qcolor.cpp @@ -107,6 +107,7 @@ private slots: void premultiply(); void unpremultiply_sse4(); void qrgba64(); + void qrgba64MemoryLayout(); void qrgba64Premultiply(); void qrgba64Equivalence(); @@ -1495,6 +1496,24 @@ void tst_QColor::qrgba64() QCOMPARE(rgb64.green(), quint16(0x4422)); } +void tst_QColor::qrgba64MemoryLayout() +{ + QRgba64 rgb64 = QRgba64::fromRgba64(0x0123, 0x4567, 0x89ab, 0xcdef); + QCOMPARE(rgb64.red(), quint16(0x0123)); + QCOMPARE(rgb64.green(), quint16(0x4567)); + QCOMPARE(rgb64.blue(), quint16(0x89ab)); + QCOMPARE(rgb64.alpha(), quint16(0xcdef)); + + // Check in-memory order, so it can be used by things like SSE + Q_STATIC_ASSERT(sizeof(QRgba64) == sizeof(quint64)); + quint16 memory[4]; + memcpy(memory, &rgb64, sizeof(QRgba64)); + QCOMPARE(memory[0], quint16(0x0123)); + QCOMPARE(memory[1], quint16(0x4567)); + QCOMPARE(memory[2], quint16(0x89ab)); + QCOMPARE(memory[3], quint16(0xcdef)); +} + void tst_QColor::qrgba64Premultiply() { // Tests that qPremultiply(qUnpremultiply(rgba64)) returns rgba64. -- cgit v1.2.3