summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/text/qbytearray
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2022-09-17 11:32:45 +0200
committerMarc Mutz <marc.mutz@qt.io>2022-10-16 23:01:36 +0200
commitbda3628402d04ed6fc244616791e1170a0cb61d0 (patch)
treecad43fd0546def0375bd1beeb4189e1338fc47c8 /tests/auto/corelib/text/qbytearray
parente37c0c20bcccd77f631c1fd229b07cc5eae9a269 (diff)
Port qUncompress() to zstream/inflate()
The zlib convenience API we've been using so far has two problems: - On Windows-64, where sizeof(long) == 4, the use of ulong for sizes meant that we could not uncompress data compressed on other 64-bit platforms (Unix). While zstream also uses ulong, being a stream API, it allows feeding data in chunks. The total_in and total_out members are only required for gzip compression and are otherwise just informational. They're unsigned, so their overflow does not cause UB. In summary, using zstream + inflate() allows us to decompress more than 4GiB of data even on Windows-64. - On all platforms, if the size hint in the header was too short, we'd double the output buffer size and try again, from scratch. Using zstream + inflate(), we still need to reallocate, but we can then let zlib pick up where it left off when it ran out of output buffer space. In all but the most pathological cases, copying the already-decoded data instead of re-decoding it again should be faster, esp. if QArrayData uses realloc() instead of malloc() + free() to grow the buffer. We also now directly allocate at least as much output buffer as we have input, to cut the first few rounds of reallocations when the expectedSize was created, as qCompress still does, using modulo arithmetic mod 4GiB instead of saturation arithmethic. Factor the growing of the output buffer into a wrapper function, flate(), which can be reused when porting qCompress(). This completely fixes the uncompression side of QTBUG-106542 and QTBUG-104972. Pick-to: 6.4 6.3 6.2 Task-number: QTBUG-104972 Task-number: QTBUG-106542 Change-Id: I97f55ea322c24db1ac48b31c16855bc91708e7e2 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'tests/auto/corelib/text/qbytearray')
-rw-r--r--tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
index b138609448..72f66640cd 100644
--- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
+++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
@@ -12,6 +12,8 @@
#include "../shared/test_number_shared.h"
+#include <q20iterator.h>
+
#include <stdexcept>
#include <string_view>
@@ -31,6 +33,7 @@ private slots:
void qCompress();
void qUncompressCorruptedData_data();
void qUncompressCorruptedData();
+ void qUncompress4GiBPlus();
void qCompressionZeroTermination();
#endif
void constByteArray();
@@ -300,6 +303,66 @@ void tst_QByteArray::qUncompressCorruptedData()
QCOMPARE(res, QByteArray());
}
+void tst_QByteArray::qUncompress4GiBPlus()
+{
+ // after three rounds, this decompresses to 4GiB + 1 'X' bytes:
+ constexpr uchar compressed_3x[] = {
+ 0x00, 0x00, 0x1a, 0x76, 0x78, 0x9c, 0x63, 0xb0, 0xdf, 0xb4, 0xad, 0x62,
+ 0xce, 0xdb, 0x3b, 0x0b, 0xf3, 0x26, 0x27, 0x4a, 0xb4, 0x3d, 0x34, 0x5b,
+ 0xed, 0xb4, 0x41, 0xf1, 0xc0, 0x99, 0x2f, 0x02, 0x05, 0x67, 0x26, 0x88,
+ 0x6c, 0x66, 0x71, 0x34, 0x62, 0x9c, 0x75, 0x26, 0xb1, 0xa0, 0xe5, 0xcc,
+ 0xda, 0x94, 0x83, 0xc9, 0x05, 0x73, 0x0e, 0x3c, 0x39, 0xc2, 0xc7, 0xd0,
+ 0xae, 0x38, 0x53, 0x7b, 0x87, 0xdc, 0x01, 0x91, 0x45, 0x59, 0x4f, 0xda,
+ 0xbf, 0xca, 0xcc, 0x52, 0xdb, 0xbb, 0xde, 0xbb, 0xf6, 0xd3, 0x55, 0xff,
+ 0x7d, 0x77, 0x0e, 0x1b, 0xf0, 0xa4, 0xdf, 0xc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9, 0xbf, 0x35, 0x13, 0xca, 0x8c, 0x75, 0xec, 0xec, 0xa4, 0x2f,
+ 0x7e, 0x2d, 0xf9, 0xf3, 0xf0, 0xee, 0xea, 0xd5, 0xf5, 0xd3, 0x14, 0x57,
+ 0x06, 0x00, 0x00, 0xb9, 0x1e, 0x35, 0xce
+ };
+
+ constexpr qint64 GiB = 1024LL * 1024 * 1024;
+
+ if constexpr (sizeof(qsizetype) == sizeof(int)) {
+ QSKIP("This is a 64-bit-only test.");
+ } else {
+
+ // 1st
+ auto c = ::qUncompress(std::data(compressed_3x), q20::ssize(compressed_3x));
+ QVERIFY(!c.isNull()); // check for decompression error
+
+ // 2nd
+ c = ::qUncompress(c);
+ QVERIFY(!c.isNull());
+
+ // 3rd
+ try {
+ c = ::qUncompress(c);
+ if (c.isNull()) // this step (~18MiB -> 4GiB) might have run out of memory
+ QSKIP("Failed to allocate enough memory.");
+ } catch (const std::bad_alloc &) {
+ QSKIP("Failed to allocate enough memory.");
+ }
+
+ QCOMPARE(c.size(), 4 * GiB + 1);
+ QCOMPARE(std::string_view{c}.find_first_not_of('X'),
+ std::string_view::npos);
+ }
+}
+
void tst_QByteArray::qCompressionZeroTermination()
{
QByteArray s = "Hello, I'm a string.";