From 29153af2bcea395ddc1ebf60b88655bfd8630529 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Thu, 6 Dec 2018 23:25:16 -0800 Subject: [PATCH] tma.tio: Improve read/write bounding. --- .../tma/source/target_io/tio_task.cpp | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/stratosphere/tma/source/target_io/tio_task.cpp b/stratosphere/tma/source/target_io/tio_task.cpp index 528efeb99..a8090bd0a 100644 --- a/stratosphere/tma/source/target_io/tio_task.cpp +++ b/stratosphere/tma/source/target_io/tio_task.cpp @@ -27,12 +27,22 @@ void TIOFileReadTask::OnStart(TmaPacket *packet) { packet->Read(this->size_remaining); packet->Read(this->cur_offset); + Result rc = 0; if (strlen(path) == 0) { - this->SendResult(0x202); - return; + rc = 0x202; } - Result rc = dmntTargetIOFileOpen(&this->handle, path, FS_OPEN_READ, DmntTIOCreateOption_OpenExisting); + if (R_SUCCEEDED(rc)) { + u64 file_size; + rc = dmntTargetIOFileGetSize(path, &file_size); + if (R_SUCCEEDED(rc)) { + if (file_size < this->cur_offset + this->size_remaining) { + this->size_remaining = file_size - this->cur_offset; + } + } + } + + rc = dmntTargetIOFileOpen(&this->handle, path, FS_OPEN_READ, DmntTIOCreateOption_OpenExisting); if (R_FAILED(rc)) { this->SendResult(rc); return; @@ -101,12 +111,23 @@ void TIOFileWriteTask::OnStart(TmaPacket *packet) { packet->Read(this->size_remaining); packet->Read(this->cur_offset); + Result rc = 0; if (strlen(path) == 0) { - this->SendResult(0x202); - return; + rc = 0x202; } - Result rc = dmntTargetIOFileOpen(&this->handle, path, FS_OPEN_READ, DmntTIOCreateOption_OpenExisting); + if (R_SUCCEEDED(rc)) { + u64 file_size; + rc = dmntTargetIOFileGetSize(path, &file_size); + if (R_SUCCEEDED(rc)) { + if (file_size < this->cur_offset + this->size_remaining) { + this->size_remaining = file_size - this->cur_offset; + } + } + } + + rc = dmntTargetIOFileOpen(&this->handle, path, FS_OPEN_READ, DmntTIOCreateOption_OpenExisting); + if (R_FAILED(rc)) { this->SendResult(rc); }