From 72bd7c4532046937e9354863d92ffd7334e56b02 Mon Sep 17 00:00:00 2001 From: Jesse Talavera-Greenberg Date: Wed, 13 Dec 2023 13:20:27 -0500 Subject: [PATCH 1/2] Simplify the SRAM's representation in `NDSCartArgs` - I overthought this one. - I could've just checked `args && args->SRAM`, but then some other poor bastard might make this mistake. - Don't mix `pair`, `optional`, and `unique_ptr` all at once, kids. --- src/NDSCart.cpp | 3 ++- src/NDSCart.h | 9 +++++++-- src/frontend/qt_sdl/ROMManager.cpp | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/NDSCart.cpp b/src/NDSCart.cpp index c0e1c5ff87..1d2d0e6729 100644 --- a/src/NDSCart.cpp +++ b/src/NDSCart.cpp @@ -1642,7 +1642,8 @@ std::unique_ptr ParseROM(std::unique_ptr&& romdata, u32 romlen } std::unique_ptr cart; - auto [sram, sramlen] = args ? std::move(*args->SRAM) : std::make_pair(nullptr, 0); + std::unique_ptr sram = args ? std::move(args->SRAM) : nullptr; + u32 sramlen = args ? args->SRAMLength : 0; if (homebrew) cart = std::make_unique(std::move(cartrom), cartromsize, cartid, romparams, args ? std::move(args->SDCard) : std::nullopt); else if (cartid & 0x08000000) diff --git a/src/NDSCart.h b/src/NDSCart.h index dcbc1eb27c..a38e3bcf55 100644 --- a/src/NDSCart.h +++ b/src/NDSCart.h @@ -61,9 +61,14 @@ struct NDSCartArgs std::optional SDCard = std::nullopt; /// Save RAM to load into the cartridge. - /// If \c nullopt, then the cart's SRAM buffer will be empty. + /// If \c nullptr, then the cart's SRAM buffer will be empty. /// Ignored for homebrew ROMs. - std::optional, u32>> SRAM = std::nullopt; + std::unique_ptr SRAM = nullptr; + + /// The length of the buffer in SRAM. + /// If 0, then the cart's SRAM buffer will be empty. + /// Ignored for homebrew ROMs. + u32 SRAMLength = 0; }; // CartCommon -- base code shared by all cart types diff --git a/src/frontend/qt_sdl/ROMManager.cpp b/src/frontend/qt_sdl/ROMManager.cpp index b065ad1a94..a20af2069d 100644 --- a/src/frontend/qt_sdl/ROMManager.cpp +++ b/src/frontend/qt_sdl/ROMManager.cpp @@ -1316,8 +1316,8 @@ bool LoadROM(EmuThread* emuthread, QStringList filepath, bool reset) // the ROM is homebrew or not. // So this is the card we *would* load if the ROM were homebrew. .SDCard = GetDLDISDCardArgs(), - - .SRAM = std::make_pair(std::move(savedata), savelen), + .SRAM = std::move(savedata), + .SRAMLength = savelen, }; auto cart = NDSCart::ParseROM(std::move(filedata), filelen, std::move(cartargs)); From d3a83ac9e8b26210aff82b92bba7143a7c5dd048 Mon Sep 17 00:00:00 2001 From: Jesse Talavera-Greenberg Date: Fri, 15 Dec 2023 08:27:55 -0500 Subject: [PATCH 2/2] Fix a `nullptr` read --- src/NDSCart.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/NDSCart.cpp b/src/NDSCart.cpp index fe2e3fa293..474a47f96b 100644 --- a/src/NDSCart.cpp +++ b/src/NDSCart.cpp @@ -404,7 +404,11 @@ CartRetail::CartRetail(std::unique_ptr&& rom, u32 len, u32 chipid, bool ba { // Copy in what we can, truncate the rest. SRAM = std::make_unique(SRAMLength); memset(SRAM.get(), 0xFF, SRAMLength); - memcpy(SRAM.get(), sram.get(), std::min(sramlen, SRAMLength)); + + if (sram) + { // If we have anything to copy, that is. + memcpy(SRAM.get(), sram.get(), std::min(sramlen, SRAMLength)); + } } }