diff --git a/core/libs/prx/libSceNet/Export.cpp b/core/libs/prx/libSceNet/Export.cpp index c5a55aa7f..d4ed6948f 100644 --- a/core/libs/prx/libSceNet/Export.cpp +++ b/core/libs/prx/libSceNet/Export.cpp @@ -38,6 +38,7 @@ namespace { constexpr int NET_ENOENT = 2; constexpr int NET_EBADF = 9; constexpr int NET_EINVAL = 22; +constexpr int NET_ENOSPC = 28; constexpr int NET_EAGAIN = 35; constexpr int NET_ENOTSOCK = 38; constexpr int NET_EOPNOTSUPP = 45; @@ -818,7 +819,7 @@ int APS5_VABI sceNetInetPton(int af, const char* src, void* dst) { } const char* APS5_VABI sceNetInetNtop(int af, const void* src, char* dst, uint32_t size) { - if (src == nullptr || dst == nullptr || size == 0) { + if (src == nullptr || dst == nullptr) { *errno_slot() = NET_EINVAL; return nullptr; } @@ -826,11 +827,23 @@ const char* APS5_VABI sceNetInetNtop(int af, const void* src, char* dst, uint32_ *errno_slot() = NET_EAFNOSUPPORT; return nullptr; } + char text[INET6_ADDRSTRLEN]; #ifdef _WIN32 - return InetNtopA(af == NET_AF_INET ? AF_INET : AF_INET6, const_cast(src), dst, size); + const auto* result = InetNtopA(af == NET_AF_INET ? AF_INET : AF_INET6, const_cast(src), text, sizeof(text)); #else - return ::inet_ntop(af == NET_AF_INET ? AF_INET : AF_INET6, src, dst, size); + const auto* result = ::inet_ntop(af == NET_AF_INET ? AF_INET : AF_INET6, src, text, sizeof(text)); #endif + if (!result) { + *errno_slot() = native_error(); + return nullptr; + } + const auto length = std::strlen(text) + 1; + if (length > size) { + *errno_slot() = NET_ENOSPC; + return nullptr; + } + std::memcpy(dst, text, length); + return dst; } int APS5_VABI sceNetEtherNtostr(const NetEtherAddr* n, char* str, size_t len) { diff --git a/core/libs/tests/GuestSceNet.cpp b/core/libs/tests/GuestSceNet.cpp index 48fcdcea1..607ab52e7 100644 --- a/core/libs/tests/GuestSceNet.cpp +++ b/core/libs/tests/GuestSceNet.cpp @@ -32,14 +32,39 @@ int APS5_VABI sceNetResolverDestroy(int); int APS5_VABI sceNetCtlGetState(int*); int APS5_VABI sceNetInetPton(int, const char*, void*); const char* APS5_VABI sceNetInetNtop(int, const void*, char*, std::uint32_t); +int* APS5_VABI sceNetErrnoLoc(void); } static void Require(bool condition) { if (!condition) std::abort(); } +static void CheckAddressText(int family, const char* text) { + std::array address{}; + Require(sceNetInetPton(family, text, address.data()) == 1); + std::array output{}; + const auto length = static_cast(std::strlen(text)); + for (std::uint32_t size = 0; size <= length; ++size) { + *sceNetErrnoLoc() = 123; + Require(sceNetInetNtop(family, address.data(), output.data(), size) == nullptr); + Require(*sceNetErrnoLoc() == 28); + } + output.fill('x'); + *sceNetErrnoLoc() = 123; + Require(sceNetInetNtop(family, address.data(), output.data(), length + 1) == output.data()); + Require(std::strcmp(output.data(), text) == 0 && output[length + 1] == 'x'); + Require(*sceNetErrnoLoc() == 123); + Require(sceNetInetNtop(99, address.data(), output.data(), output.size()) == nullptr && *sceNetErrnoLoc() == 47); + Require(sceNetInetNtop(family, nullptr, output.data(), output.size()) == nullptr && *sceNetErrnoLoc() == 22); + Require(sceNetInetNtop(family, address.data(), nullptr, output.size()) == nullptr && *sceNetErrnoLoc() == 22); +} + int main() { Require(sceNetInit_nid_postfix() == 0); + CheckAddressText(2, "127.0.0.1"); + CheckAddressText(2, "255.255.255.255"); + CheckAddressText(28, "::1"); + CheckAddressText(28, "1234:5678:9abc:def0:1234:5678:9abc:def0"); const int listener = sceNetSocket(nullptr, 2, 1, 6); Require(listener >= 0); @@ -123,4 +148,4 @@ int main() { int state = -1; Require(sceNetCtlGetState(&state) == 0); Require(state == 0 || state == 3); -} \ No newline at end of file +}