diff options
author | Erik Verbruggen <erik.verbruggen@digia.com> | 2015-07-27 10:59:39 +0200 |
---|---|---|
committer | Erik Verbruggen <erik.verbruggen@theqtcompany.com> | 2015-08-11 11:01:27 +0000 |
commit | d44ca1ed0b70a64113cce270d4dad45a1fef7235 (patch) | |
tree | 2b2462d83e31c714940be5266f0151c449d531d5 /tests/auto/gui | |
parent | 670cb2edbcdc13b33cebe45a682a6dc10f17b616 (diff) |
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 <allan.jensen@theqtcompany.com>
Diffstat (limited to 'tests/auto/gui')
-rw-r--r-- | tests/auto/gui/painting/qcolor/tst_qcolor.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
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. |