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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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 @@ -141,16 +141,23 @@ 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 original collection during iteration.
Olina-Zhang marked this conversation as resolved.
Show resolved Hide resolved
// This ensures that the collection is not altered while it is being iterated over,
// preventing issues such as InvalidOperationException.
Olina-Zhang marked this conversation as resolved.
Show resolved Hide resolved
var itemsToAdd = toolStripItems.InnerList.ToArray();
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,29 @@ 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_SameOwner_Success()
{
using ToolStrip toolStrip = new();

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

toolStrip.Items.Count.Should().Be(0);

toolStrip.Items.AddRange(itemCollection);

itemCollection.Count.Should().Be(2);
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