Skip to content

Commit 5943390

Browse files
JoeMattclaude
andcommitted
Address review feedback on save state implementation
- Zero-fill remaining bytes in retro_serialize() for deterministic save states and add bounds check against STATE_SIZE overflow - Include eeprom_ram[64] and cdromEEPROM[64] in EepromStateSave/Load so EEPROM contents are preserved across save/load cycles - Include audioEnabled and joysticksEnabled flags in JoystickStateSave/Load so derived state from joystick_ram writes is not stale after load Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent b408304 commit 5943390

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

libretro.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,13 +782,15 @@ size_t retro_serialize_size(void)
782782

783783
bool retro_serialize(void *data, size_t size)
784784
{
785-
uint8_t *buf;
785+
uint8_t *buf, *start;
786+
size_t written;
786787
uint32_t magic, version, flags, reserved;
787788

788789
if (!data || size < STATE_SIZE)
789790
return false;
790791

791-
buf = (uint8_t *)data;
792+
start = (uint8_t *)data;
793+
buf = start;
792794

793795
/* Header */
794796
magic = STATE_MAGIC;
@@ -825,6 +827,14 @@ bool retro_serialize(void *data, size_t size)
825827
buf += MTStateSave(buf);
826828
buf += DACStateSave(buf);
827829

830+
written = (size_t)(buf - start);
831+
if (written > STATE_SIZE)
832+
return false;
833+
834+
/* Zero-fill remaining bytes for deterministic save states */
835+
if (written < STATE_SIZE)
836+
memset(buf, 0, STATE_SIZE - written);
837+
828838
return true;
829839
}
830840

src/eeprom.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,10 @@ size_t EepromStateSave(uint8_t *buf)
451451
STATE_SAVE_VAR(buf, jerry_writes_enabled);
452452
STATE_SAVE_VAR(buf, jerry_ee_direct_jump);
453453

454+
/* EEPROM data arrays */
455+
STATE_SAVE_BUF(buf, eeprom_ram, sizeof(eeprom_ram));
456+
STATE_SAVE_BUF(buf, cdromEEPROM, sizeof(cdromEEPROM));
457+
454458
return (size_t)(buf - start);
455459
}
456460

@@ -468,6 +472,10 @@ size_t EepromStateLoad(const uint8_t *buf)
468472
STATE_LOAD_VAR(buf, jerry_writes_enabled);
469473
STATE_LOAD_VAR(buf, jerry_ee_direct_jump);
470474

475+
/* EEPROM data arrays */
476+
STATE_LOAD_BUF(buf, eeprom_ram, sizeof(eeprom_ram));
477+
STATE_LOAD_BUF(buf, cdromEEPROM, sizeof(cdromEEPROM));
478+
471479
return (size_t)(buf - start);
472480
}
473481

src/joystick.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ size_t JoystickStateSave(uint8_t *buf)
161161
STATE_SAVE_BUF(buf, joystick_ram, sizeof(joystick_ram));
162162
STATE_SAVE_BUF(buf, joypad0Buttons, sizeof(joypad0Buttons));
163163
STATE_SAVE_BUF(buf, joypad1Buttons, sizeof(joypad1Buttons));
164+
STATE_SAVE_VAR(buf, audioEnabled);
165+
STATE_SAVE_VAR(buf, joysticksEnabled);
164166

165167
return (size_t)(buf - start);
166168
}
@@ -172,6 +174,8 @@ size_t JoystickStateLoad(const uint8_t *buf)
172174
STATE_LOAD_BUF(buf, joystick_ram, sizeof(joystick_ram));
173175
STATE_LOAD_BUF(buf, joypad0Buttons, sizeof(joypad0Buttons));
174176
STATE_LOAD_BUF(buf, joypad1Buttons, sizeof(joypad1Buttons));
177+
STATE_LOAD_VAR(buf, audioEnabled);
178+
STATE_LOAD_VAR(buf, joysticksEnabled);
175179

176180
return (size_t)(buf - start);
177181
}

0 commit comments

Comments
 (0)