Skip to content

Commit 78708d6

Browse files
Fix IndexOutOfRangeException when an added/removed attribute spans a buffer size boundary (#50110)
**NOTE: I'm not actually proposing this for servicing right now, because it doesn't look like there would be a major benefit from including it in RC1, and including it in RC2 would be perfectly sufficient. However I'm writing up the ask mode template anyway just in case people disagree.** Extremely rarely, component rendering could cause IndexOutOfRangeException. ## Description If a component rendered a very specific number of frames, and then updated to render exactly one more frame, and that new frame was an attribute, and it was the last frame in the component, the diffing system would throw `IndexOutOfRangeException`. This was due to a bug in checking whether there are more attributes to process. Fixes #49192 ## Customer Impact Fairly low, because it's really hard to reproduce this, even if you're doing so on purpose. However, total correctness of the Blazor renderer is a high priority for us, and we do not want any edge cases where it can fail. In the event that the issue does occur, it would behave like an unhandled exception from application code: * For Blazor WebAssembly, the yellow error bar would appear, but the app would continue running * For Blazor Server, the current user's circuit would be terminated, but other server activity would be unaffected * For Blazor WebView, the unhandled exception callback would run ## Regression? - [ ] Yes - [x] No [If yes, specify the version the behavior has regressed from] ## Risk - [ ] High - [ ] Medium - [x] Low Low because the code change is small enough (4 lines) to reason clearly that it only makes a difference in the case where it would have failed before. The updated boundary condition is simple to understand. ## Verification - [x] Manual (required) - [x] Automated ## Packaging changes reviewed? - [ ] Yes - [ ] No - [x] N/A
1 parent 1a83e39 commit 78708d6

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,12 @@ private static void AppendAttributeDiffEntriesForRange(
429429

430430
while (hasMoreOld || hasMoreNew)
431431
{
432-
var oldSeq = hasMoreOld ? oldTree[oldStartIndex].SequenceField : int.MaxValue;
433-
var newSeq = hasMoreNew ? newTree[newStartIndex].SequenceField : int.MaxValue;
434-
var oldAttributeName = oldTree[oldStartIndex].AttributeNameField;
435-
var newAttributeName = newTree[newStartIndex].AttributeNameField;
432+
var (oldSeq, oldAttributeName) = hasMoreOld
433+
? (oldTree[oldStartIndex].SequenceField, oldTree[oldStartIndex].AttributeNameField)
434+
: (int.MaxValue, null);
435+
var (newSeq, newAttributeName) = hasMoreNew
436+
? (newTree[newStartIndex].SequenceField, newTree[newStartIndex].AttributeNameField)
437+
: (int.MaxValue, null);
436438

437439
if (oldSeq == newSeq &&
438440
string.Equals(oldAttributeName, newAttributeName, StringComparison.Ordinal))

src/Components/Components/test/RenderTreeDiffBuilderTest.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,6 +2319,73 @@ public void RecognizesNamedEventChangingAssignedName()
23192319
entry => AssertNamedEventChange(entry, NamedEventChangeType.Added, 123, 1, "eventname1", "changed name"));
23202320
}
23212321

2322+
[Fact]
2323+
public void CanAddNewAttributeAtArrayBuilderSizeBoundary()
2324+
{
2325+
// Represents https://github.com/dotnet/aspnetcore/issues/49192
2326+
2327+
// Arrange: old and new trees go exactly up to the array builder capacity
2328+
oldTree.OpenElement(0, "elem");
2329+
for (var i = 0; oldTree.GetFrames().Count < oldTree.GetFrames().Array.Length; i++)
2330+
{
2331+
oldTree.AddAttribute(1, $"myattribute_{i}", "value");
2332+
}
2333+
newTree.OpenElement(0, "elem");
2334+
for (var i = 0; newTree.GetFrames().Count < newTree.GetFrames().Array.Length; i++)
2335+
{
2336+
newTree.AddAttribute(1, $"myattribute_{i}", "value");
2337+
}
2338+
2339+
// ... then the new tree gets one more attribute that crosses the builder size boundary, forcing buffer expansion
2340+
newTree.AddAttribute(1, $"myattribute_final", "value");
2341+
2342+
// Act
2343+
oldTree.CloseElement();
2344+
newTree.CloseElement();
2345+
var (result, referenceFrames) = GetSingleUpdatedComponent();
2346+
2347+
// Assert
2348+
Assert.Collection(result.Edits,
2349+
entry =>
2350+
{
2351+
AssertEdit(entry, RenderTreeEditType.SetAttribute, 0);
2352+
Assert.Equal(0, entry.ReferenceFrameIndex);
2353+
AssertFrame.Attribute(referenceFrames[0], "myattribute_final", "value", 1);
2354+
});
2355+
}
2356+
2357+
[Fact]
2358+
public void CanRemoveOldAttributeAtArrayBuilderSizeBoundary()
2359+
{
2360+
// Arrange: old and new trees go exactly up to the array builder capacity
2361+
oldTree.OpenElement(0, "elem");
2362+
for (var i = 0; oldTree.GetFrames().Count < oldTree.GetFrames().Array.Length; i++)
2363+
{
2364+
oldTree.AddAttribute(1, $"myattribute_{i}", "value");
2365+
}
2366+
newTree.OpenElement(0, "elem");
2367+
for (var i = 0; newTree.GetFrames().Count < newTree.GetFrames().Array.Length; i++)
2368+
{
2369+
newTree.AddAttribute(1, $"myattribute_{i}", "value");
2370+
}
2371+
2372+
// ... then the old tree gets one more attribute that crosses the builder size boundary, forcing buffer expansion
2373+
oldTree.AddAttribute(1, $"myattribute_final", "value");
2374+
2375+
// Act
2376+
oldTree.CloseElement();
2377+
newTree.CloseElement();
2378+
var (result, referenceFrames) = GetSingleUpdatedComponent();
2379+
2380+
// Assert
2381+
Assert.Collection(result.Edits,
2382+
entry =>
2383+
{
2384+
AssertEdit(entry, RenderTreeEditType.RemoveAttribute, 0);
2385+
Assert.Equal("myattribute_final", entry.RemovedAttributeName);
2386+
});
2387+
}
2388+
23222389
private (RenderTreeDiff, RenderTreeFrame[]) GetSingleUpdatedComponent(bool initializeFromFrames = false)
23232390
{
23242391
var result = GetSingleUpdatedComponentWithBatch(initializeFromFrames);

0 commit comments

Comments
 (0)