summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2012-09-25 14:30:48 (GMT)
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-05-07 17:26:45 (GMT)
commit9dba7627557364c3498809e775ab79bc6f0c3158 (patch)
tree640a093e29eb61824543dd9e82b3ae1765a00d5a
parentbb00ac8d1251be3e703cc09e5fb2f100f24b398b (diff)
downloadQt-9dba7627557364c3498809e775ab79bc6f0c3158.zip
Qt-9dba7627557364c3498809e775ab79bc6f0c3158.tar.gz
Qt-9dba7627557364c3498809e775ab79bc6f0c3158.tar.bz2
Make QContiguousCache with zero capacity not crash
These containers don't make sense and will just result in no action being taken (all items added will simply be discarded), but it shouldn't crash due to a division by zero. Task-number: QTBUG-27339 Change-Id: Ib9acf5c0a9a826e6853e7beaf5e56511fde98dc6 (cherry-picked from qt5 commit 320c4e31e124f99601399d00935362b587c77510) Reviewed-by: Robin Burchell <robin+qt@viroteck.net>
-rw-r--r--src/corelib/tools/qcontiguouscache.h6
-rw-r--r--tests/auto/qcontiguouscache/tst_qcontiguouscache.cpp21
2 files changed, 27 insertions, 0 deletions
diff --git a/src/corelib/tools/qcontiguouscache.h b/src/corelib/tools/qcontiguouscache.h
index 70a19cc..55ebd5e 100644
--- a/src/corelib/tools/qcontiguouscache.h
+++ b/src/corelib/tools/qcontiguouscache.h
@@ -346,6 +346,8 @@ void QContiguousCache<T>::free(Data *x)
template <typename T>
void QContiguousCache<T>::append(const T &value)
{
+ if (!d->alloc)
+ return; // zero capacity
detach();
if (QTypeInfo<T>::isComplex) {
if (d->count == d->alloc)
@@ -367,6 +369,8 @@ void QContiguousCache<T>::append(const T &value)
template<typename T>
void QContiguousCache<T>::prepend(const T &value)
{
+ if (!d->alloc)
+ return; // zero capacity
detach();
if (d->start)
d->start--;
@@ -390,6 +394,8 @@ template<typename T>
void QContiguousCache<T>::insert(int pos, const T &value)
{
Q_ASSERT_X(pos >= 0 && pos < INT_MAX, "QContiguousCache<T>::insert", "index out of range");
+ if (!d->alloc)
+ return; // zero capacity
detach();
if (containsIndex(pos)) {
if (QTypeInfo<T>::isComplex) {
diff --git a/tests/auto/qcontiguouscache/tst_qcontiguouscache.cpp b/tests/auto/qcontiguouscache/tst_qcontiguouscache.cpp
index 437f140..a538a13 100644
--- a/tests/auto/qcontiguouscache/tst_qcontiguouscache.cpp
+++ b/tests/auto/qcontiguouscache/tst_qcontiguouscache.cpp
@@ -70,6 +70,7 @@ private slots:
void setCapacity();
void zeroCapacity();
+ void modifyZeroCapacityCache();
};
QTEST_MAIN(tst_QContiguousCache)
@@ -368,4 +369,24 @@ void tst_QContiguousCache::zeroCapacity()
QCOMPARE(contiguousCache.capacity(),0);
}
+void tst_QContiguousCache::modifyZeroCapacityCache()
+{
+ {
+ QContiguousCache<int> contiguousCache;
+ contiguousCache.insert(0, 42);
+ }
+ {
+ QContiguousCache<int> contiguousCache;
+ contiguousCache.insert(1, 42);
+ }
+ {
+ QContiguousCache<int> contiguousCache;
+ contiguousCache.append(42);
+ }
+ {
+ QContiguousCache<int> contiguousCache;
+ contiguousCache.prepend(42);
+ }
+}
+
#include "tst_qcontiguouscache.moc"