From 2263825591d1e7edb0f2c7ef8c6227a7282871b8 Mon Sep 17 00:00:00 2001 From: laqieer Date: Sat, 3 Jul 2021 00:25:24 +0800 Subject: [PATCH] fix: fix out of range detection --- src/gba_flash.c | 8 ++++---- src/gba_sram.c | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/gba_flash.c b/src/gba_flash.c index 737577b..02715d8 100644 --- a/src/gba_flash.c +++ b/src/gba_flash.c @@ -167,7 +167,7 @@ int flash_read(u32 addr, u8 *data, size_t size) { { int bank = 0; - if (addr > FLASH_SIZE * 2) + if (addr + size > FLASH_SIZE * 2) return E_OUT_OF_RANGE; if (addr >= FLASH_SIZE) @@ -179,7 +179,7 @@ int flash_read(u32 addr, u8 *data, size_t size) { flash_switch_bank(bank); } - if (addr > FLASH_SIZE) + if (addr + size > FLASH_SIZE) return E_OUT_OF_RANGE; flash_memcpy(data, &flash_mem[addr], size); @@ -214,7 +214,7 @@ int flash_write(u32 addr, u8 *data, size_t size) { { int bank = 0; - if (addr > FLASH_SIZE * 2) + if (addr + size > FLASH_SIZE * 2) return E_OUT_OF_RANGE; if (addr >= FLASH_SIZE) @@ -226,7 +226,7 @@ int flash_write(u32 addr, u8 *data, size_t size) { flash_switch_bank(bank); } - if (addr > FLASH_SIZE) + if (addr + size > FLASH_SIZE) return E_OUT_OF_RANGE; err = flash_erase(addr); diff --git a/src/gba_sram.c b/src/gba_sram.c index a8b143a..89340e3 100644 --- a/src/gba_sram.c +++ b/src/gba_sram.c @@ -29,7 +29,7 @@ int sram_read(u32 addr, u8 *data, size_t size) { if (addr > MEM_SRAM) addr -= MEM_SRAM; - if (addr > SRAM_SIZE) + if (addr + size > SRAM_SIZE) return E_OUT_OF_RANGE; sram_memcpy(data, &sram_mem[addr], size); @@ -44,7 +44,7 @@ int sram_write(u32 addr, u8 *data, size_t size) { if (addr > MEM_SRAM) addr -= MEM_SRAM; - if (addr > SRAM_SIZE) + if (addr + size > SRAM_SIZE) return E_OUT_OF_RANGE; sram_memcpy(&sram_mem[addr], data, size);