Skip to content

Commit bd9e4a3

Browse files
committed
Use ArgumentOutOfRangeException.Throw helpers in more places
1 parent 9a65109 commit bd9e4a3

File tree

210 files changed

+855
-2306
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

210 files changed

+855
-2306
lines changed

src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,16 @@ private static unsafe void Copy(Array sourceArray, int sourceIndex, Array destin
9494
if (sourceArray.GetType() != destinationArray.GetType() && sourceArray.Rank != destinationArray.Rank)
9595
throw new RankException(SR.Rank_MustMatch);
9696

97-
if (length < 0)
98-
throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NeedNonNegNum);
97+
ArgumentOutOfRangeException.ThrowIfNegative(length);
9998

10099
int srcLB = sourceArray.GetLowerBound(0);
101-
if (sourceIndex < srcLB || sourceIndex - srcLB < 0)
102-
throw new ArgumentOutOfRangeException(nameof(sourceIndex), SR.ArgumentOutOfRange_ArrayLB);
100+
ArgumentOutOfRangeException.ThrowIfLessThan(sourceIndex, srcLB);
101+
ArgumentOutOfRangeException.ThrowIfNegative(sourceIndex - srcLB, nameof(sourceIndex));
103102
sourceIndex -= srcLB;
104103

105104
int dstLB = destinationArray.GetLowerBound(0);
106-
if (destinationIndex < dstLB || destinationIndex - dstLB < 0)
107-
throw new ArgumentOutOfRangeException(nameof(destinationIndex), SR.ArgumentOutOfRange_ArrayLB);
105+
ArgumentOutOfRangeException.ThrowIfLessThan(destinationIndex, dstLB);
106+
ArgumentOutOfRangeException.ThrowIfNegative(destinationIndex - dstLB, nameof(destinationIndex));
108107
destinationIndex -= dstLB;
109108

110109
if ((uint)(sourceIndex + length) > sourceArray.NativeLength)

src/coreclr/System.Private.CoreLib/src/System/Collections/EmptyReadOnlyDictionaryInternal.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public void CopyTo(Array array, int index)
4040
if (array.Rank != 1)
4141
throw new ArgumentException(SR.Arg_RankMultiDimNotSupported);
4242

43-
if (index < 0)
44-
throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);
43+
ArgumentOutOfRangeException.ThrowIfNegative(index);
4544

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

src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -136,33 +136,21 @@ internal enum GC_ALLOC_FLAGS
136136

137137
public static void AddMemoryPressure(long bytesAllocated)
138138
{
139-
if (bytesAllocated <= 0)
139+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bytesAllocated);
140+
if (IntPtr.Size == 4)
140141
{
141-
throw new ArgumentOutOfRangeException(nameof(bytesAllocated),
142-
SR.ArgumentOutOfRange_NeedPosNum);
143-
}
144-
145-
if ((4 == IntPtr.Size) && (bytesAllocated > int.MaxValue))
146-
{
147-
throw new ArgumentOutOfRangeException(nameof(bytesAllocated),
148-
SR.ArgumentOutOfRange_MustBeNonNegInt32);
142+
ArgumentOutOfRangeException.ThrowIfGreaterThan(bytesAllocated, int.MaxValue);
149143
}
150144

151145
_AddMemoryPressure((ulong)bytesAllocated);
152146
}
153147

154148
public static void RemoveMemoryPressure(long bytesAllocated)
155149
{
156-
if (bytesAllocated <= 0)
157-
{
158-
throw new ArgumentOutOfRangeException(nameof(bytesAllocated),
159-
SR.ArgumentOutOfRange_NeedPosNum);
160-
}
161-
162-
if ((4 == IntPtr.Size) && (bytesAllocated > int.MaxValue))
150+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bytesAllocated);
151+
if (IntPtr.Size == 4)
163152
{
164-
throw new ArgumentOutOfRangeException(nameof(bytesAllocated),
165-
SR.ArgumentOutOfRange_MustBeNonNegInt32);
153+
ArgumentOutOfRangeException.ThrowIfGreaterThan(bytesAllocated, int.MaxValue);
166154
}
167155

168156
_RemoveMemoryPressure((ulong)bytesAllocated);
@@ -202,10 +190,7 @@ public static void Collect(int generation, GCCollectionMode mode, bool blocking)
202190

