diff --git a/src/SharpCompress/Compressors/PPMd/PpmdProperties.cs b/src/SharpCompress/Compressors/PPMd/PpmdProperties.cs index e4d52c17..708bdd36 100644 --- a/src/SharpCompress/Compressors/PPMd/PpmdProperties.cs +++ b/src/SharpCompress/Compressors/PPMd/PpmdProperties.cs @@ -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; } } diff --git a/tests/SharpCompress.Test/Streams/DictionarySizeLimitTests.cs b/tests/SharpCompress.Test/Streams/DictionarySizeLimitTests.cs index 6b5e853e..2619761b 100644 --- a/tests/SharpCompress.Test/Streams/DictionarySizeLimitTests.cs +++ b/tests/SharpCompress.Test/Streams/DictionarySizeLimitTests.cs @@ -154,6 +154,23 @@ public class DictionarySizeLimitTests : TestBase Assert.Throws(() => 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(() => 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)]