Fix PPMd properties state after rejected allocator changes

This commit is contained in:
Adam Hathcock
2026-11-13 09:27:51 +00:00
parent b76c336658
commit 09b95f1841
2 changed files with 20 additions and 2 deletions

View File

@@ -55,13 +55,14 @@ public class PpmdProperties
get => _allocatorSize;
set
{
_allocatorSize = value;
if (Version == PpmdVersion.I1)
{
_allocator ??= new Allocator();
_allocator.Start(_allocatorSize);
_allocator.Start(value);
}
// Keep the serialized properties in sync with the allocator if Start rejects the size.
_allocatorSize = value;
}
}

View File

@@ -154,6 +154,23 @@ public class DictionarySizeLimitTests : TestBase
Assert.Throws<InvalidFormatException>(() => new PpmdProperties(256 << 20, 6));
}
[Fact]
public void Ppmd_Properties_Preserve_Size_After_Rejected_Change()
{
using var limits = new DictionarySizeLimitScope();
DictionarySizeLimits.Ppmd = 2 << 20;
var properties = new PpmdProperties(1 << 20, 6);
var originalHeader = properties.Properties;
Assert.Throws<InvalidFormatException>(() => properties.AllocatorSize = 3 << 20);
Assert.Equal(1 << 20, properties.AllocatorSize);
Assert.Equal(originalHeader, properties.Properties);
properties.AllocatorSize = 2 << 20;
Assert.Equal(2 << 20, properties.AllocatorSize);
Assert.Equal(2 << 20, new PpmdProperties(properties.Properties).AllocatorSize);
}
[Theory]
[InlineData(false)]
[InlineData(true)]