Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -626,14 +626,10 @@ private static bool InvokeMemoryLoadChangeNotifications()
/// <param name="notification">delegate to invoke when operation occurs</param>s
internal static void RegisterMemoryLoadChangeNotification(float lowMemoryPercent, float highMemoryPercent, Action notification)
{
if (highMemoryPercent < 0 || highMemoryPercent > 1.0 || highMemoryPercent <= lowMemoryPercent)
{
throw new ArgumentOutOfRangeException(nameof(highMemoryPercent));
}
if (lowMemoryPercent < 0)
{
throw new ArgumentOutOfRangeException(nameof(lowMemoryPercent));
}
ArgumentOutOfRangeException.ThrowIfLessThan(highMemoryPercent, 0);
ArgumentOutOfRangeException.ThrowIfGreaterThan(highMemoryPercent, 1.0);
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(highMemoryPercent, lowMemoryPercent);
ArgumentOutOfRangeException.ThrowIfLessThan(lowMemoryPercent, 0);
ArgumentNullException.ThrowIfNull(notification);

lock (s_notifications)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,7 @@ public void SetSignature(

public ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, string? strParamName)
{
if (position < 0)
throw new ArgumentOutOfRangeException(SR.ArgumentOutOfRange_ParamSequence);
ArgumentOutOfRangeException.ThrowIfNegative(position);

ThrowIfGeneric();
m_containingType.ThrowIfCreated();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,7 @@ public static IntPtr AllocateTypeAssociatedMemory(Type type, int size)
if (type is not RuntimeType rt)
throw new ArgumentException(SR.Arg_MustBeType, nameof(type));

if (size < 0)
throw new ArgumentOutOfRangeException(nameof(size));
ArgumentOutOfRangeException.ThrowIfNegative(size);

return AllocateTypeAssociatedMemory(new QCallTypeHandle(ref rt), (uint)size);
}
Expand Down
24 changes: 21 additions & 3 deletions src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1862,6 +1862,27 @@
<data name="ArgumentOutOfRange_Year" xml:space="preserve">
<value>Year must be between 1 and 9999.</value>
</data>
<data name="ArgumentOutOfRange_Generic_MustBeNonZero" xml:space="preserve">
<value>'{0}' must be a non-zero value.</value>
</data>
<data name="ArgumentOutOfRange_Generic_MustBeNonNegative" xml:space="preserve">
<value>'{0}' must be a non-negative value.</value>
</data>
<data name="ArgumentOutOfRange_Generic_MustBeNonNegativeNonZero" xml:space="preserve">
<value>'{0}' must be a non-negative and non-zero value.</value>
</data>
<data name="ArgumentOutOfRange_Generic_MustBeLessOrEqual" xml:space="preserve">
<value>'{0}' must be less than or equal to '{1}'.</value>
</data>
<data name="ArgumentOutOfRange_Generic_MustBeLess" xml:space="preserve">
<value>'{0}' must be less than '{1}'.</value>
</data>
<data name="ArgumentOutOfRange_Generic_MustBeGreaterOrEqual" xml:space="preserve">
<value>'{0}' must be greater than or equal to '{1}'.</value>
</data>
<data name="ArgumentOutOfRange_Generic_MustBeGreater" xml:space="preserve">
<value>'{0}' must be greater than '{1}'.</value>
</data>
<data name="Arithmetic_NaN" xml:space="preserve">
<value>Function does not accept floating point Not-a-Number values.</value>
</data>
Expand Down Expand Up @@ -2735,9 +2756,6 @@
<data name="Lazy_Value_RecursiveCallsToValue" xml:space="preserve">
<value>ValueFactory attempted to access the Value property of this instance.</value>
</data>
<data name="ManualResetEventSlim_ctor_SpinCountOutOfRange" xml:space="preserve">
<value>The spinCount argument must be in the range 0 to {0}, inclusive.</value>
</data>
<data name="ManualResetEventSlim_ctor_TooManyWaiters" xml:space="preserve">
<value>There are too many threads currently waiting on the event. A maximum of {0} waiting threads are supported.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
=============================================================================*/

using System.Runtime.Serialization;
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;

namespace System
{
Expand Down Expand Up @@ -85,5 +88,121 @@ public override string Message

// Gets the value of the argument that caused the exception.
public virtual object? ActualValue => _actualValue;

[DoesNotReturn]
private static void ThrowZero(string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonZero, paramName));
}

[DoesNotReturn]
private static void ThrowNegative(string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegative, paramName));
}

[DoesNotReturn]
private static void ThrowNegativeOrZero(string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegativeNonZero, paramName));
}

[DoesNotReturn]
private static void ThrowGreater<T>(string? paramName, T other)
{
throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLessOrEqual, paramName, other));
}

[DoesNotReturn]
private static void ThrowGreaterEqual<T>(string? paramName, T other)
{
throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLess, paramName, other));
}

[DoesNotReturn]
private static void ThrowLess<T>(string? paramName, T other)
{
throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreaterOrEqual, paramName, other));
}

[DoesNotReturn]
private static void ThrowLessEqual<T>(string? paramName, T other)
{
throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreater, paramName, other));
}

