diff options
author | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2023-01-26 09:02:22 +0100 |
---|---|---|
committer | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2023-01-26 09:02:36 +0100 |
commit | d4c180ecc9aec5baa6c074f78e4d2c5e28b124ac (patch) | |
tree | 26edec3d7a365b3c1a45350cc86254da7bb9eb40 /src | |
parent | ab98673f058853e00c310afad57925f54c1ecfae (diff) | |
parent | fab958290b84037fad1d78ffb2bce34d2b0e8c36 (diff) |
Merge bitcoin/bitcoin#26960: refactor: Remove c_str from util/check
fab958290b84037fad1d78ffb2bce34d2b0e8c36 refactor: Remove c_str from util/check (MarcoFalke)
Pull request description:
Seems confusing and fragile to require calling code to call `c_str()` when passing a read-only view of a std::string.
Fix that by using std::string_view, which can be constructed from string literals and std::string.
Also, remove the now unused `c_str()` from `src/wallet/bdb.cpp`.
ACKs for top commit:
stickies-v:
ACK fab958290b84037fad1d78ffb2bce34d2b0e8c36
aureleoules:
ACK fab958290b84037fad1d78ffb2bce34d2b0e8c36
theStack:
ACK fab958290b84037fad1d78ffb2bce34d2b0e8c36
Tree-SHA512: ae39812c6bb8e2ef095e1b843774af2718f48404cb848c3e43b16d3c22240557d69d54a13a038a4a9c48b3ba0e4523e1f87abdd60f91486092f50fd43c0e8483
Diffstat (limited to 'src')
-rw-r--r-- | src/util/check.cpp | 7 | ||||
-rw-r--r-- | src/util/check.h | 8 | ||||
-rw-r--r-- | src/wallet/bdb.cpp | 2 |
3 files changed, 10 insertions, 7 deletions
diff --git a/src/util/check.cpp b/src/util/check.cpp index 34b9d376a7..795dce7124 100644 --- a/src/util/check.cpp +++ b/src/util/check.cpp @@ -14,8 +14,9 @@ #include <cstdio> #include <cstdlib> #include <string> +#include <string_view> -std::string StrFormatInternalBug(const char* msg, const char* file, int line, const char* func) +std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func) { return strprintf("Internal bug detected: \"%s\"\n%s:%d (%s)\n" "%s %s\n" @@ -23,12 +24,12 @@ std::string StrFormatInternalBug(const char* msg, const char* file, int line, co msg, file, line, func, PACKAGE_NAME, FormatFullVersion(), PACKAGE_BUGREPORT); } -NonFatalCheckError::NonFatalCheckError(const char* msg, const char* file, int line, const char* func) +NonFatalCheckError::NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func) : std::runtime_error{StrFormatInternalBug(msg, file, line, func)} { } -void assertion_fail(const char* file, int line, const char* func, const char* assertion) +void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion) { auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", file, line, func, assertion); fwrite(str.data(), 1, str.size(), stderr); diff --git a/src/util/check.h b/src/util/check.h index 96cd905d47..7ddcebf506 100644 --- a/src/util/check.h +++ b/src/util/check.h @@ -8,14 +8,16 @@ #include <attributes.h> #include <stdexcept> +#include <string> +#include <string_view> #include <utility> -std::string StrFormatInternalBug(const char* msg, const char* file, int line, const char* func); +std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func); class NonFatalCheckError : public std::runtime_error { public: - NonFatalCheckError(const char* msg, const char* file, int line, const char* func); + NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func); }; #define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), __FILE__, __LINE__, __func__) @@ -49,7 +51,7 @@ T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, co #endif /** Helper for Assert() */ -void assertion_fail(const char* file, int line, const char* func, const char* assertion); +void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion); /** Helper for Assert()/Assume() */ template <bool IS_ASSERT, typename T> diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp index dc14a60595..0d42f82f68 100644 --- a/src/wallet/bdb.cpp +++ b/src/wallet/bdb.cpp @@ -663,7 +663,7 @@ BerkeleyCursor::BerkeleyCursor(BerkeleyDatabase& database) } int ret = database.m_db->cursor(nullptr, &m_cursor, 0); if (ret != 0) { - throw std::runtime_error(STR_INTERNAL_BUG(strprintf("BDB Cursor could not be created. Returned %d", ret).c_str())); + throw std::runtime_error(STR_INTERNAL_BUG(strprintf("BDB Cursor could not be created. Returned %d", ret))); } } |