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 IndexOutOfRangeException/Infinite loop in UpdateComponent (Track control) #9934

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -120,42 +120,38 @@ private void UpdateComponent(Control oldValue, Control newValue)
{
if (oldValue != newValue)
{
if (_visualChildren == null)
{
_visualChildren = new Visual[3];
}
const int VisualChildrenCount = 3;
const int VisualChildrenMaxIndex = 2;
_visualChildren ??= new Visual[VisualChildrenCount];

// Notify the visual layer that the old component has been removed
if (oldValue != null)
{
// notify the visual layer that the old component has been removed.
RemoveVisualChild(oldValue);
}

// Remove the old value from our z index list and add new value to end
int i = 0;
while (i < 3)
{
// Array isn't full, break
if (_visualChildren[i] == null)
break;
int itemIndex = 0;
int nullIndex = 0;

// found the old value
if (_visualChildren[i] == oldValue)
{
// Move values down until end of array or a null element
while (i < 2 && _visualChildren[i + 1] != null)
{
_visualChildren[i] = _visualChildren[i + 1];
i++;
}
}
else
{
i++;
}
// Find the Count of the items
while (nullIndex < VisualChildrenCount && _visualChildren[nullIndex] != null)
nullIndex++;

// Find the oldValue item index
while (itemIndex < VisualChildrenMaxIndex && _visualChildren[itemIndex] != oldValue)
itemIndex++;

Debug.Assert(_visualChildren[itemIndex] == oldValue, "Attempt to add a 4th item into _visualChildren");

// In case we're replacing a value, we need to shift the items to the left first, and then append newValue as last
nullIndex--;
if (itemIndex < nullIndex)
{
Array.Copy(_visualChildren, itemIndex + 1, _visualChildren, itemIndex, nullIndex - itemIndex);
_visualChildren[nullIndex] = newValue;
}
else // In case there are still leftover NULL items (empty spots) or it is the last one, we just append
{
_visualChildren[itemIndex] = newValue;
}
// Add newValue at end of z-order
_visualChildren[i] = newValue;

AddVisualChild(newValue);

Expand Down Expand Up @@ -375,11 +371,7 @@ public bool IsDirectionReversed
/// </summary>
protected override Visual GetVisualChild(int index)
{
if (_visualChildren == null || _visualChildren[index] == null)
{
throw new ArgumentOutOfRangeException("index", index, SR.Visual_ArgumentOutOfRange);
}
return _visualChildren[index];
return _visualChildren?[index] ?? throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);
}

/// <summary>
Expand Down