Skip to content

fix: stop unset data variables resolving to another key's value - #708

Open
stencil-harness[bot] wants to merge 1 commit into
masterfrom
robot/issue-707
Open

stencil-harness[bot] wants to merge 1 commit into
masterfrom
robot/issue-707

Conversation

@stencil-harness

Copy link
Copy Markdown
Contributor

Closes #707

Problem

Each BindingContext caches EntryIndex values for its data variables (@root, @index, @key, @first, @last, …) in WellKnownVariables. Two things could leave a wrong index in that array:

  1. FixedSizeDictionary.AdjustIndexes restamped every index, including empty and stale ones, with the child dictionary's version. An empty index (slot 0) or a stale one left by an earlier use then looked current in the child, and returned whatever entry sat in that slot under a different key.
  2. BindingContextPolicy.Return never cleared WellKnownVariables, so a pooled context kept indexes from its last use. Once the dictionary's byte version wraps, those indexes can look current again.

Point 1 needs no version wrap. It shows up on the first render whenever a data-object key hashes to slot 0 (string hashes are randomized per process, so which key does this varies from run to run):

var data = new Dictionary<string, object> { [keyHashingToSlot0] = "WRONG" };
Handlebars.Compile("{{#with this}}[{{@index}}|{{@key}}|{{@first}}|{{@last}}]{{/with}}")(new { a = 1 }, data);
// before: [WRONG|WRONG|WRONG|WRONG]
// after:  [|||]

Change

  • FixedSizeDictionary.AdjustIndexes (source/Handlebars/Collections/FixedSizeDictionary.cs) only carries over indexes that are non-empty and current in the source dictionary. All others become default. Readers then fall back to a lookup by key (DataValues), or miss (TryGetValue(in EntryIndex) with version 0).
  • BindingContextPolicy.Return (source/Handlebars/Pools/BindingContext.Pool.cs) clears WellKnownVariables with Array.Clear. That costs one 8-element struct clear per returned context.

Live indexes are carried over exactly as before. @index, @key and the other data variables set by #each, and @root and @parent, behave the same. No public signatures change. The only behaviour change in the public AdjustIndexes method is that empty or stale indexes no longer turn into "current" ones that point at an unrelated entry.

This doesn't depend on #704 or #706 and doesn't conflict with them: it touches different lines.

Tests

  • DataVariableTests (new, named by capability):
    • UnsetDataVariableInChildFrameDoesNotResolveToAnotherDataKey reproduces the example above. It picks the slot-0 key at runtime, so the test is deterministic.
    • UnsetDataVariableNeverResolvesToAnotherDataKeyAcrossPooledReuse runs 2000 renders that interleave {{#each}} with a probe template. The probe's data has keys in slot 0 and in @index's home slot.
  • FixedSizeDictionaryTests: AdjustIndexesCarriesLiveIndexes, AdjustIndexesKeepsEmptyIndexesEmpty and AdjustIndexesDropsStaleIndexes.

Before the fix, all four new failure-case tests failed: [WRONG|WRONG|WRONG|WRONG], [][WRONG], and both AdjustIndexes asserts. After the fix, the full suite passes (2354/2354).

The AdjustIndexes change alone is enough to make these tests pass. The pool-return clear covers a root frame that reads a leftover index after a version wrap. That path can't be reproduced deterministically through the shared static pool, so no test exercises it directly.

Release

This is a bug fix with no API change, so a patch release is enough.

🤖 Generated with Claude Code

Closes #707

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 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reset BindingContext.WellKnownVariables when a context is returned to the pool

0 participants