summaryrefslogtreecommitdiffstats
path: root/src/testlib
diff options
context:
space:
mode:
authorAndré Klitzing <aklitzing@gmail.com>2022-07-08 11:35:37 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2022-07-28 13:11:54 +0000
commitd1dd9442392f3a1ab90a081bc58a84444a7a64c6 (patch)
tree98b78b716e2bb888ef43c147cc701a49a2d4f14f /src/testlib
parent4b165c6bcc0e2d29dda230854766a5359a7e8db9 (diff)
Fix race condition with QTest::ignoreMessage
If another spawned thread will log at the same time a crash was possible because IgnoreResultList was not thread-safe. Task-number: QTBUG-104840 Pick-to: 6.4 6.3 6.2 5.15 Change-Id: I251c83bb28cb75e55d477706bf2524c061a6eab7 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/testlib')
-rw-r--r--src/testlib/qtestlog.cpp9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/testlib/qtestlog.cpp b/src/testlib/qtestlog.cpp
index a20082b630..f7b75d5502 100644
--- a/src/testlib/qtestlog.cpp
+++ b/src/testlib/qtestlog.cpp
@@ -24,6 +24,7 @@
#include <QtCore/qbytearray.h>
#include <QtCore/qelapsedtimer.h>
#include <QtCore/qlist.h>
+#include <QtCore/qmutex.h>
#include <QtCore/qvariant.h>
#if QT_CONFIG(regularexpression)
#include <QtCore/QRegularExpression>
@@ -138,6 +139,7 @@ namespace QTest {
};
static IgnoreResultList *ignoreResultList = nullptr;
+ Q_CONSTINIT static QBasicMutex mutex;
static std::vector<QVariant> failOnWarningList;
@@ -151,6 +153,8 @@ namespace QTest {
static bool handleIgnoredMessage(QtMsgType type, const QString &message)
{
+ const QMutexLocker mutexLocker(&QTest::mutex);
+
if (!ignoreResultList)
return false;
IgnoreResultList *last = nullptr;
@@ -270,6 +274,7 @@ void QTestLog::enterTestData(QTestData *data)
int QTestLog::unhandledIgnoreMessages()
{
+ const QMutexLocker mutexLocker(&QTest::mutex);
int i = 0;
QTest::IgnoreResultList *list = QTest::ignoreResultList;
while (list) {
@@ -290,6 +295,7 @@ void QTestLog::leaveTestFunction()
void QTestLog::printUnhandledIgnoreMessages()
{
+ const QMutexLocker mutexLocker(&QTest::mutex);
QString message;
QTest::IgnoreResultList *list = QTest::ignoreResultList;
while (list) {
@@ -310,6 +316,7 @@ void QTestLog::printUnhandledIgnoreMessages()
void QTestLog::clearIgnoreMessages()
{
+ const QMutexLocker mutexLocker(&QTest::mutex);
QTest::IgnoreResultList::clearList(QTest::ignoreResultList);
}
@@ -594,6 +601,7 @@ void QTestLog::ignoreMessage(QtMsgType type, const char *msg)
{
QTEST_ASSERT(msg);
+ const QMutexLocker mutexLocker(&QTest::mutex);
QTest::IgnoreResultList::append(QTest::ignoreResultList, type, QString::fromUtf8(msg));
}
@@ -602,6 +610,7 @@ void QTestLog::ignoreMessage(QtMsgType type, const QRegularExpression &expressio
{
QTEST_ASSERT(expression.isValid());
+ const QMutexLocker mutexLocker(&QTest::mutex);
QTest::IgnoreResultList::append(QTest::ignoreResultList, type, QVariant(expression));
}
#endif // QT_CONFIG(regularexpression)