Skip to content

Commit

Permalink
Fixed error messages on collections indexing (#67696)
Browse files Browse the repository at this point in the history
  • Loading branch information
B1Z0N authored Apr 13, 2022
1 parent e0560ba commit a9b3898
Show file tree
Hide file tree
Showing 72 changed files with 259 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ internal T get_Item<T>(int index)
T[] _this = Unsafe.As<T[]>(this);
if ((uint)index >= (uint)_this.Length)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException();
}

return _this[index];
Expand All @@ -407,7 +407,7 @@ internal void set_Item<T>(int index, T value)
T[] _this = Unsafe.As<T[]>(this);
if ((uint)index >= (uint)_this.Length)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException();
}

_this[index] = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void CopyTo(Array array!!, int index)
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);

if (array.Length - index < this.Count)
throw new ArgumentException(SR.ArgumentOutOfRange_Index, nameof(index));
throw new ArgumentException(SR.ArgumentOutOfRange_IndexMustBeLessOrEqual, nameof(index));

// the actual copy is a NOP
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ public T this[int index]
}
catch (IndexOutOfRangeException)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException();
return default; // unreachable
}
}
Expand All @@ -1246,7 +1246,7 @@ public T this[int index]
}
catch (IndexOutOfRangeException)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/Text/DBCSDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public override unsafe int GetChars(byte[] bytes!!, int byteIndex, int byteCount
throw new ArgumentOutOfRangeException(nameof(bytes), SR.ArgumentOutOfRange_IndexCountBuffer);

if (charIndex < 0 || charIndex > chars.Length)
throw new ArgumentOutOfRangeException(nameof(charIndex), SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(charIndex), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual);

if (chars.Length == 0)
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/Text/OSEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public override unsafe int GetBytes(char[] chars!!, int charIndex, int charCount
throw new ArgumentOutOfRangeException(nameof(chars), SR.ArgumentOutOfRange_IndexCountBuffer);

if (byteIndex < 0 || byteIndex > bytes.Length)
throw new ArgumentOutOfRangeException(nameof(byteIndex), SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(byteIndex), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual);

if (bytes.Length == 0)
return 0;
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/Common/src/System/Text/OSEncoding.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override unsafe int GetBytes(string s!!, int charIndex, int charCount, by
throw new ArgumentOutOfRangeException(nameof(s), SR.ArgumentOutOfRange_IndexCount);

if (byteIndex < 0 || byteIndex > bytes.Length)
throw new ArgumentOutOfRangeException(nameof(byteIndex), SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(byteIndex), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual);

if (charCount == 0)
return 0;
Expand All @@ -80,7 +80,7 @@ public override unsafe int GetBytes(char[] chars!!, int charIndex, int charCount
throw new ArgumentOutOfRangeException(nameof(chars), SR.ArgumentOutOfRange_IndexCountBuffer);

if (byteIndex < 0 || byteIndex > bytes.Length)
throw new ArgumentOutOfRangeException(nameof(byteIndex), SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(byteIndex), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual);

if (charCount == 0)
return 0;
Expand Down Expand Up @@ -123,7 +123,7 @@ public override unsafe int GetChars(byte[] bytes!!, int byteIndex, int byteCount
throw new ArgumentOutOfRangeException(nameof(bytes), SR.ArgumentOutOfRange_IndexCountBuffer);

if (charIndex < 0 || charIndex > chars.Length)
throw new ArgumentOutOfRangeException(nameof(charIndex), SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(charIndex), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual);

if (byteCount == 0)
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@
<data name="Argument_InvalidOffLen" xml:space="preserve">
<value>Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.</value>
</data>
<data name="ArgumentOutOfRange_Index" xml:space="preserve">
<data name="ArgumentOutOfRange_IndexMustBeLess" xml:space="preserve">
<value>Index was out of range. Must be non-negative and less than the size of the collection.</value>
</data>
<data name="ArgumentOutOfRange_IndexMustBeLessOrEqual" xml:space="preserve">
<value>Index was out of range. Must be non-negative and less than or equal to the size of the collection.</value>
</data>
<data name="ArgumentOutOfRange_NeedNonNegNum" xml:space="preserve">
<value>Non-negative number required.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void Clear()
public void RemoveAt(int index)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexMustBeLess);
object? temp = InnerList[index];
OnValidate(temp!);
OnRemove(index, temp);
Expand Down Expand Up @@ -117,13 +117,13 @@ void ICollection.CopyTo(Array array, int index)
get
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexMustBeLess);
return InnerList[index];
}
set
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexMustBeLess);
OnValidate(value!);
object? temp = InnerList[index];
OnSet(index, temp, value);
Expand Down Expand Up @@ -189,7 +189,7 @@ int IList.IndexOf(object? value)
void IList.Insert(int index, object? value)
{
if (index < 0 || index > Count)
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual);
OnValidate(value!);
OnInsert(index, value);
InnerList.Insert(index, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public virtual void CopyTo(Array array!!, int index)
if (array.Rank != 1)
throw new ArgumentException(SR.Arg_RankMultiDimNotSupported, nameof(array));
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexMustBeLess);

int arrayLen = array.Length;
if (arrayLen - index < _size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ private void EnsureCapacity(int min)
public virtual object? GetByIndex(int index)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexMustBeLess);
return values[index];
}

