mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-12-05 17:11:09 +00:00
The libc.prx that titles ship (PPSA07923, PPSA05916) rejects a strerror_s capacity above SIZE_MAX >> 3, not SIZE_MAX >> 1, and calls the constraint handler with "strerror_s: bad argument", EINVAL and a null pointer for a null destination, a capacity of 0 or one past that limit. main passed "strerror_s" and the destination, and wrote the message for a capacity from 2^61 up. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
84 lines
2.7 KiB
C++
84 lines
2.7 KiB
C++
#include "prx/libc/include/WindowsFormatting.hpp"
|
|
#include "prx/libc/include/Api.hpp"
|
|
#include "prx/libc/include/general/VabiMacros.hpp"
|
|
#include "prx/libc/include/SonyCrt.hpp"
|
|
#include <cerrno>
|
|
#include <climits>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <limits>
|
|
#include <mutex>
|
|
|
|
extern "C" void APS5_VABI ignore_handler_s_nid_postfix(const char* message, void* pointer, int error) {
|
|
SonyIgnoreHandlerS(message, pointer, error);
|
|
}
|
|
|
|
extern "C" void* APS5_VABI set_constraint_handler_s_nid_postfix(void* handler) {
|
|
return SonySetConstraintHandlerS(handler);
|
|
}
|
|
|
|
extern "C" std::size_t APS5_VABI wcstombs_nid_postfix(char* destination, const std::uint16_t* source, std::size_t capacity) {
|
|
std::size_t count = 0;
|
|
for (;; ++source) {
|
|
char bytes[MB_LEN_MAX];
|
|
const std::size_t length = wcrtomb_nid_postfix(bytes, *source, nullptr);
|
|
for (std::size_t index = 0; index < length; ++index) {
|
|
if (destination && count == capacity) return count;
|
|
if (destination) destination[count] = bytes[index];
|
|
if (bytes[index] == '\0') return count;
|
|
++count;
|
|
}
|
|
}
|
|
}
|
|
|
|
extern "C" int APS5_VABI wcscpy_s_nid_postfix(std::uint16_t* dest, std::size_t size, const std::uint16_t* src) {
|
|
return SonyWcscpyS(dest, size, src);
|
|
}
|
|
|
|
namespace {
|
|
|
|
std::size_t ReadErrorMessage(int error, char (&message)[128]) {
|
|
strerror_r_nid_postfix(error, message, sizeof(message));
|
|
return std::strlen(message);
|
|
}
|
|
|
|
void ErrorMessageConstraint() {
|
|
SonyConstraintHandler handler;
|
|
{
|
|
std::lock_guard<std::mutex> lock(SonyConstraintMutex());
|
|
handler = SonyConstraintHandlerSlot();
|
|
}
|
|
if (handler == SonyDefaultConstraintHandler) {
|
|
SonyDefaultConstraintHandler("strerror_s: bad argument", nullptr, 22);
|
|
} else {
|
|
using GuestHandler = void (APS5_VABI *)(const char*, void*, int);
|
|
reinterpret_cast<GuestHandler>(handler)("strerror_s: bad argument", nullptr, 22);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extern "C" std::size_t APS5_VABI strerrorlen_s_nid_postfix(int error) {
|
|
char message[128];
|
|
return ReadErrorMessage(error, message);
|
|
}
|
|
|
|
extern "C" int APS5_VABI strerror_s_nid_postfix(char* buffer, std::size_t capacity, int error) {
|
|
if (!buffer || capacity == 0 || capacity > (std::numeric_limits<std::size_t>::max() >> 3)) {
|
|
ErrorMessageConstraint();
|
|
return 22;
|
|
}
|
|
char message[128];
|
|
const auto length = ReadErrorMessage(error, message);
|
|
if (length < capacity) {
|
|
std::memcpy(buffer, message, length + 1);
|
|
return 0;
|
|
}
|
|
std::memcpy(buffer, message, capacity - 1);
|
|
buffer[capacity - 1] = '\0';
|
|
if (capacity > 3) std::memcpy(buffer + capacity - 4, "...", 3);
|
|
return 0;
|
|
}
|