Skip to content

Commit 551f88e

Browse files
Maniac-Maschell
andauthored
Free tmp_buf only when its needed (#21)
Co-authored-by: Maschell <Maschell@gmx.de>
1 parent 0317b39 commit 551f88e

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

source/utils.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,22 +190,24 @@ MochaUtilsStatus Mocha_IOSUMemoryRead(uint32_t address, uint8_t *out_buffer, uin
190190
ALIGN_0x40 uint32_t io_buf[0x40 >> 2];
191191
io_buf[0] = address;
192192

193-
void *tmp_buf = nullptr;
193+
void *tmp_buf = out_buffer;
194194

195195
if (((uintptr_t) out_buffer & 0x3F) || (size & 0x3F)) {
196-
tmp_buf = (uint32_t *) memalign(0x40, ROUNDUP(size, 0x40));
196+
tmp_buf = memalign(0x40, ROUNDUP(size, 0x40));
197197
if (!tmp_buf) {
198198
return MOCHA_RESULT_OUT_OF_MEMORY;
199199
}
200200
}
201201

202-
int res = IOS_Ioctl(iosuhaxHandle, IOCTL_MEM_READ, io_buf, sizeof(address), tmp_buf ? tmp_buf : out_buffer, size);
202+
int res = IOS_Ioctl(iosuhaxHandle, IOCTL_MEM_READ, io_buf, sizeof(address), tmp_buf, size);
203203

204-
if (res >= 0 && tmp_buf) {
205-
memcpy(out_buffer, tmp_buf, size);
204+
if (tmp_buf != out_buffer) {
205+
if (res >= 0) {
206+
memcpy(out_buffer, tmp_buf, size);
207+
}
208+
free(tmp_buf);
206209
}
207210

208-
free(tmp_buf);
209211
return res >= 0 ? MOCHA_RESULT_SUCCESS : MOCHA_RESULT_UNKNOWN_ERROR;
210212
}
211213

@@ -236,23 +238,25 @@ MochaUtilsStatus Mocha_IOSUKernelRead32(uint32_t address, uint32_t *out_buffer)
236238
ALIGN_0x40 uint32_t io_buf[0x40 >> 2];
237239
io_buf[0] = address;
238240

239-
void *tmp_buf = NULL;
241+
void *tmp_buf = out_buffer;
240242
int32_t count = 1;
241243

242244
if (((uintptr_t) out_buffer & 0x3F) || ((count * 4) & 0x3F)) {
243-
tmp_buf = (uint32_t *) memalign(0x40, ROUNDUP((count * 4), 0x40));
245+
tmp_buf = memalign(0x40, ROUNDUP((count * 4), 0x40));
244246
if (!tmp_buf) {
245247
return MOCHA_RESULT_OUT_OF_MEMORY;
246248
}
247249
}
248250

249-
int res = IOS_Ioctl(iosuhaxHandle, IOCTL_KERN_READ32, io_buf, sizeof(address), tmp_buf ? tmp_buf : out_buffer, count * 4);
251+
int res = IOS_Ioctl(iosuhaxHandle, IOCTL_KERN_READ32, io_buf, sizeof(address), tmp_buf, count * 4);
250252

251-
if (res >= 0 && tmp_buf) {
252-
memcpy(out_buffer, tmp_buf, count * 4);
253+
if (tmp_buf != out_buffer) {
254+
if (res >= 0) {
255+
memcpy(out_buffer, tmp_buf, count * 4);
256+
}
257+
free(tmp_buf);
253258
}
254259

255-
free(tmp_buf);
256260
return res >= 0 ? MOCHA_RESULT_SUCCESS : MOCHA_RESULT_UNKNOWN_ERROR;
257261
}
258262

0 commit comments

Comments
 (0)