summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@theqtcompany.com>2015-06-03 15:57:42 +0200
committerUlf Hermann <ulf.hermann@theqtcompany.com>2015-06-16 14:30:45 +0000
commitef0f7f424872a27c8397f854eecff753428bcddc (patch)
tree72cc245da16bc233f7586a312c3ea2890f0ffd6c /src/corelib
parent8d5516b58541b2ad63497437271f0a4e36ed311d (diff)
Unify QByteArray::MaxSize and MaxAllocSize
We have established the maximum size qAllocMore can deal with in commit 880986be2357a1f80827d038d770dc2f80300201 and we should use it. The maximum size for byte arrays is reduced by one byte as with the previous code we could make qAllocMore produce ((1 << 31) - extra) by passing (1 << 30). That is not a problem for qAllocMore itself (as long as extra > 0) but it's hard to verify that no related code casts the total sum back to signed int, which would overflow to -1. To make the compiler inline access to the maximum size, a private enum MaxByteArraySize is provided, which can be used in internal code. This fixes the merge of commits 880986be2357a1f80827d038d770dc2f80300201 and c70658d301e274c3aaa1fb6cebe2a5e56db12779 Change-Id: Idb04856f7c2e53ef383063e7555d3083020ff2b7 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/io/qiodevice.cpp17
-rw-r--r--src/corelib/tools/qbytearray.cpp17
-rw-r--r--src/corelib/tools/qbytearray.h3
-rw-r--r--src/corelib/tools/qbytearray_p.h61
-rw-r--r--src/corelib/tools/qringbuffer.cpp13
-rw-r--r--src/corelib/tools/tools.pri1
6 files changed, 82 insertions, 30 deletions
diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp
index 07a2ff8f6b..3c8e97e3c6 100644
--- a/src/corelib/io/qiodevice.cpp
+++ b/src/corelib/io/qiodevice.cpp
@@ -39,6 +39,7 @@
#include "qfile.h"
#include "qstringlist.h"
#include "qdir.h"
+#include "private/qbytearray_p.h"
#include <algorithm>
@@ -941,9 +942,9 @@ QByteArray QIODevice::read(qint64 maxSize)
Q_UNUSED(d);
#endif
- if (quint64(maxSize) >= QByteArray::MaxSize) {
+ if (maxSize >= MaxByteArraySize) {
checkWarnMessage(this, "read", "maxSize argument exceeds QByteArray size limit");
- maxSize = QByteArray::MaxSize - 1;
+ maxSize = MaxByteArraySize - 1;
}
qint64 readBytes = 0;
@@ -995,7 +996,7 @@ QByteArray QIODevice::readAll()
// flush internal read buffer
if (!(d->openMode & Text) && !d->buffer.isEmpty()) {
- if (quint64(d->buffer.size()) >= QByteArray::MaxSize)
+ if (d->buffer.size() >= MaxByteArraySize)
return QByteArray();
result = d->buffer.readAll();
readBytes = result.size();
@@ -1008,7 +1009,7 @@ QByteArray QIODevice::readAll()
// Size is unknown, read incrementally.
qint64 readResult;
do {
- if (quint64(readBytes) + QIODEVICE_BUFFERSIZE >= QByteArray::MaxSize) {
+ if (readBytes + QIODEVICE_BUFFERSIZE >= MaxByteArraySize) {
// If resize would fail, don't read more, return what we have.
break;
}
@@ -1020,7 +1021,7 @@ QByteArray QIODevice::readAll()
} else {
// Read it all in one go.
// If resize fails, don't read anything.
- if (quint64(readBytes + theSize - d->pos) >= QByteArray::MaxSize)
+ if (readBytes + theSize - d->pos >= MaxByteArraySize)
return QByteArray();
result.resize(int(readBytes + theSize - d->pos));
readBytes += read(result.data() + readBytes, result.size() - readBytes);
@@ -1178,9 +1179,9 @@ QByteArray QIODevice::readLine(qint64 maxSize)
Q_UNUSED(d);
#endif
- if (quint64(maxSize) >= QByteArray::MaxSize) {
+ if (maxSize >= MaxByteArraySize) {
qWarning("QIODevice::read: maxSize argument exceeds QByteArray size limit");
- maxSize = QByteArray::MaxSize - 1;
+ maxSize = MaxByteArraySize - 1;
}
result.resize(int(maxSize));
@@ -1188,7 +1189,7 @@ QByteArray QIODevice::readLine(qint64 maxSize)
if (!result.size()) {
// If resize fails or maxSize == 0, read incrementally
if (maxSize == 0)
- maxSize = QByteArray::MaxSize - 1;
+ maxSize = MaxByteArraySize - 1;
// The first iteration needs to leave an extra byte for the terminating null
result.resize(1);
diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp
index 36c1f42995..0ed701f4fa 100644
--- a/src/corelib/tools/qbytearray.cpp
+++ b/src/corelib/tools/qbytearray.cpp
@@ -41,6 +41,7 @@
#include "qlocale_p.h"
#include "qstringalgorithms_p.h"
#include "qscopedpointer.h"
+#include "qbytearray_p.h"
#include <qdatastream.h>
#include <qmath.h>
@@ -123,8 +124,8 @@ int qFindByteArray(
int qAllocMore(int alloc, int extra) Q_DECL_NOTHROW
{
- Q_ASSERT(alloc >= 0 && extra >= 0);
- Q_ASSERT_X(uint(alloc) <= QByteArray::MaxSize, "qAllocMore", "Requested size is too large!");
+ Q_ASSERT(alloc >= 0 && extra >= 0 && extra <= MaxAllocSize);
+ Q_ASSERT_X(alloc <= MaxAllocSize - extra, "qAllocMore", "Requested size is too large!");
unsigned nalloc = qNextPowerOfTwo(alloc + extra);
@@ -838,16 +839,6 @@ static inline char qToLower(char c)
*/
/*!
- \variable QByteArray::MaxSize
- \internal
- \since 5.4
-
- The maximum size of a QByteArray (including a '\0' terminator), in bytes.
- Also applies to the maximum storage size of QString and QVector, though
- not the number of elements.
-*/
-
-/*!
\enum QByteArray::Base64Option
\since 5.2
@@ -1575,7 +1566,7 @@ void QByteArray::reallocData(uint alloc, Data::AllocationOptions options)
d = x;
} else {
if (options & Data::Grow) {
- if (alloc > uint(MaxAllocSize) - uint(sizeof(Data)))
+ if (alloc > MaxByteArraySize)
qBadAlloc();
alloc = qAllocMore(alloc, sizeof(Data));
}
diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h
index 6d14cd5131..38f277533b 100644
--- a/src/corelib/tools/qbytearray.h
+++ b/src/corelib/tools/qbytearray.h
@@ -162,9 +162,6 @@ private:
typedef QTypedArrayData<char> Data;
public:
- // undocumented:
- static const quint64 MaxSize = (1 << 30) - sizeof(Data);
-
enum Base64Option {
Base64Encoding = 0,
Base64UrlEncoding = 1,
diff --git a/src/corelib/tools/qbytearray_p.h b/src/corelib/tools/qbytearray_p.h
new file mode 100644
index 0000000000..78c667aa90
--- /dev/null
+++ b/src/corelib/tools/qbytearray_p.h
@@ -0,0 +1,61 @@
+/****************************************************************************
+**
+** Copyright (C) 2015 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL21$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** As a special exception, The Qt Company gives you certain additional
+** rights. These rights are described in The Qt Company LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QBYTEARRAY_P_H
+#define QBYTEARRAY_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of other Qt classes. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <QtCore/qbytearray.h>
+#include <QtCore/qtypetraits.h>
+#include "qtools_p.h"
+
+QT_BEGIN_NAMESPACE
+
+enum {
+ // Define as enum to force inlining. Don't expose MaxAllocSize in a public header.
+ MaxByteArraySize = MaxAllocSize - sizeof(QtPrivate::remove_pointer<QByteArray::DataPtr>::type)
+};
+
+QT_END_NAMESPACE
+
+#endif // QBYTEARRAY_P_H
diff --git a/src/corelib/tools/qringbuffer.cpp b/src/corelib/tools/qringbuffer.cpp
index bcf6d2646e..658267a521 100644
--- a/src/corelib/tools/qringbuffer.cpp
+++ b/src/corelib/tools/qringbuffer.cpp
@@ -33,6 +33,7 @@
****************************************************************************/
#include "private/qringbuffer_p.h"
+#include "private/qbytearray_p.h"
#include <string.h>
QT_BEGIN_NAMESPACE
@@ -79,7 +80,7 @@ void QRingBuffer::free(qint64 bytes)
clear(); // try to minify/squeeze us
}
} else {
- Q_ASSERT(quint64(bytes) < QByteArray::MaxSize);
+ Q_ASSERT(bytes < MaxByteArraySize);
head += int(bytes);
bufferSize -= bytes;
}
@@ -96,14 +97,14 @@ void QRingBuffer::free(qint64 bytes)
char *QRingBuffer::reserve(qint64 bytes)
{
- if (bytes <= 0 || quint64(bytes) >= QByteArray::MaxSize)
+ if (bytes <= 0 || bytes >= MaxByteArraySize)
return 0;
const qint64 newSize = bytes + tail;
// if need buffer reallocation
if (newSize > buffers.last().size()) {
if (newSize > buffers.last().capacity() && (tail >= basicBlockSize
- || quint64(newSize) >= QByteArray::MaxSize)) {
+ || newSize >= MaxByteArraySize)) {
// shrink this buffer to its current size
buffers.last().resize(tail);
@@ -117,7 +118,7 @@ char *QRingBuffer::reserve(qint64 bytes)
char *writePtr = buffers.last().data() + tail;
bufferSize += bytes;
- Q_ASSERT(quint64(bytes) < QByteArray::MaxSize);
+ Q_ASSERT(bytes < MaxByteArraySize);
tail += int(bytes);
return writePtr;
}
@@ -129,7 +130,7 @@ char *QRingBuffer::reserve(qint64 bytes)
*/
char *QRingBuffer::reserveFront(qint64 bytes)
{
- if (bytes <= 0 || quint64(bytes) >= QByteArray::MaxSize)
+ if (bytes <= 0 || bytes >= MaxByteArraySize)
return 0;
if (head < bytes) {
@@ -163,7 +164,7 @@ void QRingBuffer::chop(qint64 bytes)
clear(); // try to minify/squeeze us
}
} else {
- Q_ASSERT(quint64(bytes) < QByteArray::MaxSize);
+ Q_ASSERT(bytes < MaxByteArraySize);
tail -= int(bytes);
bufferSize -= bytes;
}
diff --git a/src/corelib/tools/tools.pri b/src/corelib/tools/tools.pri
index 1dba6784b6..60b53a3d8a 100644
--- a/src/corelib/tools/tools.pri
+++ b/src/corelib/tools/tools.pri
@@ -9,6 +9,7 @@ HEADERS += \
tools/qarraydatapointer.h \
tools/qbitarray.h \
tools/qbytearray.h \
+ tools/qbytearray_p.h \
tools/qbytearraylist.h \
tools/qbytearraymatcher.h \
tools/qbytedata_p.h \