diff --git a/Common/Data/Encoding/Utf8.cpp b/Common/Data/Encoding/Utf8.cpp index 73c48c4819..09abf6090d 100644 --- a/Common/Data/Encoding/Utf8.cpp +++ b/Common/Data/Encoding/Utf8.cpp @@ -312,14 +312,19 @@ size_t encode_utf8_modified(uint32_t code_point, unsigned char* output) { // A function to convert regular UTF-8 to Java Modified UTF-8. Only used on Android. // Written by ChatGPT and corrected and modified. void ConvertUTF8ToJavaModifiedUTF8(std::string *output, std::string_view input) { + // The overflow can't really happen on 64-bit, but let's do the check anyway. + if (input.length() > SIZE_MAX / 6) { + output->clear(); + return; + } output->resize(input.length() * 6); // worst case: every input character is encoded as 6 bytes. Can't really plausibly happen, though. size_t out_idx = 0; for (size_t i = 0; i < input.length(); ) { unsigned char c = input[i]; if (c == 0) { // Encode null character as 0xC0 0x80. TODO: We probably don't need to support this? - output[out_idx++] = (char)0xC0; - output[out_idx++] = (char)0x80; + (*output)[out_idx++] = (char)0xC0; + (*output)[out_idx++] = (char)0x80; i++; } else if ((c & 0xF0) == 0xF0) { // 4-byte sequence (U+10000 to U+10FFFF) if (i + 4 > input.length()) { diff --git a/Core/HLE/sceKernelModule.cpp b/Core/HLE/sceKernelModule.cpp index 82f7b989f8..c81607eba7 100644 --- a/Core/HLE/sceKernelModule.cpp +++ b/Core/HLE/sceKernelModule.cpp @@ -1096,8 +1096,8 @@ static PSPModule *__KernelLoadELFFromPtr(const u8 *ptr, size_t elfSize, u32 load // In this case it's definitely not compressed. Added assert below. } - // Don't accept ELFs over 24MB. - if (decryptedSize > 24 * 1024 * 1024) { + // Don't accept ELFs over 24MB - nor ones with negative size, of course. + if (decryptedSize < 0 || decryptedSize > 24 * 1024 * 1024) { *error_string = StringFromFormat("ELF/PRX corrupt, unreasonable decrypted size: %d", (u32)decryptedSize); // TODO: Might be the wrong error code. error = SCE_KERNEL_ERROR_FILEERR; @@ -1110,7 +1110,7 @@ static PSPModule *__KernelLoadELFFromPtr(const u8 *ptr, size_t elfSize, u32 load // Can't decompress in place so we need a temporary buffer. u8 *temp = (u8 *)malloc(decryptedSize); - _assert_msg_(temp != nullptr, "Failed to allocate gzip decompression buffer"); + _assert_msg_(temp != nullptr, "Failed to allocate gzip decompression buffer (decryptedSize: %d)", decryptedSize); memcpy(temp, ptr, decryptedSize); int outBytes = gzipDecompress((u8 *)ptr, maxElfSize, temp); if (outBytes < 0) {