Expand Down Expand Up @@ -406,7 +406,7 @@ public virtual IDictionaryEnumerator GetEnumerator()
//
public virtual object GetKey(int index)
{
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexMustBeLess);
return keys[index];
}

Expand Down Expand Up @@ -514,7 +514,7 @@ private void Insert(int index, object key, object? value)
//
public virtual void RemoveAt(int index)
{
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexMustBeLess);
_size--;
if (index < _size)
{
Expand Down Expand Up @@ -542,7 +542,7 @@ public virtual void Remove(object key)
//
public virtual void SetByIndex(int index, object? value)
{
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index);
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexMustBeLess);
values[index] = value;
version++;
}
Expand Down
5 changes: 4 additions & 1 deletion src/libraries/System.Collections/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,12 @@
<data name="ArgumentOutOfRange_BiggerThanCollection" xml:space="preserve">
<value>Must be less than or equal to the size of the collection.</value>
</data>
<data name="ArgumentOutOfRange_Index" xml:space="preserve">
<data name="ArgumentOutOfRange_IndexMustBeLess" xml:space="preserve">
<value>Index was out of range. Must be non-negative and less than the size of the collection.</value>
</data>
<data name="ArgumentOutOfRange_IndexMustBeLessOrEqual" xml:space="preserve">
<value>Index was out of range. Must be non-negative and less than or equal to the size of the collection.</value>
</data>
<data name="ExternalLinkedListNode" xml:space="preserve">
<value>The LinkedList node does not belong to current LinkedList.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ private static int Div4Rem(int number, out int remainder)

private static void ThrowArgumentOutOfRangeException(int index)
{
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_IndexMustBeLess);
}

private sealed class BitArrayEnumeratorSimple : IEnumerator, ICloneable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ void ICollection.CopyTo(Array array!!, int index)

if (index < 0 || index > array.Length)
{
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_IndexMustBeLessOrEqual);
}

if (array.Length - index < _queue._size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[]
{
if (arrayIndex < 0 || arrayIndex > array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex), arrayIndex, SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(arrayIndex), arrayIndex, SR.ArgumentOutOfRange_IndexMustBeLessOrEqual);
}

if (array.Length - arrayIndex < Count)
Expand Down Expand Up @@ -468,7 +468,7 @@ void ICollection.CopyTo(Array array!!, int index)

if (index < 0 || index > array.Length)
{
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_IndexMustBeLessOrEqual);
}

if (array.Length - index < Count)
Expand Down Expand Up @@ -522,15 +522,15 @@ private void EnsureCapacity(int min)
public TValue GetValueAtIndex(int index)
{
if (index < 0 || index >= _size)
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_IndexMustBeLess);
return values[index];
}

