Fix crash saving zwinder state manager when things align a certain way

This commit is contained in:
nattthebear 2020-09-23 22:03:37 -04:00
parent 276e8ab185
commit 757ac4e27e
2 changed files with 25 additions and 1 deletions

View File

@ -256,7 +256,7 @@ namespace BizHawk.Client.Common
if (Count != 0)
{
var startByte = _states[_firstStateIndex].Start;
var endByte = _states[HeadStateIndex].Start + _states[HeadStateIndex].Size;
var endByte = (_states[HeadStateIndex].Start + _states[HeadStateIndex].Size) & _sizeMask;
// TODO: Use spans to avoid these extra copies in .net core
if (startByte > endByte)
{

View File

@ -418,6 +418,30 @@ namespace BizHawk.Tests.Client.Common.Movie
Assert.AreEqual(0, zw.AllStates().Single().Frame);
}
[TestMethod]
public void WhatIfTheHeadStateWrapsAround()
{
var ss = new StateSource
{
PaddingData = new byte[400 * 1000]
};
using var zw = new ZwinderBuffer(new RewindConfig
{
BufferSize = 1,
TargetFrameLength = 1
});
// Need to get data in the zwinderbuffer so that the last state, and the last state in particular, wraps around
ss.Frame = 1;
zw.Capture(1, s => ss.SaveStateBinary(new BinaryWriter(s)), null, true);
ss.Frame = 2;
zw.Capture(2, s => ss.SaveStateBinary(new BinaryWriter(s)), null, true);
ss.Frame = 3;
zw.Capture(3, s => ss.SaveStateBinary(new BinaryWriter(s)), null, true);
zw.SaveStateBinary(new BinaryWriter(new MemoryStream()));
}
private class StateSource : IStatable
{
public int Frame { get; set; }