Skip to content

Commit d4c180e

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#26960: refactor: Remove c_str from util/check
fab9582 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 fab9582 aureleoules: ACK fab9582 theStack: ACK fab9582 Tree-SHA512: ae39812c6bb8e2ef095e1b843774af2718f48404cb848c3e43b16d3c22240557d69d54a13a038a4a9c48b3ba0e4523e1f87abdd60f91486092f50fd43c0e8483
2 parents ab98673 + fab9582 commit d4c180e

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/util/check.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,22 @@
1414
#include <cstdio>
1515
#include <cstdlib>
1616
#include <string>
17+
#include <string_view>
1718

18-
std::string StrFormatInternalBug(const char* msg, const char* file, int line, const char* func)
19+
std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func)
1920
{
2021
return strprintf("Internal bug detected: \"%s\"\n%s:%d (%s)\n"
2122
"%s %s\n"
2223
"Please report this issue here: %s\n",
2324
msg, file, line, func, PACKAGE_NAME, FormatFullVersion(), PACKAGE_BUGREPORT);
2425
}
2526

26-
NonFatalCheckError::NonFatalCheckError(const char* msg, const char* file, int line, const char* func)
27+
NonFatalCheckError::NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func)
2728
: std::runtime_error{StrFormatInternalBug(msg, file, line, func)}
2829
{
2930
}
3031

31-
void assertion_fail(const char* file, int line, const char* func, const char* assertion)
32+
void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion)
3233
{
3334
auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", file, line, func, assertion);
3435
fwrite(str.data(), 1, str.size(), stderr);

src/util/check.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
#include <attributes.h>
99

1010
#include <stdexcept>
11+
#include <string>
12+
#include <string_view>
1113
#include <utility>
1214

13-
std::string StrFormatInternalBug(const char* msg, const char* file, int line, const char* func);
15+
std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func);
1416

1517
class NonFatalCheckError : public std::runtime_error
1618
{
1719
public:
18-
NonFatalCheckError(const char* msg, const char* file, int line, const char* func);
20+
NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func);
1921
};
2022

2123
#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
4951
#endif
5052

5153
/** Helper for Assert() */
52-
void assertion_fail(const char* file, int line, const char* func, const char* assertion);
54+
void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion);
5355

5456
/** Helper for Assert()/Assume() */
5557
template <bool IS_ASSERT, typename T>

src/wallet/bdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ BerkeleyCursor::BerkeleyCursor(BerkeleyDatabase& database)
663663
}
664664
int ret = database.m_db->cursor(nullptr, &m_cursor, 0);
665665
if (ret != 0) {
666-
throw std::runtime_error(STR_INTERNAL_BUG(strprintf("BDB Cursor could not be created. Returned %d", ret).c_str()));
666+
throw std::runtime_error(STR_INTERNAL_BUG(strprintf("BDB Cursor could not be created. Returned %d", ret)));
667667
}
668668
}
669669

0 commit comments

Comments
 (0)