diff --git a/libtransmission/bitfield.cc b/libtransmission/bitfield.cc index 30ef1053f..e88af741c 100644 --- a/libtransmission/bitfield.cc +++ b/libtransmission/bitfield.cc @@ -218,12 +218,7 @@ void tr_bitfield::freeArray() void tr_bitfield::setTrueCount(size_t n) { - TR_ASSERT_MSG( - bit_count_ == 0 || n <= bit_count_, - "bit_count_:%zu, n:%zu, std::size(flags_):%zu", - bit_count_, - n, - size_t(std::size(flags_))); + TR_ASSERT(bit_count_ == 0 || n <= bit_count_); true_count_ = n; have_all_hint_ = n == bit_count_; diff --git a/libtransmission/file-posix.cc b/libtransmission/file-posix.cc index 1eae35fc5..ce95bb68f 100644 --- a/libtransmission/file-posix.cc +++ b/libtransmission/file-posix.cc @@ -582,7 +582,7 @@ tr_sys_file_t tr_sys_file_get_std(tr_std_sys_file_t std_file, tr_error** error) break; default: - TR_ASSERT_MSG(false, "unknown standard file %d", (int)std_file); + TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unknown standard file {:d}"), std_file)); set_system_error(error, EINVAL); } diff --git a/libtransmission/file-win32.cc b/libtransmission/file-win32.cc index dd3bf7a7c..76dbccf2c 100644 --- a/libtransmission/file-win32.cc +++ b/libtransmission/file-win32.cc @@ -12,7 +12,7 @@ #include /* SHCreateDirectoryEx() */ #include /* FSCTL_SET_SPARSE */ -#include +#include #include "transmission.h" #include "crypto-utils.h" /* tr_rand_int() */ @@ -894,7 +894,7 @@ tr_sys_file_t tr_sys_file_get_std(tr_std_sys_file_t std_file, tr_error** error) break; default: - TR_ASSERT_MSG(false, "unknown standard file %d", (int)std_file); + TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unknown standard file {:d}"), std_file)); set_system_error(error, ERROR_INVALID_PARAMETER); return TR_BAD_SYS_FILE; } diff --git a/libtransmission/handshake.cc b/libtransmission/handshake.cc index 5d3893971..6819fde29 100644 --- a/libtransmission/handshake.cc +++ b/libtransmission/handshake.cc @@ -12,7 +12,7 @@ #include #include -#include +#include #include "transmission.h" #include "clients.h" @@ -1055,7 +1055,7 @@ static ReadState canRead(tr_peerIo* io, void* vhandshake, size_t* piece) default: #ifdef TR_ENABLE_ASSERTS - TR_ASSERT_MSG(false, "unhandled handshake state %d", (int)handshake->state); + TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unhandled handshake state {:d}"), handshake->state)); #else ret = READ_ERR; break; diff --git a/libtransmission/net.cc b/libtransmission/net.cc index b2f4170d4..b68eaf915 100644 --- a/libtransmission/net.cc +++ b/libtransmission/net.cc @@ -456,7 +456,7 @@ void tr_netClosePeerSocket(tr_session* session, tr_peer_socket socket) #endif default: - TR_ASSERT_MSG(false, "unsupported peer socket type %d", socket.type); + TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unsupported peer socket type {:d}"), socket.type)); } } diff --git a/libtransmission/peer-io.cc b/libtransmission/peer-io.cc index b58beaf79..5ca316b68 100644 --- a/libtransmission/peer-io.cc +++ b/libtransmission/peer-io.cc @@ -15,8 +15,7 @@ #include -#include -#include // fmt::ptr +#include #include "transmission.h" #include "session.h" @@ -648,7 +647,7 @@ static tr_peerIo* tr_peerIoNew( #endif default: - TR_ASSERT_MSG(false, "unsupported peer socket type %d", socket.type); + TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unsupported peer socket type {:d}"), socket.type)); } return io; @@ -1190,7 +1189,7 @@ void tr_peerIoReadBytes(tr_peerIo* io, struct evbuffer* inbuf, void* bytes, size break; default: - TR_ASSERT_MSG(false, "unhandled encryption type %d", (int)io->encryption_type); + TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unhandled encryption type {:d}"), io->encryption_type)); } } diff --git a/libtransmission/peer-mgr.cc b/libtransmission/peer-mgr.cc index 4f813748f..81fab46c3 100644 --- a/libtransmission/peer-mgr.cc +++ b/libtransmission/peer-mgr.cc @@ -16,7 +16,7 @@ #include -#include +#include #define LIBTRANSMISSION_PEER_MODULE #include "transmission.h" @@ -863,7 +863,7 @@ static void peerCallbackFunc(tr_peer* peer, tr_peer_event const* e, void* vs) break; default: - TR_ASSERT_MSG(false, "%s", fmt::format("unhandled peer event type {}", e->eventType).c_str()); + TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unhandled peer event type {:d}"), e->eventType)); } } diff --git a/libtransmission/peer-msgs.cc b/libtransmission/peer-msgs.cc index 42892367d..285174398 100644 --- a/libtransmission/peer-msgs.cc +++ b/libtransmission/peer-msgs.cc @@ -15,6 +15,8 @@ #include #include +#include + #include "transmission.h" #include "cache.h" @@ -1961,7 +1963,7 @@ static ReadState canRead(tr_peerIo* io, void* vmsgs, size_t* piece) default: #ifdef TR_ENABLE_ASSERTS - TR_ASSERT_MSG(false, "unhandled peer messages state %d", int(msgs->state)); + TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unhandled peer messages state {:d}"), int(msgs->state))); #else ret = READ_ERR; break; diff --git a/libtransmission/subprocess-win32.cc b/libtransmission/subprocess-win32.cc index 7d5bf4995..c39f9512f 100644 --- a/libtransmission/subprocess-win32.cc +++ b/libtransmission/subprocess-win32.cc @@ -343,7 +343,7 @@ static void append_app_launcher_arguments(enum tr_app_type app_type, char** args break; default: - TR_ASSERT_MSG(false, "unsupported application type %d", (int)app_type); + TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unsupported application type {:d}"), app_type)); break; } } diff --git a/libtransmission/tr-assert.cc b/libtransmission/tr-assert.cc index 4cfb39f29..43da2d93b 100644 --- a/libtransmission/tr-assert.cc +++ b/libtransmission/tr-assert.cc @@ -3,25 +3,17 @@ // or any future license endorsed by Mnemosyne LLC. // License text can be found in the licenses/ folder. -#include #include #include +#include #include "tr-assert.h" #if !defined(NDEBUG) || defined(TR_FORCE_ASSERTIONS) -[[noreturn]] bool tr_assert_report(char const* file, int line, char const* message_fmt, ...) +[[noreturn]] bool tr_assert_report(std::string_view file, int line, std::string_view message) { - va_list args; - va_start(args, message_fmt); - - fprintf(stderr, "assertion failed: "); - vfprintf(stderr, message_fmt, args); - fprintf(stderr, " (%s:%d)\n", file, line); - - va_end(args); - + std::cerr << "assertion failed: " << message << " (" << file << ':' << line << ')' << std::endl; abort(); } diff --git a/libtransmission/tr-assert.h b/libtransmission/tr-assert.h index eca732759..62759b43c 100644 --- a/libtransmission/tr-assert.h +++ b/libtransmission/tr-assert.h @@ -7,14 +7,14 @@ #if !defined(NDEBUG) || defined(TR_FORCE_ASSERTIONS) -#include +#include #include "tr-macros.h" -[[noreturn]] bool tr_assert_report(char const* file, int line, char const* message_fmt, ...) TR_GNUC_PRINTF(3, 4); +[[noreturn]] bool tr_assert_report(std::string_view file, int line, std::string_view message); -#define TR_ASSERT(x) ((void)(TR_LIKELY(x) || tr_assert_report(__FILE__, __LINE__, "%s", #x))) -#define TR_ASSERT_MSG(x, ...) ((void)(TR_LIKELY(x) || tr_assert_report(__FILE__, __LINE__, __VA_ARGS__))) +#define TR_ASSERT(x) ((void)(TR_LIKELY(x) || tr_assert_report(__FILE__, __LINE__, #x))) +#define TR_ASSERT_MSG(x, message) ((void)(TR_LIKELY(x) || tr_assert_report(__FILE__, __LINE__, message))) #define TR_UNREACHABLE() tr_assert_report(__FILE__, __LINE__, "Unreachable code") #define TR_ENABLE_ASSERTS diff --git a/libtransmission/tr-assert.mm b/libtransmission/tr-assert.mm index 8dc6ec290..b6f1f7a37 100644 --- a/libtransmission/tr-assert.mm +++ b/libtransmission/tr-assert.mm @@ -5,9 +5,7 @@ #import -#include -#include -#include +#include #include "tr-assert.h" @@ -16,16 +14,10 @@ // macOS implementation of tr_assert_report() that provides the message in the crash report // This replaces the generic implementation of the function in tr-assert.cc -[[noreturn]] bool tr_assert_report(char const* file, int line, char const* message_fmt, ...) +[[noreturn]] bool tr_assert_report(std::string_view file, int line, std::string_view message) { - char buffer[1024]; - va_list args; - - va_start(args, message_fmt); - vsnprintf(buffer, sizeof(buffer), message_fmt, args); - va_end(args); - - [NSException raise:NSInternalInconsistencyException format:@"assertion failed: %s (%s:%d)", buffer, file, line]; + auto const full_text = fmt::format(FMT_STRING("assertion failed: {:s} ({:s}:{:d})"), message, file, line); + [NSException raise:NSInternalInconsistencyException format:@"%s", full_text.c_str()]; // We should not reach this anyway, but it helps mark the function as propertly noreturn // (the Objective-C NSException method does not).