// Sets the value of the entry at the given index.
public void SetValueAtIndex(int index, TValue value)
{
if (index < 0 || index >= _size)
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_IndexMustBeLess);
values[index] = value;
version++;
}
Expand Down Expand Up @@ -559,7 +559,7 @@ IEnumerator IEnumerable.GetEnumerator()
public TKey GetKeyAtIndex(int index)
{
if (index < 0 || index >= _size)
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_IndexMustBeLess);
return keys[index];
}

Expand Down Expand Up @@ -681,7 +681,7 @@ public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
public void RemoveAt(int index)
{
if (index < 0 || index >= _size)
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_IndexMustBeLess);
_size--;
if (index < _size)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void CopyTo(T[] array!!, int arrayIndex)
{
if (arrayIndex < 0 || arrayIndex > array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex), arrayIndex, SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(arrayIndex), arrayIndex, SR.ArgumentOutOfRange_IndexMustBeLessOrEqual);
}

if (array.Length - arrayIndex < _size)
Expand Down Expand Up @@ -127,7 +127,7 @@ void ICollection.CopyTo(Array array!!, int arrayIndex)

if (arrayIndex < 0 || arrayIndex > array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex), arrayIndex, SR.ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(arrayIndex), arrayIndex, SR.ArgumentOutOfRange_IndexMustBeLessOrEqual);
}

if (array.Length - arrayIndex < _size)
Expand Down
5 changes: 4 additions & 1 deletion src/libraries/System.Console/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,12 @@
<data name="ArgumentOutOfRange_IndexCount" xml:space="preserve">
<value>Index and count must refer to a location within the string.</value>
</data>
<data name="ArgumentOutOfRange_Index" xml:space="preserve">
<data name="ArgumentOutOfRange_IndexMustBeLess" xml:space="preserve">
<value>Index was out of range. Must be non-negative and less than the size of the collection.</value>
</data>
<data name="ArgumentOutOfRange_IndexMustBeLessOrEqual" xml:space="preserve">
<value>Index was out of range. Must be non-negative and less than or equal to the size of the collection.</value>
</data>
<data name="Argument_EncodingConversionOverflowBytes" xml:space="preserve">
<value>The output byte buffer is too small to contain the encoded data, encoding '{0}' fallback '{1}'.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,12 @@
<data name="ArgumentOutOfRange_IndexCount" xml:space="preserve">
<value>Index and count must refer to a location within the string.</value>
</data>
<data name="ArgumentOutOfRange_Index" xml:space="preserve">
<data name="ArgumentOutOfRange_IndexMustBeLess" xml:space="preserve">
<value>Index was out of range. Must be non-negative and less than the size of the collection.</value>
</data>
<data name="ArgumentOutOfRange_IndexMustBeLessOrEqual" xml:space="preserve">
<value>Index was out of range. Must be non-negative and less than or equal to the size of the collection.</value>
</data>
<data name="Argument_EncodingConversionOverflowBytes" xml:space="preserve">
<value>The output byte buffer is too small to contain the encoded data, encoding '{0}' fallback '{1}'.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1699,9 +1699,12 @@
<data name="ArgumentOutOfRange_HugeArrayNotSupported" xml:space="preserve">
<value>Arrays larger than 2GB are not supported.</value>
</data>
<data name="ArgumentOutOfRange_Index" xml:space="preserve">
<data name="ArgumentOutOfRange_IndexMustBeLess" xml:space="preserve">
<value>Index was out of range. Must be non-negative and less than the size of the collection.</value>
</data>
<data name="ArgumentOutOfRange_IndexMustBeLessOrEqual" xml:space="preserve">
<value>Index was out of range. Must be non-negative and less than or equal to the size of the collection.</value>
</data>
<data name="ArgumentOutOfRange_IndexCount" xml:space="preserve">
<value>Index and count must refer to a location within the string.</value>
</data>
Expand Down
Loading

0 comments on commit a9b3898

Please sign in to comment.