From d1dd9442392f3a1ab90a081bc58a84444a7a64c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Klitzing?= Date: Fri, 8 Jul 2022 11:35:37 +0200 Subject: 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 --- src/testlib/qtestlog.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/testlib') 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 #include #include +#include #include #if QT_CONFIG(regularexpression) #include @@ -138,6 +139,7 @@ namespace QTest { }; static IgnoreResultList *ignoreResultList = nullptr; + Q_CONSTINIT static QBasicMutex mutex; static std::vector 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) -- cgit v1.2.3