From a6d698d7603746559a6fadd275c9840644cb7210 Mon Sep 17 00:00:00 2001 From: "stencil-harness[bot]" <353851134+stencil-harness[bot]@users.noreply.github.com> Date: Thu, 26 Nov 2026 17:20:43 -0500 Subject: [PATCH] fix: wipe FixedSizeDictionary's slots when its version wraps Reset() and OptionalClear() bump a byte version and leave old entries in place, counting them as absent while their version differs. Once the version wraps from 255 back to 1, an entry written 255 versions earlier matches again, so a pooled BindingContext could hand a block param bound by one render to a later, unrelated render. When the version wraps, the entries and indexes are now wiped before version 1 is reused. Taken from #704 by @nadrummond. Co-authored-by: Noor Drummond Co-Authored-By: Claude Opus 5.5 --- .../BindingContextPoolTests.cs | 38 +++++++++++++++++++ .../Collections/FixedSizeDictionaryTests.cs | 29 ++++++++++++++ .../Collections/FixedSizeDictionary.cs | 21 ++++++++-- 3 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 source/Handlebars.Test/BindingContextPoolTests.cs 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; } ///