Skip to content

Commit 415a417

Browse files
Fix #76116. (#76747)
1 parent 0058c73 commit 415a417

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/libraries/System.Collections/tests/Generic/List/List.Generic.Tests.AddRange.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,28 @@ public void AddRange_AddSelfAsEnumerable_ThrowsExceptionWhenNotEmpty()
9898
Assert.Throws<InvalidOperationException>(() => list.AddRange(list.Where(_ => true)));
9999
Assert.Equal(6, list.Count);
100100
}
101+
102+
[Fact]
103+
public void AddRange_CollectionWithLargeCount_ThrowsOverflowException()
104+
{
105+
List<T> list = GenericListFactory(count: 1);
106+
ICollection<T> collection = new CollectionWithLargeCount();
107+
108+
Assert.Throws<OverflowException>(() => list.AddRange(collection));
109+
}
110+
111+
private class CollectionWithLargeCount : ICollection<T>
112+
{
113+
public int Count => int.MaxValue;
114+
115+
public bool IsReadOnly => throw new NotImplementedException();
116+
public void Add(T item) => throw new NotImplementedException();
117+
public void Clear() => throw new NotImplementedException();
118+
public bool Contains(T item) => throw new NotImplementedException();
119+
public void CopyTo(T[] array, int arrayIndex) => throw new NotImplementedException();
120+
public IEnumerator<T> GetEnumerator() => throw new NotImplementedException();
121+
public bool Remove(T item) => throw new NotImplementedException();
122+
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();
123+
}
101124
}
102125
}

src/libraries/System.Collections/tests/Generic/List/List.Generic.Tests.InsertRange.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,14 @@ public void InsertRange_MatchesExpectedContents()
4747
Assert.Equal(12, list.Count);
4848
Assert.Equal(new[] { 6, 5, 4, 100, 99, 98, 3, 2, 1, 0, -1, -2 }, list);
4949
}
50+
51+
[Fact]
52+
public void InsertRange_CollectionWithLargeCount_ThrowsOverflowException()
53+
{
54+
List<T> list = GenericListFactory(count: 1);
55+
ICollection<T> collection = new CollectionWithLargeCount();
56+
57+
Assert.Throws<OverflowException>(() => list.InsertRange(0, collection));
58+
}
5059
}
5160
}

src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public void AddRange(IEnumerable<T> collection)
254254
{
255255
if (_items.Length - _size < count)
256256
{
257-
Grow(_size + count);
257+
Grow(checked(_size + count));
258258
}
259259

260260
c.CopyTo(_items, _size);
@@ -787,7 +787,7 @@ public void InsertRange(int index, IEnumerable<T> collection)
787787
{
788788
if (_items.Length - _size < count)
789789
{
790-
Grow(_size + count);
790+
Grow(checked(_size + count));
791791
}
792792
if (index < _size)
793793
{

0 commit comments

Comments
 (0)