Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Free tmp_buf only when its needed #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions source/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down