From bed872ed38e19d34c6c2e3d1a3d35a9f72e46970 Mon Sep 17 00:00:00 2001
From: Lioncash <mathew1800@gmail.com>
Date: Sun, 14 Oct 2018 21:41:58 -0400
Subject: nso: Return an optional address from LoadModule

If a malformed NSO is attempted to be loaded, we shouldn't continue
onwards. We should be reporting an error and bailing out.
---
 src/core/loader/nso.cpp | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

(limited to 'src/core/loader/nso.cpp')

diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp
index d26fa482c7..68efca5c0d 100644
--- a/src/core/loader/nso.cpp
+++ b/src/core/loader/nso.cpp
@@ -93,9 +93,9 @@ static constexpr u32 PageAlignSize(u32 size) {
     return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK;
 }
 
-VAddr AppLoader_NSO::LoadModule(const FileSys::VfsFile& file, VAddr load_base,
-                                bool should_pass_arguments,
-                                boost::optional<FileSys::PatchManager> pm) {
+std::optional<VAddr> AppLoader_NSO::LoadModule(const FileSys::VfsFile& file, VAddr load_base,
+                                               bool should_pass_arguments,
+                                               std::optional<FileSys::PatchManager> pm) {
     if (file.GetSize() < sizeof(NsoHeader))
         return {};
 
@@ -154,7 +154,7 @@ VAddr AppLoader_NSO::LoadModule(const FileSys::VfsFile& file, VAddr load_base,
     program_image.resize(image_size);
 
     // Apply patches if necessary
-    if (pm != boost::none && pm->HasNSOPatch(nso_header.build_id)) {
+    if (pm && pm->HasNSOPatch(nso_header.build_id)) {
         std::vector<u8> pi_header(program_image.size() + 0x100);
         std::memcpy(pi_header.data(), &nso_header, sizeof(NsoHeader));
         std::memcpy(pi_header.data() + 0x100, program_image.data(), program_image.size());
@@ -181,7 +181,9 @@ ResultStatus AppLoader_NSO::Load(Kernel::Process& process) {
 
     // Load module
     const VAddr base_address = process.VMManager().GetCodeRegionBaseAddress();
-    LoadModule(*file, base_address, true);
+    if (!LoadModule(*file, base_address, true)) {
+        return ResultStatus::ErrorLoadingNSO;
+    }
     LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), base_address);
 
     process.Run(base_address, Kernel::THREADPRIO_DEFAULT, Memory::DEFAULT_STACK_SIZE);
-- 
cgit v1.2.3-70-g09d2