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;