Skip to content
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

Fix issue 12495: Infinite loop in ToolStripItemCollection.AddRange #12513

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -141,16 +141,21 @@ public void AddRange(ToolStripItemCollection toolStripItems)
throw new NotSupportedException(SR.ToolStripItemCollectionIsReadOnly);
}

// Return early if the collection is empty.
if (toolStripItems.Count == 0)
{
return;
}

// ToolStripDropDown will look for PropertyNames.Items to determine if it needs
// to resize itself.
using (new LayoutTransaction(_owner, _owner!, PropertyNames.Items))
{
for (int i = 0; i < toolStripItems.Count; i++)
// Create a temporary list to avoid modifying the collection while iterating over it.
Olina-Zhang marked this conversation as resolved.
Show resolved Hide resolved
var itemsToAdd = toolStripItems.Cast<ToolStripItem>().ToList();
Olina-Zhang marked this conversation as resolved.
Show resolved Hide resolved
foreach (ToolStripItem item in itemsToAdd)
{
// Items are removed from their origin when added to a different owner.
// Decrement the index to always add the items from index 0 which will preserve
// the original order and avoid a pesky ArgumentOutOfRangeException.
Add(toolStripItems[i--]);
Tanya-Solyanik marked this conversation as resolved.
Show resolved Hide resolved
Add(item);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ public void ToolStripItemCollection_AddRange_ToolStripItemCollection_Success()
Assert.Equal("c", contextMenuStrip.Items[2].Text);
}

[WinFormsFact]
Olina-Zhang marked this conversation as resolved.
Show resolved Hide resolved
public void ToolStripItemCollection_AddRange_ToolStripItemCollection_WithItems_Success()
Olina-Zhang marked this conversation as resolved.
Show resolved Hide resolved
{
using ToolStrip toolStrip = new();

// Create a ToolStripItemCollection with 2 items
ToolStripItemCollection itemCollection = new(toolStrip, new[]
{
Olina-Zhang marked this conversation as resolved.
Show resolved Hide resolved
new ToolStripButton("Button 1"),
new ToolStripButton("Button 2")
Olina-Zhang marked this conversation as resolved.
Show resolved Hide resolved
});

toolStrip.Items.AddRange(itemCollection);

toolStrip.Items.Count.Should().Be(2);
Olina-Zhang marked this conversation as resolved.
Show resolved Hide resolved

toolStrip.Items[0].Text.Should().Be("Button 1");
toolStrip.Items[1].Text.Should().Be("Button 2");
}

private ToolStripItem[] CreateToolStripItems(params (string Key, string Name)[] items) =>
items.Select(item => new ToolStripButton(item.Name) { Name = item.Key }).ToArray();

Expand Down
Loading