From 5e7e6788814549a1b60f9cc8972e03fb8a830d1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= Date: Wed, 18 Aug 2010 19:22:49 +0200 Subject: qUncompress: don't exit on allocation failure with -no-exceptions If we're not allowed to fail here with -no-exceptions, we shouldn't throw either so just check the return value from qRealloc and be done with it. Also changed the test case so it doesn't expect bad_alloc being thrown; the test data now gets properly initialized... Reviewed-by: Olivier Goffart Reviewed-by: Harald Fernengel --- src/corelib/tools/qbytearray.cpp | 12 +++++++---- tests/auto/qbytearray/tst_qbytearray.cpp | 36 +++++++++++++------------------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 8d38e4c..3062c4a 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -542,12 +542,14 @@ QByteArray qUncompress(const uchar* data, int nbytes) forever { ulong alloc = len; - d.reset(q_check_ptr(static_cast(qRealloc(d.take(), sizeof(QByteArray::Data) + alloc)))); - if (!d) { + QByteArray::Data *p = static_cast(qRealloc(d.data(), sizeof(QByteArray::Data) + alloc)); + if (!p) { // we are not allowed to crash here when compiling with QT_NO_EXCEPTIONS qWarning("qUncompress: could not allocate enough memory to uncompress data"); return QByteArray(); } + d.take(); // realloc was successful + d.reset(p); int res = ::uncompress((uchar*)d->array, &len, (uchar*)data+4, nbytes-4); @@ -555,12 +557,14 @@ QByteArray qUncompress(const uchar* data, int nbytes) switch (res) { case Z_OK: if (len != alloc) { - d.reset(q_check_ptr(static_cast(qRealloc(d.take(), sizeof(QByteArray::Data) + len)))); - if (!d) { + QByteArray::Data *p = static_cast(qRealloc(d.data(), sizeof(QByteArray::Data) + len)); + if (!p) { // we are not allowed to crash here when compiling with QT_NO_EXCEPTIONS qWarning("qUncompress: could not allocate enough memory to uncompress data"); return QByteArray(); } + d.take(); // realloc was successful + d.reset(p); } d->ref = 1; d->alloc = d->size = len; diff --git a/tests/auto/qbytearray/tst_qbytearray.cpp b/tests/auto/qbytearray/tst_qbytearray.cpp index e3341d1..0dc2282 100644 --- a/tests/auto/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/qbytearray/tst_qbytearray.cpp @@ -223,18 +223,18 @@ void tst_QByteArray::qUncompress_data() QTest::addColumn("in"); QTest::addColumn("out"); - QTest::newRow("0x00000000") << QByteArray("\x00\x00\x00\x00") << QByteArray(); - QTest::newRow("0x000000ff") << QByteArray("\x00\x00\x00\xff") << QByteArray(); - QTest::newRow("0x3f000000") << QByteArray("\x3f\x00\x00\x00") << QByteArray(); - QTest::newRow("0x3fffffff") << QByteArray("\x3f\xff\xff\xff") << QByteArray(); - QTest::newRow("0x7fffff00") << QByteArray("\x7f\xff\xff\x00") << QByteArray(); - QTest::newRow("0x7fffffff") << QByteArray("\x7f\xff\xff\xff") << QByteArray(); - QTest::newRow("0x80000000") << QByteArray("\x80\x00\x00\x00") << QByteArray(); - QTest::newRow("0x800000ff") << QByteArray("\x80\x00\x00\xff") << QByteArray(); - QTest::newRow("0xcf000000") << QByteArray("\xcf\x00\x00\x00") << QByteArray(); - QTest::newRow("0xcfffffff") << QByteArray("\xcf\xff\xff\xff") << QByteArray(); - QTest::newRow("0xffffff00") << QByteArray("\xff\xff\xff\x00") << QByteArray(); - QTest::newRow("0xffffffff") << QByteArray("\xff\xff\xff\xff") << QByteArray(); + QTest::newRow("0x00000000") << QByteArray("\x00\x00\x00\x00", 4) << QByteArray(); + QTest::newRow("0x000000ff") << QByteArray("\x00\x00\x00\xff", 4) << QByteArray(); + QTest::newRow("0x3f000000") << QByteArray("\x3f\x00\x00\x00", 4) << QByteArray(); + QTest::newRow("0x3fffffff") << QByteArray("\x3f\xff\xff\xff", 4) << QByteArray(); + QTest::newRow("0x7fffff00") << QByteArray("\x7f\xff\xff\x00", 4) << QByteArray(); + QTest::newRow("0x7fffffff") << QByteArray("\x7f\xff\xff\xff", 4) << QByteArray(); + QTest::newRow("0x80000000") << QByteArray("\x80\x00\x00\x00", 4) << QByteArray(); + QTest::newRow("0x800000ff") << QByteArray("\x80\x00\x00\xff", 4) << QByteArray(); + QTest::newRow("0xcf000000") << QByteArray("\xcf\x00\x00\x00", 4) << QByteArray(); + QTest::newRow("0xcfffffff") << QByteArray("\xcf\xff\xff\xff", 4) << QByteArray(); + QTest::newRow("0xffffff00") << QByteArray("\xff\xff\xff\x00", 4) << QByteArray(); + QTest::newRow("0xffffffff") << QByteArray("\xff\xff\xff\xff", 4) << QByteArray(); } void tst_QByteArray::qUncompress() @@ -251,18 +251,10 @@ void tst_QByteArray::qUncompress() #endif QByteArray res; - QT_TRY { - res = ::qUncompress(in); - } QT_CATCH(const std::bad_alloc &) { - res = QByteArray(); - } + res = ::qUncompress(in); QCOMPARE(res, out); - QT_TRY { - res = ::qUncompress(in + "blah"); - } QT_CATCH(const std::bad_alloc &) { - res = QByteArray(); - } + res = ::qUncompress(in + "blah"); QCOMPARE(res, out); } -- cgit v0.12