xamarin/Xamarin.Forms

[Android] System.ArgumentException in OrderedDictionary: An item with the same key has already been added.

オープン

#3,516 opened on 2018/08/08

 (1 件のコメント) (0 件のリアクション) (0 人の担当者)C# (1,926 件のフォーク)batch import
e/2 :clock2:good first issuehelp wantedi/highinactivep/Androidt/bug :bug:up-for-grabs

Repository metrics

Stars
 (5,644 個のスター)
PR merge metrics
 (30d に merged PR はありません)

説明

Description

In our crash logs, we saw an ArgumentException for Android devices. After digging through Xamarin.Froms's code, I discovered it is possible that the method OrderedDictionary.Insert(int, TKey, TValue) could attempt to add duplicate keys to a Dictionary.

public void Insert(int index, TKey key, TValue value)
{
    _keyOrder.Insert(index, key);
    _dict.Add(key, value);
}

The problem I see is that the argument passed to the parameter key originates from an ListProxy which is an IReadOnlyList. There is no guarantee that objects within the ListProxy are unique.

Below is an abridged execution path for how we get to Insert(int, TKey, TValue). GroupedReset() is called which then calls InsertGrouped(object, int) which then calls Insert(int, TKey, TValue)

// TemplatedItemsList.cs

void GroupedReset()
{
    ...
    
    var i = 0;
    foreach (object item in ListProxy)
        InsertGrouped(item, i++);

    OnCollectionChanged(
        new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)
    );


TemplatedItemsList<TView, TItem> InsertGrouped(object item, int index)
{
    ...
   
    // `_groupedItems` is an OrderedDictionary. The definition for `Insert` is pasted above.
    _groupedItems.Insert(index, item, groupProxy);

    groupProxy.CollectionChanged += OnInnerCollectionChanged;

    return groupProxy;
}

Steps to Reproduce

The reproduction steps within the context of our app is unclear.

Expected Behavior

I would expect the ListProxy to be a Set so duplicates could not exist.

Actual Behavior

A System.ArgumentException is raised:

System.ArgumentException: An item with the same key has already been added

Basic Information

  • Version with issue: 3.1.0.637273
  • Last known good version:
  • IDE: VisualStudio for Mac Community, 7.5.4 (3)
  • Platform Target Frameworks:
    • iOS: 10
    • Android: 8.0
  • Android Support Library Version: 4
  • Nuget Packages:
  • Affected Devices: All Android devices

Screenshots

N/A

Reproduction Link

N/A

コントリビューターガイド