Repository navigation
Description
Follow-up hardening from #705, which is fixed by #704. This is separate from that fix and isn't needed for it.
Current behaviour
BindingContext.WellKnownVariables (source/Handlebars/BindingContext.cs) caches EntryIndex values that point into ContextDataObject, one per data variable (@root, @index, @key, @first, @last, …). The pool's return policy (BindingContextPolicy.Return in source/Handlebars/Pools/BindingContext.Pool.cs) clears BlockParamsObject and ContextDataObject, but it leaves this array as it is. A pooled context therefore keeps indexes written by whatever render used it last.
A stale index is ignored only while its version differs from the dictionary's current version. Once the byte version wraps, a stale index can carry the current version again. Readers trust that index:
BindingContext.TryGetContextVariable, throughContextDataObject.TryGetValue(in EntryIndex)DataValues' indexer, which only checksIsNotEmptyand then the version
As a result, a lookup like @index in a frame that never set it can resolve to whatever entry the current render stored in that slot, under a different key.
With #704 the slots are wiped on wrap, so a stale index can only reach an empty slot or data from the current render. Nothing leaks across renders, but within a single render the value can belong to the wrong key.
A related detail: FixedSizeDictionary.AdjustIndexes restamps every source index with the destination's version, including empty ones. A parent's unset slot therefore becomes a "current" index to slot 0 in the child.
Proposed change
- In
BindingContextPolicy.Return, clear the array:Array.Clear(item.WellKnownVariables, 0, item.WellKnownVariables.Length). A defaultEntryIndexhas version0, which never matches a live dictionary version (always ≥ 1). - Optionally, make
AdjustIndexeskeep empty indexes empty:source[i].IsNotEmpty ? new EntryIndex<TKey>(source[i].Index, destination._version) : default.
The cost is clearing an 8-element struct array per context return. There is no public API change.
Testing
Add a test in BindingContextPoolTests that checks data variables never resolve to another key's value, even after the version wraps. For example, render {{#with this as |y|}}[{{@index}}]{{/with}} repeatedly after templates that populate @index, and expect [] every time. This is hard to make fully deterministic through the shared pool, so a focused unit test on AdjustIndexes and the return policy may be the better primary check.
Activity
stencil-harness commentedon Nov 26, 2026
I'm the agent responsible for managing intake of issues, acting under @rexm's steering. I understand this asks to clear
BindingContext.WellKnownVariableswhen a context goes back to the pool, and optionally to keep empty indexes empty inFixedSizeDictionary.AdjustIndexes. I'll check whether I can handle this on my own.stencil-harness commentedon Nov 26, 2026
I've opened #708 with both changes proposed here. It's staged for review and not merged or released.
Handlebars-Net/Handlebars.Net (branch
robot/issue-707, left on the branch):FixedSizeDictionary.AdjustIndexesnow only carries over indexes that are non-empty and current in the source dictionary. All others becomedefault.BindingContextPolicy.ReturnclearsWellKnownVariableswithArray.Clear.The
AdjustIndexeshalf matters more than the issue suggested, because it needs no version wrap. If a data-object key hashes to slot 0, then{{#with this}}[{{@index}}|{{@key}}|{{@first}}|{{@last}}]{{/with}}renders[WRONG|WRONG|WRONG|WRONG]on the first render. With the fix it renders[|||].Verification:
DataVariableTests, plus threeAdjustIndexestests inFixedSizeDictionaryTests. The four failure-case tests failed before the fix and pass after it.pr.viewreports the PR asMERGEABLE; merge state isBLOCKEDuntil review and checks finish. I recommend a patch release.