From 5e3723e7ff00636a6c4be4dc4550b7f2639f2f8d Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 27 Jan 2024 10:32:20 +0100 Subject: [PATCH] QBitArray: DRY operator<<(QDataStream&) Drag the common parts out of the if-else. This also means that qsizetype size() is no longer truncated in the assignment to quint32 len, but that doesn't fix the problem that we create invalid Qt 5 streams when the QBitArray has more than INT_MAX bits in it. That is for a follow-up patch (which is why the {} stay for now). As a drive-by, replace verbose constData() with data() (the QByteArray is already const). Pick-to: 6.6 6.5 6.2 Change-Id: Iabf816e16f823bb3959469a635a21339242f36fc Reviewed-by: Ivan Solovev Reviewed-by: Thiago Macieira Reviewed-by: Allan Sandfeld Jensen (cherry picked from commit 741617aea0e4c631b2fd4b822f0948ec067f1cdc) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/tools/qbitarray.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/corelib/tools/qbitarray.cpp b/src/corelib/tools/qbitarray.cpp index ef30b97a6cb..249bd11db19 100644 --- a/src/corelib/tools/qbitarray.cpp +++ b/src/corelib/tools/qbitarray.cpp @@ -894,19 +894,15 @@ QBitArray operator^(const QBitArray &a1, const QBitArray &a2) QDataStream &operator<<(QDataStream &out, const QBitArray &ba) { + const qsizetype len = ba.size(); if (out.version() < QDataStream::Qt_6_0) { - quint32 len = ba.size(); - out << len; - if (len > 0) - out.writeRawData(ba.d.constData() + 1, ba.d.size() - 1); - return out; + out << quint32(len); } else { - quint64 len = ba.size(); - out << len; - if (len > 0) - out.writeRawData(ba.d.constData() + 1, ba.d.size() - 1); - return out; + out << quint64(len); } + if (len > 0) + out.writeRawData(ba.d.data() + 1, ba.d.size() - 1); + return out; } /*!