Skip to content

Commit

Permalink
fix: fix out of range detection
Browse files Browse the repository at this point in the history
  • Loading branch information
laqieer committed Jul 2, 2021
1 parent 8053a4d commit 2263825
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/gba_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/gba_sram.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 2263825

Please sign in to comment.