fix(net): report address text buffer errors to the guest

This commit is contained in:
Tomás Ortellado
2026-10-05 09:01:14 -03:00
parent 53dc19cf61
commit 77dc9e77e1
2 changed files with 42 additions and 4 deletions
+16 -3
View File
@@ -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;
@@ -814,7 +815,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;
}
@@ -822,11 +823,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<void*>(src), dst, size);
const auto* result = InetNtopA(af == NET_AF_INET ? AF_INET : AF_INET6, const_cast<void*>(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) {
+26 -1
View File
@@ -28,14 +28,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<std::uint8_t, 16> address{};
Require(sceNetInetPton(family, text, address.data()) == 1);
std::array<char, 64> output{};
const auto length = static_cast<std::uint32_t>(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);
@@ -108,4 +133,4 @@ int main() {
int state = -1;
Require(sceNetCtlGetState(&state) == 0);
Require(state == 0 || state == 3);
}
}