diff --git a/core/libs/prx/libc/src/FileAndHeap.cpp b/core/libs/prx/libc/src/FileAndHeap.cpp index bb36a2ab6..7c15cffde 100644 --- a/core/libs/prx/libc/src/FileAndHeap.cpp +++ b/core/libs/prx/libc/src/FileAndHeap.cpp @@ -12,6 +12,7 @@ #include "prx/libc/include/FileStream.hpp" #include "prx/libc/include/ApplicationHeap.hpp" #include "prx/libc/include/General.hpp" +#include "prx/libc/include/GuestArena.hpp" static std::string NativeFileMode(const char* mode) { std::string result(mode); @@ -154,6 +155,11 @@ size_t APS5_VABI fread_nid_postfix(void* buffer, size_t size, size_t count, File auto* handle = GetNativeStream(stream); if (size == 0 || count == 0) return 0; if (!buffer) throw std::runtime_error("fread: null buffer"); + if (count > std::numeric_limits::max() / size) throw std::overflow_error("fread: buffer size overflow"); + const auto bytes = size * count; + if (bytes > std::numeric_limits::max() - reinterpret_cast(buffer)) throw std::overflow_error("fread: buffer address overflow"); + const GuestArena::HostWrite destination(buffer, bytes); + if (!destination.Open()) throw std::runtime_error("fread: buffer is not writable"); const auto result = std::fread(buffer, size, count, handle); stream->SyncStatus(); if (std::ferror(handle)) throw std::runtime_error("fread: read failed");