QDataStream: add a new SizeLimitExceeded status code

This status is supposed to be used when the stream tries to read
or write more data than it is supported by the current platform.
For example, reading more than 2 GiB of data on a 32-bit platform will
result into this status, but it will work fine on a 64-bit platform.

This patch uses the new status in read operations.

Amends fd48ce0b73

Found in 6.7 API review

Pick-to: 6.7
Change-Id: I675b1ee25fafba174ce8f94c3470dbb7893d6d9e
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Ivan Solovev 2024-01-23 16:39:38 +01:00
parent 61130d73d4
commit 36cc9fc54a
5 changed files with 15 additions and 9 deletions

View File

@ -222,6 +222,11 @@ QT_BEGIN_NAMESPACE
data in the underlying device.
\value ReadCorruptData The data stream has read corrupt data.
\value WriteFailed The data stream cannot write to the underlying device.
\value [since 6.7] SizeLimitExceeded The data stream cannot read or write
the data because its size is larger than supported
by the current platform. This can happen, for
example, when trying to read more that 2 GiB of
data on a 32-bit platform.
*/
/*****************************************************************************
@ -1065,7 +1070,7 @@ QDataStream &QDataStream::readBytes(char *&s, qsizetype &l)
qsizetype len = qsizetype(length);
if (length != len || length < 0) {
setStatus(ReadCorruptData); // Cannot store len in l
setStatus(SizeLimitExceeded); // Cannot store len in l
return *this;
}

View File

@ -102,7 +102,8 @@ public:
Ok,
ReadPastEnd,
ReadCorruptData,
WriteFailed
WriteFailed,
SizeLimitExceeded,
};
enum FloatingPointPrecision {
@ -268,7 +269,7 @@ QDataStream &readArrayBasedContainer(QDataStream &s, Container &c)
qint64 size = QDataStream::readQSizeType(s);
qsizetype n = size;
if (size != n || size < 0) {
s.setStatus(QDataStream::ReadCorruptData);
s.setStatus(QDataStream::SizeLimitExceeded);
return s;
}
c.reserve(n);
@ -294,7 +295,7 @@ QDataStream &readListBasedContainer(QDataStream &s, Container &c)
qint64 size = QDataStream::readQSizeType(s);
qsizetype n = size;
if (size != n || size < 0) {
s.setStatus(QDataStream::ReadCorruptData);
s.setStatus(QDataStream::SizeLimitExceeded);
return s;
}
for (qsizetype i = 0; i < n; ++i) {
@ -319,7 +320,7 @@ QDataStream &readAssociativeContainer(QDataStream &s, Container &c)
qint64 size = QDataStream::readQSizeType(s);
qsizetype n = size;
if (size != n || size < 0) {
s.setStatus(QDataStream::ReadCorruptData);
s.setStatus(QDataStream::SizeLimitExceeded);
return s;
}
for (qsizetype i = 0; i < n; ++i) {

View File

@ -3303,7 +3303,7 @@ QDataStream &operator>>(QDataStream &in, QByteArray &ba)
qsizetype len = size;
if (size != len || size < -1) {
ba.clear();
in.setStatus(QDataStream::ReadCorruptData);
in.setStatus(QDataStream::SizeLimitExceeded);
return in;
}
if (len == -1) { // null byte-array

View File

@ -9366,7 +9366,7 @@ QDataStream &operator>>(QDataStream &in, QString &str)
qsizetype bytes = size;
if (size != bytes || size < -1) {
str.clear();
in.setStatus(QDataStream::ReadCorruptData);
in.setStatus(QDataStream::SizeLimitExceeded);
return in;
}
if (bytes == -1) { // null string

View File

@ -2904,10 +2904,10 @@ void tst_QDataStream::status_QString_data()
QTest::newRow("32 bit size MAX string no content") << QByteArray("\xff\xff\xff\xfc", 4) << (int) QDataStream::ReadPastEnd << QString();
#else
// too big for 32 bit platforms
QTest::newRow("32 bit size MAX string no content") << QByteArray("\xff\xff\xff\xfc", 4) << (int) QDataStream::ReadCorruptData << QString();
QTest::newRow("32 bit size MAX string no content") << QByteArray("\xff\xff\xff\xfc", 4) << (int) QDataStream::SizeLimitExceeded << QString();
#endif
// too big on both 32 and 64 bit platforms because qsizetype is signed
QTest::newRow("64 bit size MAX string no content") << QByteArray("\xff\xff\xff\xfe\xff\xff\xff\xff\xff\xff\xff\xfe", 12) << (int) QDataStream::ReadCorruptData << QString();
QTest::newRow("64 bit size MAX string no content") << QByteArray("\xff\xff\xff\xfe\xff\xff\xff\xff\xff\xff\xff\xfe", 12) << (int) QDataStream::SizeLimitExceeded << QString();
// corrupt data because QChar is 16 bit => even size required
QTest::newRow("corrupt1") << QByteArray("yyyy") << (int) QDataStream::ReadCorruptData << QString();