From 415a41770bdf8efd4c3217e2c281233ee5cd03ea Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Fri, 7 Oct 2022 22:25:17 +0100 Subject: [PATCH] Fix #76116. (#76747) --- .../Generic/List/List.Generic.Tests.AddRange.cs | 23 ++++++++++++++++++++++ .../Generic/List/List.Generic.Tests.InsertRange.cs | 9 +++++++++ .../src/System/Collections/Generic/List.cs | 4 ++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Collections/tests/Generic/List/List.Generic.Tests.AddRange.cs b/src/libraries/System.Collections/tests/Generic/List/List.Generic.Tests.AddRange.cs index 26298c6..c0309d0 100644 --- a/src/libraries/System.Collections/tests/Generic/List/List.Generic.Tests.AddRange.cs +++ b/src/libraries/System.Collections/tests/Generic/List/List.Generic.Tests.AddRange.cs @@ -98,5 +98,28 @@ namespace System.Collections.Tests Assert.Throws(() => list.AddRange(list.Where(_ => true))); Assert.Equal(6, list.Count); } + + [Fact] + public void AddRange_CollectionWithLargeCount_ThrowsOverflowException() + { + List list = GenericListFactory(count: 1); + ICollection collection = new CollectionWithLargeCount(); + + Assert.Throws(() => list.AddRange(collection)); + } + + private class CollectionWithLargeCount : ICollection + { + public int Count => int.MaxValue; + + public bool IsReadOnly => throw new NotImplementedException(); + public void Add(T item) => throw new NotImplementedException(); + public void Clear() => throw new NotImplementedException(); + public bool Contains(T item) => throw new NotImplementedException(); + public void CopyTo(T[] array, int arrayIndex) => throw new NotImplementedException(); + public IEnumerator GetEnumerator() => throw new NotImplementedException(); + public bool Remove(T item) => throw new NotImplementedException(); + IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); + } } } diff --git a/src/libraries/System.Collections/tests/Generic/List/List.Generic.Tests.InsertRange.cs b/src/libraries/System.Collections/tests/Generic/List/List.Generic.Tests.InsertRange.cs index d72fd45..4074bde 100644 --- a/src/libraries/System.Collections/tests/Generic/List/List.Generic.Tests.InsertRange.cs +++ b/src/libraries/System.Collections/tests/Generic/List/List.Generic.Tests.InsertRange.cs @@ -47,5 +47,14 @@ namespace System.Collections.Tests Assert.Equal(12, list.Count); Assert.Equal(new[] { 6, 5, 4, 100, 99, 98, 3, 2, 1, 0, -1, -2 }, list); } + + [Fact] + public void InsertRange_CollectionWithLargeCount_ThrowsOverflowException() + { + List list = GenericListFactory(count: 1); + ICollection collection = new CollectionWithLargeCount(); + + Assert.Throws(() => list.InsertRange(0, collection)); + } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs index 6f1340e..a7b9aa6 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs @@ -254,7 +254,7 @@ namespace System.Collections.Generic { if (_items.Length - _size < count) { - Grow(_size + count); + Grow(checked(_size + count)); } c.CopyTo(_items, _size); @@ -787,7 +787,7 @@ namespace System.Collections.Generic { if (_items.Length - _size < count) { - Grow(_size + count); + Grow(checked(_size + count)); } if (index < _size) { -- 2.7.4