diff --git a/source/Handlebars.Test/BindingContextPoolTests.cs b/source/Handlebars.Test/BindingContextPoolTests.cs new file mode 100644 index 00000000..e1700cb9 --- /dev/null +++ b/source/Handlebars.Test/BindingContextPoolTests.cs @@ -0,0 +1,38 @@ +using System.Collections.Generic; +using Xunit; + +namespace HandlebarsDotNet.Test +{ + [CollectionDefinition(nameof(BindingContextPoolTests), DisableParallelization = true)] + public class BindingContextPoolCollection + { + } + + // Binding contexts are pooled process-wide and shared by every IHandlebars instance. A value one + // render binds must never be visible to a later render, however many renders come between. + [Collection(nameof(BindingContextPoolTests))] + public class BindingContextPoolTests + { + [Fact] + public void BlockParamBoundInOneRenderIsNotSeenByLaterRenders() + { + var first = Handlebars.Create().Compile("{{#each secrets as |x|}}{{/each}}"); + // `x` is read in two nested frames, so whichever pooled context holds the earlier + // binding, it is read wherever that context lands except as the root + var second = Handlebars.Create().Compile("{{#with this as |y|}}{{#with this as |z|}}[{{x}}]{{/with}}[{{x}}]{{/with}}"); + + // A context returned at a version that is a multiple of 10 is fully cleared, so bind `x` + // in several renders to leave it in more than one pooled context + for (var i = 0; i < 20; i++) first(new { secrets = new[] { "from the first render" } }); + + var leaked = new List(); + for (var i = 0; i < 100000 && leaked.Count < 5; i++) + { + var output = second(new { x = "own value" }); + if (output != "[own value][own value]") leaked.Add(i + ": " + output); + } + + Assert.Empty(leaked); + } + } +} diff --git a/source/Handlebars.Test/Collections/FixedSizeDictionaryTests.cs b/source/Handlebars.Test/Collections/FixedSizeDictionaryTests.cs index 3c0b2cb3..5ee73046 100644 --- a/source/Handlebars.Test/Collections/FixedSizeDictionaryTests.cs +++ b/source/Handlebars.Test/Collections/FixedSizeDictionaryTests.cs @@ -284,6 +284,35 @@ public void CanAddAfterClear() } } + [Theory] + [InlineData(nameof(FixedSizeDictionary>.Reset))] + [InlineData(nameof(FixedSizeDictionary>.OptionalClear))] + public void EntryFromAnEarlierUseStaysGoneOnceTheVersionWraps(string clear) + { + var dictionary = new FixedSizeDictionary(16, 7, new StringComparerAdapter()); + dictionary.AddOrReplace("earlier", "earlier value", out _); + + // The version is a byte: 255 resets bring it back to the value it had when "earlier" was added + for (var i = 0; i < 255; i++) + { + if (clear == nameof(dictionary.Reset)) dictionary.Reset(); + else dictionary.OptionalClear(); + } + + dictionary.AddOrReplace("current", "current value", out _); + + Assert.False(dictionary.TryGetValue("earlier", out var value), $"found \"{value}\" from before the resets"); + Assert.False(dictionary.ContainsKey("earlier")); + Assert.Single(dictionary); + } + + private readonly struct StringComparerAdapter : IEqualityComparer + { + public bool Equals(string? x, string? y) => string.Equals(x, y, StringComparison.Ordinal); + + public int GetHashCode(string obj) => StringComparer.Ordinal.GetHashCode(obj); + } + public void Dispose() { FixedSizeDictionary.OptionalClear(); diff --git a/source/Handlebars/Collections/FixedSizeDictionary.cs b/source/Handlebars/Collections/FixedSizeDictionary.cs index 01e43cb2..83fd27e7 100644 --- a/source/Handlebars/Collections/FixedSizeDictionary.cs +++ b/source/Handlebars/Collections/FixedSizeDictionary.cs @@ -393,9 +393,7 @@ public void Reset() { _count = 0; - unchecked { ++_version; } - - if (_version == 0) _version = 1; + NextVersion(); } /// @@ -415,9 +413,24 @@ public void Clear() _count = 0; + NextVersion(); + } + + /// + /// Moves to the next version. Entries left from earlier versions are kept for speed and + /// count as absent only while their version differs from the current one, so when the + /// version wraps every slot is wiped: an entry must never come back as current. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void NextVersion() + { unchecked { ++_version; } - if (_version == 0) _version = 1; + if (_version != 0) return; + + Array.Clear(_entries, 0, _entries.Length); + Array.Clear(_indexes, 0, _indexes.Length); + _version = 1; } ///