dotnet / dotnet/wpf

PriorityBinding.InitializeClone throws ArgumentOutOfRangeException

Open
#11,419 0 comments 3 reactions 0 assignees View on GitHub
:construction: work in progress
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

`PriorityBinding.InitializeClone` has the same off-by-one bug that was fixed for `MultiBinding` in #3220 / #3221.

The fix for `MultiBinding` changed `i<=_bindingCollection.Count` to `i<_bindingCollection.Count`, but the identical bug in `PriorityBinding` was not addressed.

https://github.com/dotnet/wpf/blob/main/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/PriorityBinding.cs#L116

### Reproduction Steps

```csharp
var priorityBinding = new PriorityBinding();
priorityBinding.Bindings.Add(new Binding("Path1"));
priorityBinding.Bindings.Add(new Binding("Path2"));

var cloneMethod = typeof(BindingBase).GetMethod("Clone", BindingFlags.Instance | BindingFlags.NonPublic);
var clone = cloneMethod.Invoke(priorityBinding, new object[] { BindingMode.Default }); // throws ArgumentOutOfRangeException
```

### Expected behavior

`PriorityBinding` should be cloned without exception

### Actual behavior

```
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
at System.Collections.Generic.List`1.get_Item(Int32 index)
at System.Windows.Data.PriorityBinding.InitializeClone(BindingBase baseClone, BindingMode mode)
at System.Windows.Data.BindingBase.Clone(BindingMode mode)
```

### Regression?

No. This bug has existed since the code was written. The same bug in `MultiBinding` was fixed in #3221, but `PriorityBinding` was not included in that fix

### Known Workarounds

Clone `PriorityBinding` manually by creating a new instance and copying properties/bindings

### Impact

_No response_

### Configuration

- .NET 10.0 (also reproduces on .NET Framework 4.8)
- Windows 11
- x64
- Not specific to configuration

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.