xamarin/Xamarin.Forms

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

开放

#3,516 创建于 2018年8月8日

 (1 条评论) (0 个反应) (0 位负责人)C# (1,926 个派生)batch import
e/2 :clock2:good first issuehelp wantedi/highinactivep/Androidt/bug :bug:up-for-grabs

仓库指标

星标
 (5,644 个星标)
PR 合并指标
 (30 天内没有已合并 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

贡献者指南