From 4d480f7c3bb53a1d2173d490a85d88dc5ea7cd1d Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Wed, 9 Sep 2020 19:19:42 +0200 Subject: [PATCH] Fix a potential crash in logging handlers --- libnymea-core/debugserverhandler.cpp | 2 ++ libnymea-core/debugserverhandler.h | 2 ++ libnymea-core/scriptengine/scriptengine.cpp | 2 ++ libnymea-core/scriptengine/scriptengine.h | 2 ++ libnymea/loggingcategories.cpp | 3 +++ 5 files changed, 11 insertions(+) diff --git a/libnymea-core/debugserverhandler.cpp b/libnymea-core/debugserverhandler.cpp index a402ec2d..b9e485cd 100644 --- a/libnymea-core/debugserverhandler.cpp +++ b/libnymea-core/debugserverhandler.cpp @@ -53,6 +53,7 @@ namespace nymeaserver { QList DebugServerHandler::s_websocketClients; +QMutex DebugServerHandler::s_loggingMutex; DebugServerHandler::DebugServerHandler(QObject *parent) : QObject(parent) @@ -572,6 +573,7 @@ void DebugServerHandler::logMessageHandler(QtMsgType type, const QMessageLogCont break; } + QMutexLocker locker(&s_loggingMutex); foreach (QWebSocket *client, s_websocketClients) { client->sendTextMessage(finalMessage); } diff --git a/libnymea-core/debugserverhandler.h b/libnymea-core/debugserverhandler.h index 43a4bd0d..5e29c81c 100644 --- a/libnymea-core/debugserverhandler.h +++ b/libnymea-core/debugserverhandler.h @@ -36,6 +36,7 @@ #include #include #include +#include #include "debugreportgenerator.h" #include "servers/httpreply.h" @@ -53,6 +54,7 @@ public: private: static QList s_websocketClients; static void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message); + static QMutex s_loggingMutex; QWebSocketServer *m_websocketServer = nullptr; diff --git a/libnymea-core/scriptengine/scriptengine.cpp b/libnymea-core/scriptengine/scriptengine.cpp index d74d9b3a..3d948797 100644 --- a/libnymea-core/scriptengine/scriptengine.cpp +++ b/libnymea-core/scriptengine/scriptengine.cpp @@ -53,6 +53,7 @@ namespace nymeaserver { QList ScriptEngine::s_engines; QtMessageHandler ScriptEngine::s_upstreamMessageHandler; QLoggingCategory::CategoryFilter ScriptEngine::s_oldCategoryFilter = nullptr; +QMutex ScriptEngine::s_loggerMutex; ScriptEngine::ScriptEngine(ThingManager *deviceManager, QObject *parent) : QObject(parent), m_deviceManager(deviceManager) @@ -446,6 +447,7 @@ void ScriptEngine::logMessageHandler(QtMsgType type, const QMessageLogContext &c return; } + QMutexLocker locker(&s_loggerMutex); // Copy the message to the script engine foreach (ScriptEngine *engine, s_engines) { engine->onScriptMessage(type, context, message); diff --git a/libnymea-core/scriptengine/scriptengine.h b/libnymea-core/scriptengine/scriptengine.h index c4f14661..e60532b9 100644 --- a/libnymea-core/scriptengine/scriptengine.h +++ b/libnymea-core/scriptengine/scriptengine.h @@ -36,6 +36,7 @@ #include #include #include +#include #include "integrations/thingmanager.h" #include "script.h" @@ -111,6 +112,7 @@ private: static QLoggingCategory::CategoryFilter s_oldCategoryFilter; static void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message); static void logCategoryFilter(QLoggingCategory *category); + static QMutex s_loggerMutex; }; } diff --git a/libnymea/loggingcategories.cpp b/libnymea/loggingcategories.cpp index 4d5070c7..45906af3 100644 --- a/libnymea/loggingcategories.cpp +++ b/libnymea/loggingcategories.cpp @@ -32,6 +32,7 @@ #include #include #include +#include QStringList s_nymeaLoggingCategories; @@ -80,6 +81,7 @@ NYMEA_LOGGING_CATEGORY(dcI2C, "I2C") static QFile s_logFile; static bool s_useColors; static QList s_handlers; +static QMutex s_loggerMutex; static const char *const normal = "\033[0m"; static const char *const warning = "\033[33m"; @@ -130,6 +132,7 @@ void nymeaLogMessageHandler(QtMsgType type, const QMessageLogContext &context, c } fflush(stdout); + QMutexLocker locker(&s_loggerMutex); if (s_logFile.isOpen()) { QTextStream textStream(&s_logFile); textStream << messageString << endl;