From 0e75922c5b5429d3d611f9f90849cb049ab3aa9a Mon Sep 17 00:00:00 2001 From: Maniac- Date: Tue, 17 Oct 2023 20:25:09 +0300 Subject: [PATCH] Free tmp_buf only when its needed --- source/utils.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/source/utils.cpp b/source/utils.cpp index 34b2fb1..6a0f3f2 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -189,22 +189,24 @@ MochaUtilsStatus Mocha_IOSUMemoryRead(uint32_t address, uint8_t *out_buffer, uin ALIGN_0x40 uint32_t io_buf[0x40 >> 2]; io_buf[0] = address; - void *tmp_buf = nullptr; + void *tmp_buf = out_buffer; if (((uintptr_t) out_buffer & 0x3F) || (size & 0x3F)) { - tmp_buf = (uint32_t *) memalign(0x40, ROUNDUP(size, 0x40)); + tmp_buf = memalign(0x40, ROUNDUP(size, 0x40)); if (!tmp_buf) { return MOCHA_RESULT_OUT_OF_MEMORY; } } - int res = IOS_Ioctl(iosuhaxHandle, IOCTL_MEM_READ, io_buf, sizeof(address), tmp_buf ? tmp_buf : out_buffer, size); + int res = IOS_Ioctl(iosuhaxHandle, IOCTL_MEM_READ, io_buf, sizeof(address), tmp_buf, size); - if (res >= 0 && tmp_buf) { - memcpy(out_buffer, tmp_buf, size); + if (tmp_buf != out_buffer) { + if (res >= 0) { + memcpy(out_buffer, tmp_buf, size); + } + free(tmp_buf); } - free(tmp_buf); return res >= 0 ? MOCHA_RESULT_SUCCESS : MOCHA_RESULT_UNKNOWN_ERROR; } @@ -235,23 +237,25 @@ MochaUtilsStatus Mocha_IOSUKernelRead32(uint32_t address, uint32_t *out_buffer) ALIGN_0x40 uint32_t io_buf[0x40 >> 2]; io_buf[0] = address; - void *tmp_buf = NULL; + void *tmp_buf = out_buffer; int32_t count = 1; if (((uintptr_t) out_buffer & 0x3F) || ((count * 4) & 0x3F)) { - tmp_buf = (uint32_t *) memalign(0x40, ROUNDUP((count * 4), 0x40)); + tmp_buf = memalign(0x40, ROUNDUP((count * 4), 0x40)); if (!tmp_buf) { return MOCHA_RESULT_OUT_OF_MEMORY; } } - int res = IOS_Ioctl(iosuhaxHandle, IOCTL_KERN_READ32, io_buf, sizeof(address), tmp_buf ? tmp_buf : out_buffer, count * 4); + int res = IOS_Ioctl(iosuhaxHandle, IOCTL_KERN_READ32, io_buf, sizeof(address), tmp_buf, count * 4); - if (res >= 0 && tmp_buf) { - memcpy(out_buffer, tmp_buf, count * 4); + if (tmp_buf != out_buffer) { + if (res >= 0) { + memcpy(out_buffer, tmp_buf, count * 4); + } + free(tmp_buf); } - free(tmp_buf); return res >= 0 ? MOCHA_RESULT_SUCCESS : MOCHA_RESULT_UNKNOWN_ERROR; }