From 47c91696ee574957f0d3312f92a50ad89bc80304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Thu, 19 Nov 2020 02:36:36 +0100 Subject: [PATCH] Common/Log: Check format strings Helps with catching incorrect format strings. PanicAlertFmt already uses FMT_STRING and fmt::make_args_checked. --- Source/Core/Common/Logging/Log.h | 15 ++++++++------- Source/Core/Common/Logging/LogManager.cpp | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Source/Core/Common/Logging/Log.h b/Source/Core/Common/Logging/Log.h index 95f76b138a..53aee4ec9a 100644 --- a/Source/Core/Common/Logging/Log.h +++ b/Source/Core/Common/Logging/Log.h @@ -76,13 +76,14 @@ enum LOG_LEVELS static const char LOG_LEVEL_TO_CHAR[7] = "-NEWID"; void GenericLogFmtImpl(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, - std::string_view format, const fmt::format_args& args); + fmt::string_view format, const fmt::format_args& args); -template -void GenericLogFmt(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, - std::string_view format, const Args&... args) +template +void GenericLogFmt(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const S& format, + const Args&... args) { - GenericLogFmtImpl(level, type, file, line, format, fmt::make_format_args(args...)); + GenericLogFmtImpl(level, type, file, line, format, + fmt::make_args_checked(format, args...)); } void GenericLog(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt, ...) @@ -136,11 +137,11 @@ void GenericLog(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, con // fmtlib capable API -#define GENERIC_LOG_FMT(t, v, ...) \ +#define GENERIC_LOG_FMT(t, v, format, ...) \ do \ { \ if (v <= MAX_LOGLEVEL) \ - Common::Log::GenericLogFmt(v, t, __FILE__, __LINE__, __VA_ARGS__); \ + Common::Log::GenericLogFmt(v, t, __FILE__, __LINE__, FMT_STRING(format), ##__VA_ARGS__); \ } while (0) #define ERROR_LOG_FMT(t, ...) \ diff --git a/Source/Core/Common/Logging/LogManager.cpp b/Source/Core/Common/Logging/LogManager.cpp index 2255fc6f8e..7e56b6426a 100644 --- a/Source/Core/Common/Logging/LogManager.cpp +++ b/Source/Core/Common/Logging/LogManager.cpp @@ -81,7 +81,7 @@ void GenericLog(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, con } void GenericLogFmtImpl(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, - std::string_view format, const fmt::format_args& args) + fmt::string_view format, const fmt::format_args& args) { auto* instance = LogManager::GetInstance(); if (instance == nullptr)