From e64430941fcd0991e651a8cf3859e682a3232b1e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 Mar 2022 19:13:37 -0500 Subject: [PATCH] refactor: remove tr_strdup_printf() (#2854) --- daemon/daemon-win32.cc | 6 ++--- libtransmission/subprocess-win32.cc | 17 +++++++------- libtransmission/utils.cc | 32 ++------------------------ libtransmission/utils.h | 6 ----- tests/libtransmission/utils-test.cc | 35 ----------------------------- 5 files changed, 13 insertions(+), 83 deletions(-) diff --git a/daemon/daemon-win32.cc b/daemon/daemon-win32.cc index 18fd7b0f4..8ad0be89b 100644 --- a/daemon/daemon-win32.cc +++ b/daemon/daemon-win32.cc @@ -7,7 +7,7 @@ #include -#include +#include #include #include @@ -45,9 +45,7 @@ static HANDLE service_stop_thread = nullptr; static void set_system_error(tr_error** error, DWORD code, char const* message) { auto* const system_message = tr_win32_format_message(code); - auto* const buf = tr_strdup_printf("%s (0x%08lx): %s", message, code, system_message); - tr_error_set(error, code, buf); - tr_free(buf); + tr_error_set(error, code, fmt::format(FMT_STRING("{:s} ({:#08x}): {:s})"), message, code, system_message)); tr_free(system_message); } diff --git a/libtransmission/subprocess-win32.cc b/libtransmission/subprocess-win32.cc index 16b77cd3e..7d5bf4995 100644 --- a/libtransmission/subprocess-win32.cc +++ b/libtransmission/subprocess-win32.cc @@ -11,6 +11,8 @@ #include #include +#include + #include #include "transmission.h" @@ -34,16 +36,15 @@ static void set_system_error(tr_error** error, DWORD code, std::string_view what return; } - char* message = tr_win32_format_message(code); - - if (message == nullptr) + if (char* message = tr_win32_format_message(code); message != nullptr) { - message = tr_strdup_printf("Unknown error: 0x%08lx", code); + tr_error_set(error, code, fmt::format(FMT_STRING("{:s} failed: {:s}"), what, message)); + tr_free(message); + } + else + { + tr_error_set(error, code, fmt::format(FMT_STRING("{:s} failed: Unknown error: {:#08x}"), what, code)); } - - tr_error_set(error, code, tr_strvJoin(what, " failed: "sv, message)); - - tr_free(message); } static void append_to_env_block(wchar_t** env_block, size_t* env_block_len, wchar_t const* part, size_t part_len) diff --git a/libtransmission/utils.cc b/libtransmission/utils.cc index d179d03b1..c84915949 100644 --- a/libtransmission/utils.cc +++ b/libtransmission/utils.cc @@ -36,7 +36,7 @@ #include #include -#include +#include #include "transmission.h" @@ -419,22 +419,6 @@ std::string evbuffer_free_to_str(evbuffer* buf) return ret; } -static char* evbuffer_free_to_str(struct evbuffer* buf, size_t* result_len) -{ - size_t const n = evbuffer_get_length(buf); - auto* const ret = tr_new(char, n + 1); - evbuffer_copyout(buf, ret, n); - evbuffer_free(buf); - ret[n] = '\0'; - - if (result_len != nullptr) - { - *result_len = n; - } - - return ret; -} - char* tr_strvDup(std::string_view in) { auto const n = std::size(in); @@ -466,18 +450,6 @@ bool tr_wildmat(char const* text, char const* p) return (p[0] == '*' && p[1] == '\0') || (DoMatch(text, p) != 0); } -char* tr_strdup_printf(char const* fmt, ...) -{ - evbuffer* const buf = evbuffer_new(); - - va_list ap; - va_start(ap, fmt); - evbuffer_add_vprintf(buf, fmt, ap); - va_end(ap); - - return evbuffer_free_to_str(buf, nullptr); -} - char const* tr_strerror(int i) { char const* ret = strerror(i); @@ -899,7 +871,7 @@ char* tr_win32_format_message(uint32_t code) if (wide_size == 0) { - return tr_strdup_printf("Unknown error (0x%08x)", code); + return tr_strvDup(fmt::format(FMT_STRING("Unknown error ({:#08x})"), code)); } if (wide_size != 0 && wide_text != nullptr) diff --git a/libtransmission/utils.h b/libtransmission/utils.h index a02e4ba7a..0b90e9191 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -269,12 +269,6 @@ constexpr bool tr_str_is_empty(char const* value) std::string evbuffer_free_to_str(evbuffer* buf); -/** - * @brief sprintf() a string into a newly-allocated buffer large enough to hold it - * @return a newly-allocated string that can be freed with tr_free() - */ -char* tr_strdup_printf(char const* fmt, ...) TR_GNUC_MALLOC TR_GNUC_PRINTF(1, 2); - /** @brief Portability wrapper for strlcpy() that uses the system implementation if available */ size_t tr_strlcpy(void* dst, void const* src, size_t siz); diff --git a/tests/libtransmission/utils-test.cc b/tests/libtransmission/utils-test.cc index 0d330de09..fe83e13d3 100644 --- a/tests/libtransmission/utils-test.cc +++ b/tests/libtransmission/utils-test.cc @@ -331,41 +331,6 @@ TEST_F(UtilsTest, truncd) #endif } -TEST_F(UtilsTest, trStrdupPrintfFmtS) -{ - auto s = makeString(tr_strdup_printf("%s", "test")); - EXPECT_EQ("test", s); -} - -TEST_F(UtilsTest, trStrdupPrintf) -{ - auto s = makeString(tr_strdup_printf("%d %s %c %u", -1, "0", '1', 2)); - EXPECT_EQ("-1 0 1 2", s); - - auto* s3 = reinterpret_cast(tr_malloc0(4098)); - memset(s3, '-', 4097); - s3[2047] = 't'; - s3[2048] = 'e'; - s3[2049] = 's'; - s3[2050] = 't'; - - auto* s2 = reinterpret_cast(tr_malloc0(4096)); - memset(s2, '-', 4095); - s2[2047] = '%'; - s2[2048] = 's'; - - // NOLINTNEXTLINE(clang-diagnostic-format-nonliteral) - s = makeString(tr_strdup_printf(s2, "test")); - EXPECT_EQ(s3, s); - - tr_free(s2); - - s = makeString(tr_strdup_printf("%s", s3)); - EXPECT_EQ(s3, s); - - tr_free(s3); -} - TEST_F(UtilsTest, trStrlcpy) { // destination will be initialized with this char