From dc85194fac9f0527a15cf147fe75a15a7e573a1e Mon Sep 17 00:00:00 2001 From: Lioncache Date: Mon, 18 Dec 2023 10:48:42 -0500 Subject: [PATCH] Common/TraversalServer: Make use of fmt over sprintf where applicable Resolves some deprecation warnings on macOS. This is better anyway, given fmt has generic type formatting. --- Source/Core/Common/CMakeLists.txt | 2 +- Source/Core/Common/TraversalServer.cpp | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt index a4a761276b..9f8702659b 100644 --- a/Source/Core/Common/CMakeLists.txt +++ b/Source/Core/Common/CMakeLists.txt @@ -329,7 +329,7 @@ endif() if(UNIX) # Posix networking code needs to be fixed for Windows add_executable(traversal_server TraversalServer.cpp) - target_link_libraries(traversal_server PRIVATE common) + target_link_libraries(traversal_server PRIVATE common fmt::fmt) if(SYSTEMD_FOUND) target_link_libraries(traversal_server PRIVATE ${SYSTEMD_LIBRARIES}) endif() diff --git a/Source/Core/Common/TraversalServer.cpp b/Source/Core/Common/TraversalServer.cpp index 5991de9e99..784ef9d743 100644 --- a/Source/Core/Common/TraversalServer.cpp +++ b/Source/Core/Common/TraversalServer.cpp @@ -18,6 +18,8 @@ #include #include +#include + #ifdef HAVE_LIBSYSTEMD #include #endif @@ -177,17 +179,17 @@ static sockaddr_in6 MakeSinAddr(const Common::TraversalInetAddress& addr) static void GetRandomHostId(Common::TraversalHostId* hostId) { - char buf[9]; + char buf[9]{}; const u32 num = Common::Random::GenerateValue(); - sprintf(buf, "%08x", num); + fmt::format_to_n(buf, sizeof(buf) - 1, "{:08x}", num); memcpy(hostId->data(), buf, 8); } static const char* SenderName(sockaddr_in6* addr) { - static char buf[INET6_ADDRSTRLEN + 10]; + static char buf[INET6_ADDRSTRLEN + 10]{}; inet_ntop(PF_INET6, &addr->sin6_addr, buf, sizeof(buf)); - sprintf(buf + strlen(buf), ":%d", ntohs(addr->sin6_port)); + fmt::format_to(buf + strlen(buf), ":{}", ntohs(addr->sin6_port)); return buf; }