diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/TagList.netcore.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/TagList.netcore.cs index 325a0ee380c758..733a280cb8a94d 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/TagList.netcore.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/TagList.netcore.cs @@ -154,13 +154,17 @@ public readonly void CopyTo(Span> tags) /// The one-dimensional Array that is the destination of the elements copied from . The Array must have zero-based indexing. /// The zero-based index in at which copying begins. /// is null. - /// is less than 0 or greater that or equal the length. + /// is less than 0 or greater than the length. public readonly void CopyTo(KeyValuePair[] array, int arrayIndex) { ArgumentNullException.ThrowIfNull(array); - ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)arrayIndex, (uint)array.Length, nameof(arrayIndex)); + ArgumentOutOfRangeException.ThrowIfGreaterThan((uint)arrayIndex, (uint)array.Length, nameof(arrayIndex)); - CopyTo(array.AsSpan(arrayIndex)); + if (_tagsCount > 0) + { + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)arrayIndex, (uint)array.Length, nameof(arrayIndex)); + CopyTo(array.AsSpan(arrayIndex)); + } } /// diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/TagList.netfx.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/TagList.netfx.cs index d446fa0648aa92..bcc8f850d9e76a 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/TagList.netfx.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/TagList.netfx.cs @@ -257,17 +257,25 @@ public readonly void CopyTo(Span> tags) /// The one-dimensional Array that is the destination of the elements copied from . The Array must have zero-based indexing. /// The zero-based index in at which copying begins. /// is null. - /// is less than 0 or greater that or equal the length. + /// is less than 0 or greater than the length. public readonly void CopyTo(KeyValuePair[] array, int arrayIndex) { ArgumentNullException.ThrowIfNull(array); - if ((uint)arrayIndex >= array.Length) + if ((uint)arrayIndex > (uint)array.Length) { throw new ArgumentOutOfRangeException(nameof(arrayIndex)); } - CopyTo(array.AsSpan(arrayIndex)); + if (_tagsCount > 0) + { + if ((uint)arrayIndex >= (uint)array.Length) + { + throw new ArgumentOutOfRangeException(nameof(arrayIndex)); + } + + CopyTo(array.AsSpan(arrayIndex)); + } } /// diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/tests/TagListTests.cs b/src/libraries/System.Diagnostics.DiagnosticSource/tests/TagListTests.cs index 11c1ee48955788..ee8b229eb4c960 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/tests/TagListTests.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/tests/TagListTests.cs @@ -3,6 +3,7 @@ using Xunit; using System.Collections.Generic; +using System.Linq; namespace System.Diagnostics.Tests { @@ -82,6 +83,9 @@ public void TestCopyTo() array = new KeyValuePair[tagList.Count]; tagList.CopyTo(array); ValidateTags(tagList, array); + array = new KeyValuePair[tagList.Count]; + ((ICollection>)tagList).CopyTo(array, 0); + ValidateTags(tagList, array); } } @@ -277,6 +281,31 @@ public void TestIndex(int count) } } + [Fact] + public void TestEmptyTagListCopyTo() + { + TagList emptyList = new TagList(); + Assert.Equal(0, emptyList.Count); + + KeyValuePair[] emptyArray = Array.Empty>(); + emptyList.CopyTo(emptyArray, 0); + emptyList.CopyTo(emptyArray.AsSpan()); + emptyList.CopyTo(emptyArray); + + KeyValuePair[] nonEmptyArray = new KeyValuePair[3]; + emptyList.CopyTo(nonEmptyArray, 0); + emptyList.CopyTo(nonEmptyArray, 2); + emptyList.CopyTo(nonEmptyArray, 3); + emptyList.CopyTo(nonEmptyArray.AsSpan()); + emptyList.CopyTo(nonEmptyArray); + + Assert.Throws(() => emptyList.CopyTo(nonEmptyArray, -1)); + Assert.Throws(() => emptyList.CopyTo(nonEmptyArray, 4)); + + KeyValuePair[] result = emptyList.ToArray(); + Assert.Empty(result); + } + [Fact] public void TestNegativeCases() {