From 4df2b49643b8e026aa701005bcc9185e793f3fcd Mon Sep 17 00:00:00 2001 From: Morilli <35152647+Morilli@users.noreply.github.com> Date: Thu, 27 Feb 2025 21:54:06 +0100 Subject: [PATCH] add error check in ines_load --- source/quickerNES/core/cart.hpp | 7 ++++++- source/quickerNES/core/emu.cpp | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/source/quickerNES/core/cart.hpp b/source/quickerNES/core/cart.hpp index ccd61f4..75d025c 100644 --- a/source/quickerNES/core/cart.hpp +++ b/source/quickerNES/core/cart.hpp @@ -39,7 +39,7 @@ class Cart static_assert(sizeof(ines_header_t) == 16); // Load iNES file - void load_ines(const uint8_t *buffer) + const char *load_ines(const uint8_t *buffer) { ines_header_t h; @@ -50,6 +50,9 @@ class Cart bufferPos += copySize; } + if (memcmp(h.signature, "NES\x1A", 4) != 0) + return "Not an iNES file"; + set_mapper(h.flags, h.flags2); // skip trainer @@ -73,6 +76,8 @@ class Cart memcpy(chr(), &buffer[bufferPos], copySize); bufferPos += copySize; } + + return nullptr; } inline bool has_battery_ram() const { return mapper & 0x02; } diff --git a/source/quickerNES/core/emu.cpp b/source/quickerNES/core/emu.cpp index a706be2..751b12a 100644 --- a/source/quickerNES/core/emu.cpp +++ b/source/quickerNES/core/emu.cpp @@ -172,7 +172,9 @@ const char *Emu::emulate_frame(uint32_t joypad1, uint32_t joypad2, uint32_t arka const char *Emu::load_ines(const uint8_t *buffer) { - private_cart.load_ines(buffer); + const char *error = private_cart.load_ines(buffer); + if (error) return error; + return set_cart(&private_cart); } @@ -842,4 +844,4 @@ void Emu::RestoreAudioBufferState() sound_buf->RestoreAudioBufferState(); } -} // namespace quickerNES \ No newline at end of file +} // namespace quickerNES