From 6ed2db0ce59f3e12aabea1cf8eceade53d14523a Mon Sep 17 00:00:00 2001 From: Justin Van Patten Date: Fri, 15 Mar 2019 07:52:31 -0700 Subject: [PATCH] Collection: Validate parameters in the public methods (dotnet/coreclr#23166) I was looking over the new Range methods on `Collection` and it occurred to me that for all existing methods, parameter validation is done in the *public* methods before calling the *protected virtual* methods. This changes the new Range methods to do the same. Commit migrated from https://github.com/dotnet/coreclr/commit/52311794f86596eeeedb745fc2258edd0eeafb95 --- .../System/Collections/ObjectModel/Collection.cs | 110 ++++++++++++++------- 1 file changed, 72 insertions(+), 38 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/Collection.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/Collection.cs index 40c8ed8..5d6c9c9 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/Collection.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/Collection.cs @@ -116,7 +116,25 @@ namespace System.Collections.ObjectModel InsertItem(index, item); } - public void InsertRange(int index, IEnumerable collection) => InsertItemsRange(index, collection); + public void InsertRange(int index, IEnumerable collection) + { + if (items.IsReadOnly) + { + ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); + } + + if (collection == null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list); + } + + if ((uint)index > (uint)items.Count) + { + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert); + } + + InsertItemsRange(index, collection); + } public bool Remove(T item) { @@ -131,9 +149,60 @@ namespace System.Collections.ObjectModel return true; } - public void RemoveRange(int index, int count) => RemoveItemsRange(index, count); + public void RemoveRange(int index, int count) + { + if (items.IsReadOnly) + { + ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); + } + + if ((uint)index > (uint)items.Count) + { + ThrowHelper.ThrowArgumentOutOfRange_IndexException(); + } + + if (count < 0) + { + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + } + + if (index > items.Count - count) + { + ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); + } + + RemoveItemsRange(index, count); + } + + public void ReplaceRange(int index, int count, IEnumerable collection) + { + if (items.IsReadOnly) + { + ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); + } - public void ReplaceRange(int index, int count, IEnumerable collection) => ReplaceItemsRange(index, count, collection); + if ((uint)index > (uint)items.Count) + { + ThrowHelper.ThrowArgumentOutOfRange_IndexException(); + } + + if (count < 0) + { + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + } + + if (index > items.Count - count) + { + ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); + } + + if (collection == null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list); + } + + ReplaceItemsRange(index, count, collection); + } public void RemoveAt(int index) { @@ -172,21 +241,6 @@ namespace System.Collections.ObjectModel protected virtual void InsertItemsRange(int index, IEnumerable collection) { - if (items.IsReadOnly) - { - ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); - } - - if (collection == null) - { - ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list); - } - - if ((uint)index > (uint)items.Count) - { - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert); - } - if (GetType() == typeof(Collection) && items is List list) { list.InsertRange(index, collection); @@ -202,26 +256,6 @@ namespace System.Collections.ObjectModel protected virtual void RemoveItemsRange(int index, int count) { - if (items.IsReadOnly) - { - ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); - } - - if ((uint)index > (uint)items.Count) - { - ThrowHelper.ThrowArgumentOutOfRange_IndexException(); - } - - if (count < 0) - { - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); - } - - if (index > items.Count - count) - { - ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); - } - if (GetType() == typeof(Collection) && items is List list) { list.RemoveRange(index, count); -- 2.7.4