Merge bitcoin/bitcoin#26960: refactor: Remove c_str from util/check

fab958290b 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 fab958290b
  aureleoules:
    ACK fab958290b
  theStack:
    ACK fab958290b

Tree-SHA512: ae39812c6bb8e2ef095e1b843774af2718f48404cb848c3e43b16d3c22240557d69d54a13a038a4a9c48b3ba0e4523e1f87abdd60f91486092f50fd43c0e8483
This commit is contained in:
MarcoFalke 2023-01-26 09:02:22 +01:00
commit d4c180ecc9
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
3 changed files with 10 additions and 7 deletions

View file

@ -14,8 +14,9 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <string> #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" return strprintf("Internal bug detected: \"%s\"\n%s:%d (%s)\n"
"%s %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); 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)} : 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); auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", file, line, func, assertion);
fwrite(str.data(), 1, str.size(), stderr); fwrite(str.data(), 1, str.size(), stderr);

View file

@ -8,14 +8,16 @@
#include <attributes.h> #include <attributes.h>
#include <stdexcept> #include <stdexcept>
#include <string>
#include <string_view>
#include <utility> #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 class NonFatalCheckError : public std::runtime_error
{ {
public: 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__) #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 #endif
/** Helper for Assert() */ /** 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() */ /** Helper for Assert()/Assume() */
template <bool IS_ASSERT, typename T> template <bool IS_ASSERT, typename T>

View file

@ -663,7 +663,7 @@ BerkeleyCursor::BerkeleyCursor(BerkeleyDatabase& database)
} }
int ret = database.m_db->cursor(nullptr, &m_cursor, 0); int ret = database.m_db->cursor(nullptr, &m_cursor, 0);
if (ret != 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)));
} }
} }