From 6ce9404a6e7ad6ba3ff37f6890fe400c643c3d52 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 1 Aug 2019 22:56:30 -0700 Subject: QBitArray: fix fromBits() and actually test it When I initially added it, it was ony for QCborValue, but I never added the tests. Turns out there were two bugs: [ChangeLog][QtCore][QBitArray] Fixed two bugs that caused QBitArrays created using fromBits() not to compare equal to the equivalent QBitArray created using other methods if the size was zero or not a multiple of 4. If the size modulus 8 was 5, 6, or 7, the data was actually incorrect. Fixes: QTBUG-77285 Change-Id: Ife213d861bb14c1787e1fffd15b70573d162042c Reviewed-by: Lars Knoll --- src/corelib/tools/qbitarray.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qbitarray.cpp b/src/corelib/tools/qbitarray.cpp index 4e8e3c241e..94aadf7fdf 100644 --- a/src/corelib/tools/qbitarray.cpp +++ b/src/corelib/tools/qbitarray.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. +** Copyright (C) 2019 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -132,12 +132,12 @@ QT_BEGIN_NAMESPACE * We overallocate the byte array by 1 byte. The first user bit is at * d.data()[1]. On the extra first byte, we store the difference between the * number of bits in the byte array (including this byte) and the number of - * bits in the bit array. Therefore, it's always a number between 8 and 15. + * bits in the bit array. Therefore, for a non-empty QBitArray, it's always a + * number between 8 and 15. For the empty one, d is the an empty QByteArray and + * *d.constData() is the QByteArray's terminating NUL (0) byte. * * This allows for fast calculation of the bit array size: * inline int size() const { return (d.size() << 3) - *d.constData(); } - * - * Note: for an array of zero size, *d.constData() is the QByteArray implicit NUL. */ /*! @@ -326,6 +326,8 @@ void QBitArray::fill(bool value, int begin, int end) QBitArray QBitArray::fromBits(const char *data, qsizetype size) { QBitArray result; + if (size == 0) + return result; qsizetype nbytes = (size + 7) / 8; result.d = QByteArray(nbytes + 1, Qt::Uninitialized); @@ -334,7 +336,7 @@ QBitArray QBitArray::fromBits(const char *data, qsizetype size) // clear any unused bits from the last byte if (size & 7) - bits[nbytes] &= 0xffU >> (size & 7); + bits[nbytes] &= 0xffU >> (8 - (size & 7)); *bits = result.d.size() * 8 - size; return result; -- cgit v1.2.3