From 3d68e861473e25a2a3015f6661110e1f4a20dcdc Mon Sep 17 00:00:00 2001 From: "stencil-harness[bot]" <353851134+stencil-harness[bot]@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:03:19 -0500 Subject: [PATCH] fix: stop unset data variables resolving to another key's value FixedSizeDictionary.AdjustIndexes restamped every source index with the destination's version, including empty and stale ones. In a child frame an unset @index/@key/@first/@last then pointed at whatever entry sat in that slot, e.g. a data-object key hashing to slot 0, so {{#with this}}{{@index}}{{/with}} rendered that key's value. Only carry over indexes that are live in the source. Also clear BindingContext.WellKnownVariables when a context goes back to the pool, so indexes from a previous use cannot look current again after the data dictionary's byte version wraps. Refs #707 Co-Authored-By: Claude Opus 5.5 --- .../Collections/FixedSizeDictionaryTests.cs | 51 +++++++++++++++ source/Handlebars.Test/DataVariableTests.cs | 62 +++++++++++++++++++ .../Collections/FixedSizeDictionary.cs | 8 ++- .../Handlebars/Pools/BindingContext.Pool.cs | 6 +- 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 source/Handlebars.Test/DataVariableTests.cs diff --git a/source/Handlebars.Test/Collections/FixedSizeDictionaryTests.cs b/source/Handlebars.Test/Collections/FixedSizeDictionaryTests.cs index 3c0b2cb3..0b836c97 100644 --- a/source/Handlebars.Test/Collections/FixedSizeDictionaryTests.cs +++ b/source/Handlebars.Test/Collections/FixedSizeDictionaryTests.cs @@ -284,6 +284,57 @@ public void CanAddAfterClear() } } + [Fact] + public void AdjustIndexesCarriesLiveIndexes() + { + var source = new FixedSizeDictionary(16, 7, new CollisionsComparer(0)); + var destination = new FixedSizeDictionary(16, 7, new CollisionsComparer(0)); + var sourceIndexes = new EntryIndex[2]; + var targetIndexes = new EntryIndex[2]; + + source.AddOrReplace(new object(), "value", out sourceIndexes[0]); + source.CopyTo(destination); + source.AdjustIndexes(sourceIndexes, destination, targetIndexes); + + Assert.True(destination.TryGetValue(targetIndexes[0], out var value)); + Assert.Equal("value", value); + } + + // https://github.com/Handlebars-Net/Handlebars.Net/issues/707 + [Fact] + public void AdjustIndexesKeepsEmptyIndexesEmpty() + { + var source = new FixedSizeDictionary(16, 7, new CollisionsComparer(0)); + var destination = new FixedSizeDictionary(16, 7, new CollisionsComparer(0)); + var sourceIndexes = new EntryIndex[2]; + var targetIndexes = new EntryIndex[2]; + + // occupies slot 0, which an empty index would otherwise be restamped to point at + destination.AddOrReplace(new object(), "other key", out _); + source.AdjustIndexes(sourceIndexes, destination, targetIndexes); + + Assert.False(targetIndexes[1].IsNotEmpty); + Assert.False(destination.TryGetValue(targetIndexes[1], out _)); + } + + // https://github.com/Handlebars-Net/Handlebars.Net/issues/707 + [Fact] + public void AdjustIndexesDropsStaleIndexes() + { + var source = new FixedSizeDictionary(16, 7, new CollisionsComparer(0)); + var destination = new FixedSizeDictionary(16, 7, new CollisionsComparer(0)); + var sourceIndexes = new EntryIndex[2]; + var targetIndexes = new EntryIndex[2]; + + source.AddOrReplace(new object(), "previous use", out sourceIndexes[0]); + source.Reset(); + destination.AddOrReplace(new object(), "other key", out _); + source.AdjustIndexes(sourceIndexes, destination, targetIndexes); + + Assert.False(targetIndexes[0].IsNotEmpty); + Assert.False(destination.TryGetValue(targetIndexes[0], out _)); + } + public void Dispose() { FixedSizeDictionary.OptionalClear(); diff --git a/source/Handlebars.Test/DataVariableTests.cs b/source/Handlebars.Test/DataVariableTests.cs new file mode 100644 index 00000000..78921548 --- /dev/null +++ b/source/Handlebars.Test/DataVariableTests.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using HandlebarsDotNet.PathStructure; +using Xunit; + +namespace HandlebarsDotNet.Test +{ + // Data variables (`@index`, `@key`, `@first`, ...) are cached per frame as entry indexes into the + // frame's data dictionary. These tests check that an unset data variable never resolves to the + // value stored under a different key. See https://github.com/Handlebars-Net/Handlebars.Net/issues/707 + public class DataVariableTests + { + // Must match BindingContext's ContextDataObject: 16 buckets of 7 entries. + private const int BucketsCount = 16; + private const int BucketSize = 7; + + [Fact] + public void UnsetDataVariableInChildFrameDoesNotResolveToAnotherDataKey() + { + var key = FindKeyForSlot(0); + var data = new Dictionary { [key] = "WRONG" }; + + var template = Handlebars.Create().Compile("{{#with this}}[{{@index}}|{{@key}}|{{@first}}|{{@last}}]{{/with}}"); + + Assert.Equal("[|||]", template(new { a = 1 }, data)); + } + + [Fact] + public void UnsetDataVariableNeverResolvesToAnotherDataKeyAcrossPooledReuse() + { + var handlebars = Handlebars.Create(); + var populate = handlebars.Compile("{{#each this}}{{@index}}{{/each}}"); + var probe = handlebars.Compile("[{{@index}}]{{#with this}}[{{@index}}]{{/with}}"); + + // `populate` stores @index at its home slot; put another key there (and at slot 0) for `probe`, + // so a leftover index from an earlier render would point at a live entry. + var data = new Dictionary + { + [FindKeyForSlot(SlotOf(ChainSegment.Index.GetHashCode()))] = "WRONG", + [FindKeyForSlot(0)] = "WRONG" + }; + + // Enough renders for the data dictionaries' byte versions to wrap several times. + for (var i = 0; i < 2000; i++) + { + populate(new[] { 1, 2, 3 }); + Assert.Equal("[][]", probe(new { a = 1 }, data)); + } + } + + private static string FindKeyForSlot(int slot) + { + for (var i = 0; ; i++) + { + var key = "k" + i; + if (SlotOf(ChainSegment.Create(key).GetHashCode()) == slot) return key; + } + } + + private static int SlotOf(int hash) => (hash & (BucketsCount - 1)) * BucketSize + Math.Abs(hash % BucketSize); + } +} diff --git a/source/Handlebars/Collections/FixedSizeDictionary.cs b/source/Handlebars/Collections/FixedSizeDictionary.cs index 01e43cb2..e65eb960 100644 --- a/source/Handlebars/Collections/FixedSizeDictionary.cs +++ b/source/Handlebars/Collections/FixedSizeDictionary.cs @@ -381,7 +381,13 @@ public void AdjustIndexes(EntryIndex[] source, FixedSizeDictionary(source[i].Index, destination._version); + // Only carry over indexes that are live in this dictionary. Restamping an empty or stale + // index with the destination's version would make it point at whatever entry sits in + // that slot, under a different key. + var index = source[i]; + target[i] = index.IsNotEmpty && index.Version == _version + ? new EntryIndex(index.Index, destination._version) + : default; } } diff --git a/source/Handlebars/Pools/BindingContext.Pool.cs b/source/Handlebars/Pools/BindingContext.Pool.cs index fb337e6f..2778313e 100644 --- a/source/Handlebars/Pools/BindingContext.Pool.cs +++ b/source/Handlebars/Pools/BindingContext.Pool.cs @@ -1,3 +1,4 @@ +using System; using HandlebarsDotNet.Compiler; using HandlebarsDotNet.Pools; @@ -60,7 +61,10 @@ public bool Return(BindingContext item) item.BlockParamsObject.OptionalClear(); item.ContextDataObject.OptionalClear(); - + // Indexes into ContextDataObject from this use must not survive into the next one: + // once the dictionary's byte version wraps they would look current again. + Array.Clear(item.WellKnownVariables, 0, item.WellKnownVariables.Length); + item.Descriptor.Reset(); return true;