/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is zero.</summary>
/// <param name="value">The argument to validate as non-zero.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
public static void ThrowIfZero<T>(T value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
where T : INumberBase<T>
{
if (T.IsZero(value))
ThrowZero(paramName);
}

/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is negative.</summary>
/// <param name="value">The argument to validate as non-negative.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
public static void ThrowIfNegative<T>(T value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
where T : INumberBase<T>
{
if (T.IsNegative(value))
ThrowNegative(paramName);
}

/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is negative or zero.</summary>
/// <param name="value">The argument to validate as non-zero or non-negative.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
public static void ThrowIfNegativeOrZero<T>(T value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
where T : INumberBase<T>
{
if (T.IsNegative(value) || T.IsZero(value))
Comment thread
stephentoub marked this conversation as resolved.
ThrowNegativeOrZero(paramName);
}

/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is greater than <paramref name="other"/>.</summary>
/// <param name="value">The argument to validate as less or equal than <paramref name="other"/>.</param>
/// <param name="other">The value to compare with <paramref name="value"/>.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
public static void ThrowIfGreaterThan<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null)
where T : IComparable<T>
{
if (value.CompareTo(other) > 0)
Comment thread
stephentoub marked this conversation as resolved.
ThrowGreater(paramName, other);
}

/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is greater than or equal <paramref name="other"/>.</summary>
/// <param name="value">The argument to validate as less than <paramref name="other"/>.</param>
/// <param name="other">The value to compare with <paramref name="value"/>.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
public static void ThrowIfGreaterThanOrEqual<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null)
where T : IComparable<T>
{
if (value.CompareTo(other) >= 0)
ThrowGreaterEqual(paramName, other);
}

/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is less than <paramref name="other"/>.</summary>
/// <param name="value">The argument to validate as greatar than or equal than <paramref name="other"/>.</param>
/// <param name="other">The value to compare with <paramref name="value"/>.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
public static void ThrowIfLessThan<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null)
where T : IComparable<T>
{
if (value.CompareTo(other) < 0)
ThrowLess(paramName, other);
}

/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is less than or equal <paramref name="other"/>.</summary>
/// <param name="value">The argument to validate as greatar than than <paramref name="other"/>.</param>
/// <param name="other">The value to compare with <paramref name="value"/>.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
public static void ThrowIfLessThanOrEqual<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null)
where T : IComparable<T>
{
if (value.CompareTo(other) <= 0)
ThrowLessEqual(paramName, other);
}
}
}
10 changes: 3 additions & 7 deletions src/libraries/System.Private.CoreLib/src/System/BitConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,7 @@ public static string ToString(byte[] value, int startIndex, int length)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
if (startIndex < 0 || startIndex >= value.Length && startIndex > 0)
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLess);
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_GenericPositive);
ArgumentOutOfRangeException.ThrowIfNegative(length);
if (startIndex > value.Length - length)
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall, ExceptionArgument.value);

Expand All @@ -667,11 +666,8 @@ public static string ToString(byte[] value, int startIndex, int length)
return string.Empty;
}

if (length > (int.MaxValue / 3))
{
// (int.MaxValue / 3) == 715,827,882 Bytes == 699 MB
throw new ArgumentOutOfRangeException(nameof(length), SR.Format(SR.ArgumentOutOfRange_LengthTooLarge, int.MaxValue / 3));
}
// (int.MaxValue / 3) == 715,827,882 Bytes == 699 MB
ArgumentOutOfRangeException.ThrowIfGreaterThan(length, int.MaxValue / 3);

return string.Create(length * 3 - 1, (value, startIndex, length), static (dst, state) =>
{
Expand Down
9 changes: 3 additions & 6 deletions src/libraries/System.Private.CoreLib/src/System/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@ public static unsafe void BlockCopy(Array src, int srcOffset, Array dst, int dst
}
}

if (srcOffset < 0)
throw new ArgumentOutOfRangeException(nameof(srcOffset), SR.ArgumentOutOfRange_MustBeNonNegInt32);
if (dstOffset < 0)
throw new ArgumentOutOfRangeException(nameof(dstOffset), SR.ArgumentOutOfRange_MustBeNonNegInt32);
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_MustBeNonNegInt32);
ArgumentOutOfRangeException.ThrowIfNegative(srcOffset);
ArgumentOutOfRangeException.ThrowIfNegative(dstOffset);
ArgumentOutOfRangeException.ThrowIfNegative(count);

nuint uCount = (nuint)count;
nuint uSrcOffset = (nuint)srcOffset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ internal ConfigurableArrayPool() : this(DefaultMaxArrayLength, DefaultMaxNumberO

internal ConfigurableArrayPool(int maxArrayLength, int maxArraysPerBucket)
{
if (maxArrayLength <= 0)
{
throw new ArgumentOutOfRangeException(nameof(maxArrayLength));
}
if (maxArraysPerBucket <= 0)
{
throw new ArgumentOutOfRangeException(nameof(maxArraysPerBucket));
}
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(maxArrayLength);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(maxArraysPerBucket);

// Our bucketing algorithm has a min length of 2^4 and a max length of 2^30.
// Constrain the actual max used to those values.
Expand Down Expand Up @@ -61,11 +55,8 @@ public override T[] Rent(int minimumLength)
// Arrays can't be smaller than zero. We allow requesting zero-length arrays (even though
// pooling such an array isn't valuable) as it's a valid length array, and we want the pool
// to be usable in general instead of using `new`, even for computed lengths.
if (minimumLength < 0)
{
throw new ArgumentOutOfRangeException(nameof(minimumLength));
}
else if (minimumLength == 0)
ArgumentOutOfRangeException.ThrowIfNegative(minimumLength);
if (minimumLength == 0)
{
// No need for events with the empty array. Our pool is effectively infinite
// and we'll never allocate for rents and never store for returns.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ public override T[] Rent(int minimumLength)
// effectively infinite for empty arrays and we'll never allocate for rents and never store for returns.
return Array.Empty<T>();
}
else if (minimumLength < 0)
else
{
throw new ArgumentOutOfRangeException(nameof(minimumLength));
ArgumentOutOfRangeException.ThrowIfNegative(minimumLength);
}

buffer = GC.AllocateUninitializedArray<T>(minimumLength);
Expand Down
Loading