Skip to content

Commit

Permalink
Merge pull request #1076 from Rosalie241/rom-size-check
Browse files Browse the repository at this point in the history
Correct ROM file size checks
  • Loading branch information
richard42 authored May 7, 2024
2 parents db5fb1a + d5f1267 commit c99a25d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/api/frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ EXPORT m64p_error CALL CoreDoCommand(m64p_command Command, int ParamInt, void *P
if (g_EmulatorRunning || l_DiskOpen || l_ROMOpen)
return M64ERR_INVALID_STATE;
// ROM buffer size must be divisible by 4 to avoid out-of-bounds read in swap_copy_rom (v64/n64 formats)
if (ParamPtr == NULL || ParamInt < 4096 || ParamInt > CART_ROM_MAX_SIZE || ParamInt % 4 != 0)
if (ParamPtr == NULL || ParamInt < 4096 || ParamInt > CART_ROM_MAX_SIZE)
return M64ERR_INPUT_ASSERT;
rval = open_rom((const unsigned char *) ParamPtr, ParamInt);
if (rval == M64ERR_SUCCESS)
Expand Down
12 changes: 6 additions & 6 deletions src/main/rom.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ static const uint8_t Z64_SIGNATURE[4] = { 0x80, 0x37, 0x12, 0x40 };
static const uint8_t V64_SIGNATURE[4] = { 0x37, 0x80, 0x40, 0x12 };
static const uint8_t N64_SIGNATURE[4] = { 0x40, 0x12, 0x37, 0x80 };

/* Tests if a file is a valid N64 rom by checking the first 4 bytes. */
static int is_valid_rom(const unsigned char *buffer)
/* Tests if a file is a valid N64 rom by checking the first 4 bytes and size */
static int is_valid_rom(const unsigned char *buffer, unsigned int size)
{
if (memcmp(buffer, Z64_SIGNATURE, sizeof(Z64_SIGNATURE)) == 0
|| memcmp(buffer, V64_SIGNATURE, sizeof(V64_SIGNATURE)) == 0
|| memcmp(buffer, N64_SIGNATURE, sizeof(N64_SIGNATURE)) == 0)
if ((memcmp(buffer, Z64_SIGNATURE, sizeof(Z64_SIGNATURE)) == 0)
|| (memcmp(buffer, V64_SIGNATURE, sizeof(V64_SIGNATURE)) == 0 && size % 2 == 0)
|| (memcmp(buffer, N64_SIGNATURE, sizeof(N64_SIGNATURE)) == 0 && size % 4 == 0))
return 1;
else
return 0;
Expand Down Expand Up @@ -146,7 +146,7 @@ m64p_error open_rom(const unsigned char* romimage, unsigned int size)
int i;

/* check input requirements */
if (romimage == NULL || !is_valid_rom(romimage))
if (romimage == NULL || !is_valid_rom(romimage, size))
{
DebugMessage(M64MSG_ERROR, "open_rom(): not a valid ROM image");
return M64ERR_INPUT_INVALID;
Expand Down

0 comments on commit c99a25d

Please sign in to comment.