Fix potential crash in savestate loading

Also throw when attempting to create such an invalid savestate. No idea yet how that can happen but apparently it can.
This commit is contained in:
Morilli 2024-06-22 01:41:01 +02:00
parent aeb80e5810
commit 6364475b21
2 changed files with 13 additions and 0 deletions

View File

@ -135,6 +135,12 @@ namespace BizHawk.Client.Common
stateFrame = newLog.Count; // In case the frame count failed to parse, revert to using the entire state input log
}
if (stateFrame > newLog.Count)
{
errorMessage = $"Savestate has invalid frame number {stateFrame} (expected maximum {newLog.Count})";
return false;
}
if (Log.Count < stateFrame)
{
if (this.IsFinished())

View File

@ -104,6 +104,13 @@ namespace BizHawk.Client.Common
bs.PutLump(BinaryStateLump.Input,
tw =>
{
// TODO: this should not happen and no exception should be thrown here.
// Just make this noisy for now until the issue is fixed.
if (_movieSession.Movie.FrameCount < _emulator.Frame)
{
throw new InvalidOperationException(
$"Tried to create a savestate at frame {_emulator.Frame}, but only got a log of length {_movieSession.Movie.FrameCount}!");
}
// this never should have been a core's responsibility
tw.WriteLine("Frame {0}", _emulator.Frame);
_movieSession.HandleSaveState(tw);