-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Infinite loop in ToolStripItemCollection.AddRange #12495
Comments
@Olina-Zhang can your team test this please? |
We can repro this issue from .NET 6.0 to .NET 10 latest build with following testing code and sample application, not .NET 5.0 and .NET framework 4.8.1
|
@Olina-Zhang since the fix is pretty straight forward. Did your team want to take it on? |
@elachlan Tried to modify AddRange method with following, although this issue is resolved, but the issue #4454 is not fixed well. Need to investigate further...
|
@Olina-Zhang use a reverse for loop. public void AddRange(ToolStripItemCollection toolStripItems)
{
ArgumentNullException.ThrowIfNull(toolStripItems);
if (IsReadOnly)
{
throw new NotSupportedException(SR.ToolStripItemCollectionIsReadOnly);
}
// ToolStripDropDown will look for PropertyNames.Items to determine if it needs
// to resize itself.
using (new LayoutTransaction(_owner, _owner!, PropertyNames.Items))
{
// Iterate in reverse to avoid index shifting issues.
for (int i = toolStripItems.Count - 1; i >= 0; i--)
{
Add(toolStripItems[i]);
}
}
} |
Looks good, it can resolve both of issues. |
We should make sure we have test coverage of both issues. |
Great work @Olina-Zhang and @elachlan ! Feel free to submit the PR 😄 |
Consider converting collection to an array (.ToArray()) instead of reversing and reading backwards. |
@merriemcgaw - are we servicing this hang? |
…12513) Fixes #12495 and #4454 Proposed changes Early return for empty collection Converts the ToolStripItemCollection into a temporary array (using ToArray()) to avoid modifying the original collection during iteration. This ensures that items can be safely added to the new collection without causing exceptions or unintended behavior, especially when items are removed from the original collection if they have a different owner control. Add unit test for issue Infinite loop in ToolStripItemCollection.AddRange #12495 case Regression? Yes Test methodology Test fixing for GH issues: 12495 and 4454 manually Unit test
.NET version
.NET 8.0
Did it work in .NET Framework?
Yes
Did it work in any of the earlier releases of .NET Core or .NET 5+?
I dont know
Issue description
There is an infinite loop in ToolStripItemCollection.cs:
for (int i = 0; i < toolStripItems.Count; i++) counts up from 0 but decrements 'i' down in Add(toolStripItems[i--]);
Any call to add items using this override will infinite loop with i going from 0 -> -1 -> 0 -> -1 etc...
Steps to reproduce
Any call to ToolStripItemCollection.AddRange(ToolStripItemCollection toolStripItems) with items.
The text was updated successfully, but these errors were encountered: