From 6e6b86eabca0a7c0b9b9777267cc0276fea2844a Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sun, 13 Mar 2022 03:30:17 -0700 Subject: [PATCH] strat: minor result/overflow check fixes --- .../include/stratosphere/fs/fsa/fs_ifile.hpp | 20 ++++++++----------- .../time/impl/util/time_impl_util_api.cpp | 6 +----- .../source/sysupdater/sysupdater_fs_utils.cpp | 4 ++-- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/libraries/libstratosphere/include/stratosphere/fs/fsa/fs_ifile.hpp b/libraries/libstratosphere/include/stratosphere/fs/fsa/fs_ifile.hpp index 9ca61db0d..687751800 100644 --- a/libraries/libstratosphere/include/stratosphere/fs/fsa/fs_ifile.hpp +++ b/libraries/libstratosphere/include/stratosphere/fs/fsa/fs_ifile.hpp @@ -36,12 +36,10 @@ namespace ams::fs::fsa { } /* Check that the read is valid. */ - R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument()); - R_UNLESS(offset >= 0, fs::ResultOutOfRange()); - R_UNLESS(util::IsIntValueRepresentable(size), fs::ResultOutOfRange()); - - const s64 signed_size = static_cast(size); - R_UNLESS((std::numeric_limits::max() - offset) >= signed_size, fs::ResultOutOfRange()); + R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument()); + R_UNLESS(offset >= 0, fs::ResultOutOfRange()); + R_UNLESS(util::IsIntValueRepresentable(size), fs::ResultOutOfRange()); + R_UNLESS(util::CanAddWithoutOverflow(offset, size), fs::ResultOutOfRange()); /* Do the read. */ R_RETURN(this->DoRead(out, offset, buffer, size, option)); @@ -68,12 +66,10 @@ namespace ams::fs::fsa { } /* Check the write is valid. */ - R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument()); - R_UNLESS(offset >= 0, fs::ResultOutOfRange()); - R_UNLESS(util::IsIntValueRepresentable(size), fs::ResultOutOfRange()); - - const s64 signed_size = static_cast(size); - R_UNLESS((std::numeric_limits::max() - offset) >= signed_size, fs::ResultOutOfRange()); + R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument()); + R_UNLESS(offset >= 0, fs::ResultOutOfRange()); + R_UNLESS(util::IsIntValueRepresentable(size), fs::ResultOutOfRange()); + R_UNLESS(util::CanAddWithoutOverflow(offset, size), fs::ResultOutOfRange()); R_RETURN(this->DoWrite(offset, buffer, size, option)); } diff --git a/libraries/libstratosphere/source/time/impl/util/time_impl_util_api.cpp b/libraries/libstratosphere/source/time/impl/util/time_impl_util_api.cpp index 8cb8f961e..3226db2b4 100644 --- a/libraries/libstratosphere/source/time/impl/util/time_impl_util_api.cpp +++ b/libraries/libstratosphere/source/time/impl/util/time_impl_util_api.cpp @@ -157,11 +157,7 @@ namespace ams::time::impl::util { R_UNLESS(out != nullptr, time::ResultInvalidPointer()); R_UNLESS(from.source_id == to.source_id, time::ResultNotComparable()); - const bool no_overflow = (from.value >= 0 ? (to.value >= std::numeric_limits::min() + from.value) - : (to.value <= std::numeric_limits::max() + from.value)); - R_UNLESS(no_overflow, time::ResultOverflowed()); - - *out = to.value - from.value; + R_UNLESS(ams::util::TrySubtractWithoutOverflow(out, to.value, from.value), time::ResultOverflowed()); return ResultSuccess(); } diff --git a/stratosphere/ams_mitm/source/sysupdater/sysupdater_fs_utils.cpp b/stratosphere/ams_mitm/source/sysupdater/sysupdater_fs_utils.cpp index 722c895ee..c78e43add 100644 --- a/stratosphere/ams_mitm/source/sysupdater/sysupdater_fs_utils.cpp +++ b/stratosphere/ams_mitm/source/sysupdater/sysupdater_fs_utils.cpp @@ -123,7 +123,7 @@ namespace ams::mitm::sysupdater { /* Open the file storage. */ std::shared_ptr file_storage = fssystem::AllocateShared(); - R_UNLESS(file_storage != nullptr, fs::ResultAllocationMemoryFailedInFileSystemProxyCoreImplD()); + R_UNLESS(file_storage != nullptr, fs::ResultAllocationMemoryFailedInNcaFileSystemServiceImplA()); R_TRY(file_storage->Initialize(std::move(base_fs), nsp_path, ams::fs::OpenMode_Read)); /* Create a partition fs. */ @@ -138,7 +138,7 @@ namespace ams::mitm::sysupdater { Result ParseNca(const char **path, std::shared_ptr *out, std::shared_ptr base_fs) { /* Open the file storage. */ std::shared_ptr file_storage = fssystem::AllocateShared(); - R_UNLESS(file_storage != nullptr, fs::ResultAllocationMemoryFailedInFileSystemProxyCoreImplE()); + R_UNLESS(file_storage != nullptr, fs::ResultAllocationMemoryFailedInNcaFileSystemServiceImplB()); /* Get the nca path. */ ams::fs::Path nca_path;