summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qresultstore.h
diff options
context:
space:
mode:
authorAndrei Golubev <andrei.golubev@qt.io>2020-10-09 11:01:16 +0200
committerAndrei Golubev <andrei.golubev@qt.io>2020-10-13 17:04:16 +0200
commitba511b2fa4782d6618a5261bbbd50f0c57266a3a (patch)
tree62dd28d3ce8a8fd540e50726b648c989be818424 /src/corelib/thread/qresultstore.h
parent1ae15edd7e7ec4bd96d3a9a4d6b5793c7f7e8830 (diff)
Reject overwrites by the same index in QPromise::addResult()
One can call addResult(value, index) twice and consequently set the value twice by the same index. This seems rather strange and probably should not be allowed. This commit rejects setting results when there's already a valid result by that index. Consequently, this fixes memory leaks caused by N-times-called addResult(..., index) Fixes: QTBUG-86828 Change-Id: I77494f2cb73ce727ffad721cfcdcaa420899eb25 Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'src/corelib/thread/qresultstore.h')
-rw-r--r--src/corelib/thread/qresultstore.h23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/corelib/thread/qresultstore.h b/src/corelib/thread/qresultstore.h
index 5f8c7f150a..0a1382fd79 100644
--- a/src/corelib/thread/qresultstore.h
+++ b/src/corelib/thread/qresultstore.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -87,6 +87,8 @@ public:
bool operator!=(const ResultIteratorBase &other) const;
bool isVector() const;
bool canIncrementVectorIndex() const;
+ bool isValid() const;
+
protected:
QMap<int, ResultItem>::const_iterator mapIterator;
int m_vectorIndex;
@@ -139,6 +141,7 @@ public:
protected:
int insertResultItem(int index, ResultItem &resultItem);
void insertResultItemIfValid(int index, ResultItem &resultItem);
+ bool containsValidResultItem(int index) const;
void syncPendingResults();
void syncResultCount();
int updateInsertIndex(int index, int _count);
@@ -169,6 +172,9 @@ public:
template <typename T>
int addResult(int index, const T *result)
{
+ if (containsValidResultItem(index)) // reject if already present
+ return -1;
+
if (result == nullptr)
return addResult(index, static_cast<void *>(nullptr));
@@ -178,18 +184,27 @@ public:
template <typename T>
int moveResult(int index, T &&result)
{
+ if (containsValidResultItem(index)) // reject if already present
+ return -1;
+
return addResult(index, static_cast<void *>(new T(std::move_if_noexcept(result))));
}
template<typename T>
int addResults(int index, const QList<T> *results)
{
+ if (containsValidResultItem(index)) // reject if already present
+ return -1;
+
return addResults(index, new QList<T>(*results), results->count(), results->count());
}
template<typename T>
int addResults(int index, const QList<T> *results, int totalCount)
{
+ if (containsValidResultItem(index)) // reject if already present
+ return -1;
+
if (m_filterMode == true && results->count() != totalCount && 0 == results->count())
return addResults(index, nullptr, 0, totalCount);
@@ -198,12 +213,18 @@ public:
int addCanceledResult(int index)
{
+ if (containsValidResultItem(index)) // reject if already present
+ return -1;
+
return addResult(index, static_cast<void *>(nullptr));
}
template <typename T>
int addCanceledResults(int index, int _count)
{
+ if (containsValidResultItem(index)) // reject if already present
+ return -1;
+
QList<T> empty;
return addResults(index, &empty, _count);
}