From 5860e0e9372f89f4fc32b225c3bd7b7de91b78c1 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Wed, 13 Oct 2021 13:19:15 -0400 Subject: [PATCH] Disallow negative sizes in SAVEBLOCK_CHUNK --- src/save.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/save.c b/src/save.c index 52301df036..a05d2c184c 100644 --- a/src/save.c +++ b/src/save.c @@ -43,11 +43,12 @@ static u8 HandleWriteSector(u16 a1, const struct SaveSectionLocation *location); // (u8 *)structure was removed from the first statement of the macro in Emerald. // This is because malloc is used to allocate addresses so storing the raw // addresses should not be done in the offsets information. -#define SAVEBLOCK_CHUNK(structure, chunkNum) \ -{ \ - chunkNum * SECTOR_DATA_SIZE, \ - min(sizeof(structure) - chunkNum * SECTOR_DATA_SIZE, SECTOR_DATA_SIZE) \ -} \ +#define SAVEBLOCK_CHUNK(structure, chunkNum) \ +{ \ + chunkNum * SECTOR_DATA_SIZE, \ + sizeof(structure) >= chunkNum * SECTOR_DATA_SIZE ? \ + min(sizeof(structure) - chunkNum * SECTOR_DATA_SIZE, SECTOR_DATA_SIZE) : 0 \ +} static const struct SaveSectionOffsets sSaveSectionOffsets[] = {