From d5113384c9ef9a3b01a856b7317d7d756023af91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Thu, 28 Apr 2022 14:25:53 +0200 Subject: [PATCH] AppleUnifiedLogger: Remove manually managed os_log_t cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The os_log_create API documents that the logging subsystem will maintain a global list of logs, and will return existing logs for subsequent calls for the same category and subsystem, so we don't need to maintain our own log. This also avoids a crash were we would end up in the logging backend during application shutdown, when destructor functions run, for example via QLibraryStore::cleanup(). In this case local static such as our log cache had already been destroyed, leading to use after free. Pick-to: 6.2 6.3 5.15 Change-Id: I91abfef6c56f8ca6dd8d602ac83aa163ce3d6860 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Marc Mutz --- src/corelib/kernel/qcore_mac.mm | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/src/corelib/kernel/qcore_mac.mm b/src/corelib/kernel/qcore_mac.mm index a251c51477..1b09bdfdd5 100644 --- a/src/corelib/kernel/qcore_mac.mm +++ b/src/corelib/kernel/qcore_mac.mm @@ -133,7 +133,7 @@ bool AppleUnifiedLogger::messageHandler(QtMsgType msgType, const QMessageLogCont const bool isDefault = !context.category || !strcmp(context.category, "default"); os_log_t log = isDefault ? OS_LOG_DEFAULT : - cachedLog(subsystem, QString::fromLatin1(context.category)); + os_log_create(subsystem.toLatin1().constData(), context.category); os_log_type_t logType = logTypeForMessageType(msgType); if (!os_log_type_enabled(log, logType)) @@ -169,29 +169,6 @@ os_log_type_t AppleUnifiedLogger::logTypeForMessageType(QtMsgType msgType) return OS_LOG_TYPE_DEFAULT; } -os_log_t AppleUnifiedLogger::cachedLog(const QString &subsystem, const QString &category) -{ - static QBasicMutex mutex; - const auto locker = qt_scoped_lock(mutex); - - static QHash, os_log_t> logs; - const auto cacheKey = qMakePair(subsystem, category); - os_log_t log = logs.value(cacheKey); - - if (!log) { - log = os_log_create(subsystem.toLatin1().constData(), - category.toLatin1().constData()); - logs.insert(cacheKey, log); - - // Technically we should release the os_log_t resource when done - // with it, but since we don't know when a category is disabled - // we keep all cached os_log_t instances until shutdown, where - // the OS will clean them up for us. - } - - return log; -} - #endif // QT_USE_APPLE_UNIFIED_LOGGING // -------------------------------------------------------------------------