From fa7087b896c0150c29d7a27c53e0533831a2bf3b Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Tue, 30 Jul 2024 14:30:16 +0200 Subject: [PATCH] util: Use compile-time check for FatalErrorf --- src/index/base.cpp | 5 +++-- src/index/base.h | 5 +++-- src/util/string.h | 9 +++++++++ test/lint/lint-format-strings.py | 1 - test/lint/run-lint-format-strings.py | 2 -- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/index/base.cpp b/src/index/base.cpp index 955d7b67c9e..6f9860415f3 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2022 The Bitcoin Core developers +// Copyright (c) 2017-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include // For g_chainman @@ -27,7 +28,7 @@ constexpr auto SYNC_LOG_INTERVAL{30s}; constexpr auto SYNC_LOCATOR_WRITE_INTERVAL{30s}; template -void BaseIndex::FatalErrorf(const char* fmt, const Args&... args) +void BaseIndex::FatalErrorf(util::ConstevalFormatString fmt, const Args&... args) { auto message = tfm::format(fmt, args...); node::AbortNode(m_chain->context()->shutdown, m_chain->context()->exit_status, Untranslated(message), m_chain->context()->warnings.get()); diff --git a/src/index/base.h b/src/index/base.h index 0eb1d9ca3b2..beb1575ab21 100644 --- a/src/index/base.h +++ b/src/index/base.h @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2022 The Bitcoin Core developers +// Copyright (c) 2017-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -94,7 +95,7 @@ private: virtual bool AllowPrune() const = 0; template - void FatalErrorf(const char* fmt, const Args&... args); + void FatalErrorf(util::ConstevalFormatString fmt, const Args&... args); protected: std::unique_ptr m_chain; diff --git a/src/util/string.h b/src/util/string.h index bdf074a9a60..c5183d6c801 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -6,6 +6,7 @@ #define BITCOIN_UTIL_STRING_H #include +#include #include #include @@ -234,4 +235,12 @@ template } } // namespace util +namespace tinyformat { +template +std::string format(util::ConstevalFormatString fmt, const Args&... args) +{ + return format(fmt.fmt, args...); +} +} // namespace tinyformat + #endif // BITCOIN_UTIL_STRING_H diff --git a/test/lint/lint-format-strings.py b/test/lint/lint-format-strings.py index 9c9fbb2e392..5dbad3f452f 100755 --- a/test/lint/lint-format-strings.py +++ b/test/lint/lint-format-strings.py @@ -16,7 +16,6 @@ import re import sys FUNCTION_NAMES_AND_NUMBER_OF_LEADING_ARGUMENTS = [ - 'FatalErrorf,0', 'tfm::format,1', # Assuming tfm::::format(std::ostream&, ... 'LogConnectFailure,1', 'LogError,0', diff --git a/test/lint/run-lint-format-strings.py b/test/lint/run-lint-format-strings.py index 9a7386ee64a..f78f356a349 100755 --- a/test/lint/run-lint-format-strings.py +++ b/test/lint/run-lint-format-strings.py @@ -13,8 +13,6 @@ import re import sys FALSE_POSITIVES = [ - ("src/index/base.cpp", "FatalErrorf(const char* fmt, const Args&... args)"), - ("src/index/base.h", "FatalErrorf(const char* fmt, const Args&... args)"), ("src/netbase.cpp", "LogConnectFailure(bool manual_connection, const char* fmt, const Args&... args)"), ("src/clientversion.cpp", "strprintf(_(COPYRIGHT_HOLDERS).translated, COPYRIGHT_HOLDERS_SUBSTITUTION)"), ("src/test/translation_tests.cpp", "strprintf(format, arg)"),