From 04822d5a2c870e0e8441fb0e40d4851da63ffa29 Mon Sep 17 00:00:00 2001 From: Lioncache Date: Mon, 18 Dec 2023 10:05:48 -0500 Subject: [PATCH 1/7] Core/IOS/ES: Make use of fmt::format where applicable Eliminates a deprecation warnings on macOS. While we're in the same area, we can remove the call to GetPointer() and instead use CopyToEmu() to copy the string data back to the emulated memory. --- Source/Core/Core/IOS/ES/ES.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Source/Core/Core/IOS/ES/ES.cpp b/Source/Core/Core/IOS/ES/ES.cpp index ec8a5aa5ab..41e15e613b 100644 --- a/Source/Core/Core/IOS/ES/ES.cpp +++ b/Source/Core/Core/IOS/ES/ES.cpp @@ -9,6 +9,8 @@ #include #include +#include + #include "Common/ChunkFile.h" #include "Common/EnumUtils.h" #include "Common/Logging/Log.h" @@ -218,10 +220,11 @@ IPCReply ESDevice::GetTitleDirectory(const IOCtlVRequest& request) auto& memory = system.GetMemory(); const u64 title_id = memory.Read_U64(request.in_vectors[0].address); + const auto path = fmt::format("/title/{:08x}/{:08x}/data", static_cast(title_id >> 32), + static_cast(title_id)); - char* path = reinterpret_cast(memory.GetPointer(request.io_vectors[0].address)); - sprintf(path, "/title/%08x/%08x/data", static_cast(title_id >> 32), - static_cast(title_id)); + const auto path_dst = request.io_vectors[0].address; + memory.CopyToEmu(path_dst, path.data(), path.size()); INFO_LOG_FMT(IOS_ES, "IOCTL_ES_GETTITLEDIR: {}", path); return IPCReply(IPC_SUCCESS); From 2b877fc92bce9b821306c8521408419f5ed1b2c6 Mon Sep 17 00:00:00 2001 From: Lioncache Date: Mon, 18 Dec 2023 10:11:47 -0500 Subject: [PATCH 2/7] Core/IOS/Top: Make use of fmt::format_to_n where applicable Same behavior, but avoids deprecation warnings on macOS builds and also lets us use generic print formatting. --- Source/Core/Core/IOS/Network/IP/Top.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Source/Core/Core/IOS/Network/IP/Top.cpp b/Source/Core/Core/IOS/Network/IP/Top.cpp index 12a6cda53b..1cf70e8df3 100644 --- a/Source/Core/Core/IOS/Network/IP/Top.cpp +++ b/Source/Core/Core/IOS/Network/IP/Top.cpp @@ -667,10 +667,11 @@ IPCReply NetIPTopDevice::HandleInetNToPRequest(const IOCtlRequest& request) // u32 validAddress = memory.Read_U32(request.buffer_in + 4); // u32 src = memory.Read_U32(request.buffer_in + 8); - char ip_s[16]; - sprintf(ip_s, "%i.%i.%i.%i", memory.Read_U8(request.buffer_in + 8), - memory.Read_U8(request.buffer_in + 8 + 1), memory.Read_U8(request.buffer_in + 8 + 2), - memory.Read_U8(request.buffer_in + 8 + 3)); + char ip_s[16]{}; + fmt::format_to_n(ip_s, sizeof(ip_s) - 1, "{}.{}.{}.{}", memory.Read_U8(request.buffer_in + 8), + memory.Read_U8(request.buffer_in + 8 + 1), + memory.Read_U8(request.buffer_in + 8 + 2), + memory.Read_U8(request.buffer_in + 8 + 3)); INFO_LOG_FMT(IOS_NET, "IOCTL_SO_INETNTOP {}", ip_s); memory.CopyToEmu(request.buffer_out, reinterpret_cast(ip_s), std::strlen(ip_s)); From 9abcc1c08b6f51a7dead69523dfc86ba115d8f86 Mon Sep 17 00:00:00 2001 From: Lioncache Date: Mon, 18 Dec 2023 10:16:10 -0500 Subject: [PATCH 3/7] Core/NetPlayServer: Use fmt::format_to_n in GetInterfaceHost() Lets us avoid some noisy deprecation warnings on macOS (and also lets us use a more generic formatting specifier). --- Source/Core/Core/NetPlayServer.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/Core/Core/NetPlayServer.cpp b/Source/Core/Core/NetPlayServer.cpp index a939a15681..0705e4d2a7 100644 --- a/Source/Core/Core/NetPlayServer.cpp +++ b/Source/Core/Core/NetPlayServer.cpp @@ -2294,8 +2294,9 @@ std::unordered_set NetPlayServer::GetInterfaceSet() const // called from ---GUI--- thread std::string NetPlayServer::GetInterfaceHost(const std::string& inter) const { - char buf[16]; - sprintf(buf, ":%d", GetPort()); + char buf[16]{}; + fmt::format_to_n(buf, sizeof(buf) - 1, ":{}", GetPort()); + auto lst = GetInterfaceListInternal(); for (const auto& list_entry : lst) { From 62740d97a83b6035e1c783b9cf9ae786e358b4ee Mon Sep 17 00:00:00 2001 From: Lioncache Date: Mon, 18 Dec 2023 10:20:26 -0500 Subject: [PATCH 4/7] Core/DSP/DSPTables: Make use of fmt::format in pdname() Resolves a deprecation warning on macOS. Also, we can convert the function to just return a std::string instead of using a static internal buffer, which is gross and thread-unsafe. --- Source/Core/Core/DSP/DSPTables.cpp | 9 ++++----- Source/Core/Core/DSP/DSPTables.h | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Source/Core/Core/DSP/DSPTables.cpp b/Source/Core/Core/DSP/DSPTables.cpp index cb8a224396..191f8b3622 100644 --- a/Source/Core/Core/DSP/DSPTables.cpp +++ b/Source/Core/Core/DSP/DSPTables.cpp @@ -10,6 +10,8 @@ #include #include +#include + #include "Common/CommonTypes.h" #include "Common/Logging/Log.h" @@ -492,18 +494,15 @@ const std::array regnames = }}; // clang-format on -const char* pdname(u16 val) +std::string pdname(u16 val) { - static char tmpstr[12]; // nasty - for (const pdlabel_t& pdlabel : pdlabels) { if (pdlabel.addr == val) return pdlabel.name; } - sprintf(tmpstr, "0x%04x", val); - return tmpstr; + return fmt::format("0x{:04x}", val); } const char* pdregname(int val) diff --git a/Source/Core/Core/DSP/DSPTables.h b/Source/Core/Core/DSP/DSPTables.h index e2a90d8e7f..8dead3acad 100644 --- a/Source/Core/Core/DSP/DSPTables.h +++ b/Source/Core/Core/DSP/DSPTables.h @@ -93,7 +93,7 @@ struct pdlabel_t extern const std::array regnames; extern const std::array pdlabels; -const char* pdname(u16 val); +std::string pdname(u16 val); const char* pdregname(int val); const char* pdregnamelong(int val); From 1764b13423059a06f718687c55d44bc20cc3330b Mon Sep 17 00:00:00 2001 From: Lioncache Date: Mon, 18 Dec 2023 12:55:11 -0500 Subject: [PATCH 5/7] Core/DSP/DSPTables: Make pdregname() and pdregnamelong() return std::string Keeps the interface consistent. --- Source/Core/Core/DSP/DSPTables.cpp | 4 ++-- Source/Core/Core/DSP/DSPTables.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Core/Core/DSP/DSPTables.cpp b/Source/Core/Core/DSP/DSPTables.cpp index 191f8b3622..175b7601c7 100644 --- a/Source/Core/Core/DSP/DSPTables.cpp +++ b/Source/Core/Core/DSP/DSPTables.cpp @@ -505,12 +505,12 @@ std::string pdname(u16 val) return fmt::format("0x{:04x}", val); } -const char* pdregname(int val) +std::string pdregname(int val) { return regnames[val].name; } -const char* pdregnamelong(int val) +std::string pdregnamelong(int val) { return regnames[val].description; } diff --git a/Source/Core/Core/DSP/DSPTables.h b/Source/Core/Core/DSP/DSPTables.h index 8dead3acad..e2ec833273 100644 --- a/Source/Core/Core/DSP/DSPTables.h +++ b/Source/Core/Core/DSP/DSPTables.h @@ -94,8 +94,8 @@ extern const std::array regnames; extern const std::array pdlabels; std::string pdname(u16 val); -const char* pdregname(int val); -const char* pdregnamelong(int val); +std::string pdregname(int val); +std::string pdregnamelong(int val); void InitInstructionTable(); From dc85194fac9f0527a15cf147fe75a15a7e573a1e Mon Sep 17 00:00:00 2001 From: Lioncache Date: Mon, 18 Dec 2023 10:48:42 -0500 Subject: [PATCH 6/7] 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; } From affe92898725d20ad0bac7e090063dad2f2cecf9 Mon Sep 17 00:00:00 2001 From: Lioncache Date: Mon, 18 Dec 2023 10:55:31 -0500 Subject: [PATCH 7/7] Common/TraversalServer: Make use of fmt more We can convert printf and fprintf over to the use of fmt as well, given that we've converted the sprintf calls over. --- Source/Core/Common/TraversalServer.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Source/Core/Common/TraversalServer.cpp b/Source/Core/Common/TraversalServer.cpp index 784ef9d743..3d449256d1 100644 --- a/Source/Core/Common/TraversalServer.cpp +++ b/Source/Core/Common/TraversalServer.cpp @@ -87,12 +87,12 @@ retry: } } #if DEBUG - printf("failed to find key '"); + fmt::print("failed to find key '"); for (size_t i = 0; i < sizeof(key); i++) { - printf("%02x", ((u8*)&key)[i]); + fmt::print("{:02x}", ((u8*)&key)[i]); } - printf("'\n"); + fmt::print("'\n"); #endif result.found = false; return result; @@ -133,7 +133,7 @@ static Common::TraversalInetAddress MakeInetAddress(const sockaddr_in6& addr) { if (addr.sin6_family != AF_INET6) { - fprintf(stderr, "bad sockaddr_in6\n"); + fmt::print(stderr, "bad sockaddr_in6\n"); exit(1); } u32* words = (u32*)addr.sin6_addr.s6_addr; @@ -197,8 +197,8 @@ static void TrySend(const void* buffer, size_t size, sockaddr_in6* addr, bool fr { #if DEBUG const auto* packet = static_cast(buffer); - printf("%s-> %d %llu %s\n", fromAlt ? "alt " : "", static_cast(packet->type), - static_cast(packet->requestId), SenderName(addr)); + fmt::print("{}-> {} {} {}\n", fromAlt ? "alt " : "", static_cast(packet->type), + static_cast(packet->requestId), SenderName(addr)); #endif if ((size_t)sendto(fromAlt ? sockAlt : sock, buffer, size, 0, (sockaddr*)addr, sizeof(*addr)) != size) @@ -271,8 +271,8 @@ static void ResendPackets() static void HandlePacket(Common::TraversalPacket* packet, sockaddr_in6* addr, bool toAlt) { #if DEBUG - printf("<- %d %llu %s\n", static_cast(packet->type), - static_cast(packet->requestId), SenderName(addr)); + fmt::print("<- {} {} {}\n", static_cast(packet->type), + static_cast(packet->requestId), SenderName(addr)); #endif bool packetOk = true; switch (packet->type) @@ -377,8 +377,8 @@ static void HandlePacket(Common::TraversalPacket* packet, sockaddr_in6* addr, bo break; } default: - fprintf(stderr, "received unknown packet type %d from %s\n", static_cast(packet->type), - SenderName(addr)); + fmt::print(stderr, "received unknown packet type {} from {}\n", static_cast(packet->type), + SenderName(addr)); break; } if (packet->type != Common::TraversalPacketType::Ack) @@ -516,7 +516,7 @@ int main() } else if ((size_t)rv < sizeof(packet)) { - fprintf(stderr, "received short packet from %s\n", SenderName(&raddr)); + fmt::print(stderr, "received short packet from {}\n", SenderName(&raddr)); } else {