203191
public static void Collect(int generation, GCCollectionMode mode, bool blocking, bool compacting)
204192
{
205-
if (generation < 0)
206-
{
207-
throw new ArgumentOutOfRangeException(nameof(generation), SR.ArgumentOutOfRange_GenericPositive);
208-
}
193+
ArgumentOutOfRangeException.ThrowIfNegative(generation);
209194

210195
if ((mode < GCCollectionMode.Default) || (mode > GCCollectionMode.Aggressive))
211196
{
@@ -253,10 +238,7 @@ public static void Collect(int generation, GCCollectionMode mode, bool blocking,
253238

254239
public static int CollectionCount(int generation)
255240
{
256-
if (generation < 0)
257-
{
258-
throw new ArgumentOutOfRangeException(nameof(generation), SR.ArgumentOutOfRange_GenericPositive);
259-
}
241+
ArgumentOutOfRangeException.ThrowIfNegative(generation);
260242
return _CollectionCount(generation, 0);
261243
}
262244

@@ -447,8 +429,7 @@ public static GCNotificationStatus WaitForFullGCApproach()
447429

448430
public static GCNotificationStatus WaitForFullGCApproach(int millisecondsTimeout)
449431
{
450-
if (millisecondsTimeout < -1)
451-
throw new ArgumentOutOfRangeException(nameof(millisecondsTimeout), SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
432+
ArgumentOutOfRangeException.ThrowIfLessThan(millisecondsTimeout, -1);
452433

453434
return (GCNotificationStatus)_WaitForFullGCApproach(millisecondsTimeout);
454435
}
@@ -460,8 +441,7 @@ public static GCNotificationStatus WaitForFullGCComplete()
460441

461442
public static GCNotificationStatus WaitForFullGCComplete(int millisecondsTimeout)
462443
{
463-
if (millisecondsTimeout < -1)
464-
throw new ArgumentOutOfRangeException(nameof(millisecondsTimeout), SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
444+
ArgumentOutOfRangeException.ThrowIfLessThan(millisecondsTimeout, -1);
465445
return (GCNotificationStatus)_WaitForFullGCComplete(millisecondsTimeout);
466446
}
467447

@@ -483,22 +463,13 @@ private enum EndNoGCRegionStatus
483463

484464
private static bool StartNoGCRegionWorker(long totalSize, bool hasLohSize, long lohSize, bool disallowFullBlockingGC)
485465
{
486-
if (totalSize <= 0)
487-
{
488-
throw new ArgumentOutOfRangeException(nameof(totalSize), SR.ArgumentOutOfRange_MustBePositive);
489-
}
466+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(totalSize);
490467

491468
if (hasLohSize)
492469
{
493-
if (lohSize <= 0)
494-
{
495-
throw new ArgumentOutOfRangeException(nameof(lohSize), SR.ArgumentOutOfRange_MustBePositive);
496-
}
470+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(lohSize);
497471

498-
if (lohSize > totalSize)
499-
{
500-
throw new ArgumentOutOfRangeException(nameof(lohSize), SR.ArgumentOutOfRange_NoGCLohSizeGreaterTotalSize);
501-
}
472+
ArgumentOutOfRangeException.ThrowIfGreaterThan(lohSize, totalSize);
502473
}
503474

504475
StartNoGCRegionStatus status = (StartNoGCRegionStatus)_StartNoGCRegion(totalSize, hasLohSize, lohSize, disallowFullBlockingGC);

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -907,8 +907,7 @@ public void SetCode(byte[]? code, int maxStackSize)
907907
[CLSCompliant(false)]
908908
public unsafe void SetCode(byte* code, int codeSize, int maxStackSize)
909909
{
910-
if (codeSize < 0)
911-
throw new ArgumentOutOfRangeException(nameof(codeSize), SR.ArgumentOutOfRange_GenericPositive);
910+
ArgumentOutOfRangeException.ThrowIfNegative(codeSize);
912911
if (codeSize > 0)
913912
ArgumentNullException.ThrowIfNull(code);
914913

@@ -924,8 +923,7 @@ public void SetExceptions(byte[]? exceptions)
924923
[CLSCompliant(false)]
925924
public unsafe void SetExceptions(byte* exceptions, int exceptionsSize)
926925
{
927-
if (exceptionsSize < 0)
928-
throw new ArgumentOutOfRangeException(nameof(exceptionsSize), SR.ArgumentOutOfRange_GenericPositive);
926+
ArgumentOutOfRangeException.ThrowIfNegative(exceptionsSize);
929927

930928
if (exceptionsSize > 0)
931929
ArgumentNullException.ThrowIfNull(exceptions);
@@ -941,9 +939,7 @@ public void SetLocalSignature(byte[]? localSignature)
941939
[CLSCompliant(false)]
942940
public unsafe void SetLocalSignature(byte* localSignature, int signatureSize)
943941
{
944-
if (signatureSize < 0)
945-
throw new ArgumentOutOfRangeException(nameof(signatureSize), SR.ArgumentOutOfRange_GenericPositive);
946-
942+
ArgumentOutOfRangeException.ThrowIfNegative(signatureSize);
947943
if (signatureSize > 0)
948944
ArgumentNullException.ThrowIfNull(localSignature);
949945

src/coreclr/System.Private.CoreLib/src/System/Text/StringBuilder.CoreCLR.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ private int GetReplaceBufferCapacity(int requiredCapacity)
2626

2727
internal unsafe void ReplaceBufferInternal(char* newBuffer, int newLength)
2828
{
29-
if (newLength > m_MaxCapacity)
30-
throw new ArgumentOutOfRangeException("capacity", SR.ArgumentOutOfRange_Capacity);
29+
ArgumentOutOfRangeException.ThrowIfGreaterThan(newLength, m_MaxCapacity, "capacity");
3130

3231
if (newLength > m_ChunkChars.Length)
3332
{
@@ -42,8 +41,7 @@ internal unsafe void ReplaceBufferInternal(char* newBuffer, int newLength)
4241

4342
internal void ReplaceBufferUtf8Internal(ReadOnlySpan<byte> source)
4443
{
45-
if (source.Length > m_MaxCapacity)
46-
throw new ArgumentOutOfRangeException("capacity", SR.ArgumentOutOfRange_Capacity);
44+
ArgumentOutOfRangeException.ThrowIfGreaterThan(source.Length, m_MaxCapacity, "capacity");
4745

4846
int numChars = Encoding.UTF8.GetCharCount(source);
4947
if (numChars > m_ChunkChars.Length)
@@ -58,8 +56,7 @@ internal void ReplaceBufferUtf8Internal(ReadOnlySpan<byte> source)
5856

5957
internal unsafe void ReplaceBufferAnsiInternal(sbyte* newBuffer, int newLength)
6058
{
61-
if (newLength > m_MaxCapacity)
62-
throw new ArgumentOutOfRangeException("capacity", SR.ArgumentOutOfRange_Capacity);
59+
ArgumentOutOfRangeException.ThrowIfGreaterThan(newLength, m_MaxCapacity, "capacity");
6360

6461
if (newLength > m_ChunkChars.Length)
6562
{

src/coreclr/System.Private.CoreLib/src/System/Threading/Monitor.CoreCLR.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ public static bool IsEntered(object obj)
151151
public static bool Wait(object obj, int millisecondsTimeout)
152152
{
153153
ArgumentNullException.ThrowIfNull(obj);
154-
if (millisecondsTimeout < -1)
155-
throw new ArgumentOutOfRangeException(nameof(millisecondsTimeout), SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
154+
ArgumentOutOfRangeException.ThrowIfLessThan(millisecondsTimeout, -1);
156155

157156
return ObjWait(millisecondsTimeout, obj);
158157
}

src/coreclr/nativeaot/System.Private.CoreLib/src/System/GC.NativeAot.cs

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@ public static void Collect(int generation, GCCollectionMode mode, bool blocking)
122122

123123
public static void Collect(int generation, GCCollectionMode mode, bool blocking, bool compacting)
124124
{
125-
if (generation < 0)
126-
{
127-
throw new ArgumentOutOfRangeException(nameof(generation), SR.ArgumentOutOfRange_GenericPositive);
128-
}
125+
ArgumentOutOfRangeException.ThrowIfNegative(generation);
129126

130127
if ((mode < GCCollectionMode.Default) || (mode > GCCollectionMode.Aggressive))
131128
{
@@ -224,12 +221,7 @@ public static GCNotificationStatus WaitForFullGCApproach()
224221
/// <returns>The status of a registered full GC notification</returns>
225222
public static GCNotificationStatus WaitForFullGCApproach(int millisecondsTimeout)
226223
{
227-
if (millisecondsTimeout < -1)
228-
{
229-
throw new ArgumentOutOfRangeException(
230-
nameof(millisecondsTimeout),
231-
SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
232-
}
224+
ArgumentOutOfRangeException.ThrowIfLessThan(millisecondsTimeout, -1);
233225

234226
return (GCNotificationStatus)RuntimeImports.RhWaitForFullGCApproach(millisecondsTimeout);
235227
}
@@ -252,12 +244,7 @@ public static GCNotificationStatus WaitForFullGCComplete()
252244
/// <returns></returns>
253245
public static GCNotificationStatus WaitForFullGCComplete(int millisecondsTimeout)
254246
{
255-
if (millisecondsTimeout < -1)
256-
{
257-
throw new ArgumentOutOfRangeException(
258-
nameof(millisecondsTimeout),
259-
SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
260-
}
247+
ArgumentOutOfRangeException.ThrowIfLessThan(millisecondsTimeout, -1);
261248

262249
return (GCNotificationStatus)RuntimeImports.RhWaitForFullGCComplete(millisecondsTimeout);
263250
}
@@ -341,22 +328,10 @@ public static bool TryStartNoGCRegion(long totalSize, long lohSize, bool disallo
341328

342329
private static bool StartNoGCRegionWorker(long totalSize, bool hasLohSize, long lohSize, bool disallowFullBlockingGC)
343330
{
344-
if (totalSize <= 0)
345-
{
346-
throw new ArgumentOutOfRangeException(
347-
nameof(totalSize),
348-
SR.Format(SR.ArgumentOutOfRange_MustBePositive, nameof(totalSize)));
349-
}
350-
331+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(totalSize);
351332
if (hasLohSize)
352333
{
353-
if (lohSize <= 0)
354-
{
355-
throw new ArgumentOutOfRangeException(
356-
nameof(lohSize),
357-
SR.Format(SR.ArgumentOutOfRange_MustBePositive, nameof(lohSize)));
358-
}
359-
334+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(lohSize);
360335
if (lohSize > totalSize)
361336
{
362337
throw new ArgumentOutOfRangeException(nameof(lohSize), SR.ArgumentOutOfRange_NoGCLohSizeGreaterTotalSize);
@@ -445,8 +420,7 @@ public static int MaxGeneration
445420

446421
public static int CollectionCount(int generation)
447422
{
448-
if (generation < 0)
449-
throw new ArgumentOutOfRangeException(nameof(generation), SR.ArgumentOutOfRange_GenericPositive);
423+
ArgumentOutOfRangeException.ThrowIfNegative(generation);
450424

451425
return RuntimeImports.RhGetGcCollectionCount(generation, false);
452426
}
@@ -527,18 +501,9 @@ private static long InterlockedAddMemoryPressure(ref long pAugend, long addend)
527501
/// <param name="bytesAllocated"></param>
528502
public static void AddMemoryPressure(long bytesAllocated)
529503
{
530-
if (bytesAllocated <= 0)
531-
{
532-
throw new ArgumentOutOfRangeException(nameof(bytesAllocated),
533-
SR.ArgumentOutOfRange_NeedPosNum);
534-
}
535-
504+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bytesAllocated);
536505
#if !TARGET_64BIT
537-
if (bytesAllocated > int.MaxValue)
538-
{
539-
throw new ArgumentOutOfRangeException(nameof(bytesAllocated),
540-
SR.ArgumentOutOfRange_MustBeNonNegInt32);
541-
}
506+
ArgumentOutOfRangeException.ThrowIfGreaterThan(bytesAllocated, int.MaxValue);
542507
#endif
543508

544509
CheckCollectionCount();
@@ -654,18 +619,9 @@ public static unsafe IReadOnlyDictionary<string, object> GetConfigurationVariabl
654619

655620
public static void RemoveMemoryPressure(long bytesAllocated)
656621
{
657-
if (bytesAllocated <= 0)
658-
{
659-
throw new ArgumentOutOfRangeException(nameof(bytesAllocated),
660-
SR.ArgumentOutOfRange_NeedPosNum);
661-
}
662-
622+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bytesAllocated);
663623
#if !TARGET_64BIT
664-
if (bytesAllocated > int.MaxValue)
665-
{
666-
throw new ArgumentOutOfRangeException(nameof(bytesAllocated),
667-
SR.ArgumentOutOfRange_MustBeNonNegInt32);
668-
}
624+
ArgumentOutOfRangeException.ThrowIfGreaterThan(bytesAllocated, int.MaxValue);
669625
#endif
670626

671627
CheckCollectionCount();

src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ public static unsafe string GetProcPath(int pid)
103103
public static unsafe ProcessInfo GetProcessInfoById(int pid)
104104
{
105105
// Negative PIDs are invalid
106-
if (pid < 0)
107-
{
108-
throw new ArgumentOutOfRangeException(nameof(pid));
109-
}
106+
ArgumentOutOfRangeException.ThrowIfNegative(pid);
110107

111108
ProcessInfo info;
112109

src/libraries/Common/src/Interop/OSX/Interop.libproc.GetProcessInfoById.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ private static unsafe partial int proc_pidinfo(
108108
internal static unsafe proc_taskallinfo? GetProcessInfoById(int pid)
109109
{
110110
// Negative PIDs are invalid
111-
if (pid < 0)
112-
{
113-
throw new ArgumentOutOfRangeException(nameof(pid));
114-
}
111+
ArgumentOutOfRangeException.ThrowIfNegative(pid);
115112

116113
// Get the process information for the specified pid
117114
int size = sizeof(proc_taskallinfo);

0 commit comments

Comments
 (0)