Skip to content

Fix IndexOutOfRangeException when an added/removed attribute spans a buffer size boundary #50110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,12 @@ private static void AppendAttributeDiffEntriesForRange(

while (hasMoreOld || hasMoreNew)
{
var oldSeq = hasMoreOld ? oldTree[oldStartIndex].SequenceField : int.MaxValue;
var newSeq = hasMoreNew ? newTree[newStartIndex].SequenceField : int.MaxValue;
var oldAttributeName = oldTree[oldStartIndex].AttributeNameField;
var newAttributeName = newTree[newStartIndex].AttributeNameField;
var (oldSeq, oldAttributeName) = hasMoreOld
? (oldTree[oldStartIndex].SequenceField, oldTree[oldStartIndex].AttributeNameField)
: (int.MaxValue, null);
var (newSeq, newAttributeName) = hasMoreNew
? (newTree[newStartIndex].SequenceField, newTree[newStartIndex].AttributeNameField)
: (int.MaxValue, null);

if (oldSeq == newSeq &&
string.Equals(oldAttributeName, newAttributeName, StringComparison.Ordinal))
Expand Down
67 changes: 67 additions & 0 deletions src/Components/Components/test/RenderTreeDiffBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2319,6 +2319,73 @@ public void RecognizesNamedEventChangingAssignedName()
entry => AssertNamedEventChange(entry, NamedEventChangeType.Added, 123, 1, "eventname1", "changed name"));
}

[Fact]
public void CanAddNewAttributeAtArrayBuilderSizeBoundary()
{
// Represents https://github.com/dotnet/aspnetcore/issues/49192

// Arrange: old and new trees go exactly up to the array builder capacity
oldTree.OpenElement(0, "elem");
for (var i = 0; oldTree.GetFrames().Count < oldTree.GetFrames().Array.Length; i++)
{
oldTree.AddAttribute(1, $"myattribute_{i}", "value");
}
newTree.OpenElement(0, "elem");
for (var i = 0; newTree.GetFrames().Count < newTree.GetFrames().Array.Length; i++)
{
newTree.AddAttribute(1, $"myattribute_{i}", "value");
}

// ... then the new tree gets one more attribute that crosses the builder size boundary, forcing buffer expansion
newTree.AddAttribute(1, $"myattribute_final", "value");

// Act
oldTree.CloseElement();
newTree.CloseElement();
var (result, referenceFrames) = GetSingleUpdatedComponent();

// Assert
Assert.Collection(result.Edits,
entry =>
{
AssertEdit(entry, RenderTreeEditType.SetAttribute, 0);
Assert.Equal(0, entry.ReferenceFrameIndex);
AssertFrame.Attribute(referenceFrames[0], "myattribute_final", "value", 1);
});
}

[Fact]
public void CanRemoveOldAttributeAtArrayBuilderSizeBoundary()
{
// Arrange: old and new trees go exactly up to the array builder capacity
oldTree.OpenElement(0, "elem");
for (var i = 0; oldTree.GetFrames().Count < oldTree.GetFrames().Array.Length; i++)
{
oldTree.AddAttribute(1, $"myattribute_{i}", "value");
}
newTree.OpenElement(0, "elem");
for (var i = 0; newTree.GetFrames().Count < newTree.GetFrames().Array.Length; i++)
{
newTree.AddAttribute(1, $"myattribute_{i}", "value");
}

// ... then the old tree gets one more attribute that crosses the builder size boundary, forcing buffer expansion
oldTree.AddAttribute(1, $"myattribute_final", "value");

// Act
oldTree.CloseElement();
newTree.CloseElement();
var (result, referenceFrames) = GetSingleUpdatedComponent();

// Assert
Assert.Collection(result.Edits,
entry =>
{
AssertEdit(entry, RenderTreeEditType.RemoveAttribute, 0);
Assert.Equal("myattribute_final", entry.RemovedAttributeName);
});
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of these new unit tests do fail with IndexOutOfRangeException without the fix, and of course pass with the fix.

private (RenderTreeDiff, RenderTreeFrame[]) GetSingleUpdatedComponent(bool initializeFromFrames = false)
{
var result = GetSingleUpdatedComponentWithBatch(initializeFromFrames);
Expand Down