Skip to content

Commit 248045d

Browse files
authored
manual uses of ArgumentOutOfRangeException throw helpers (#8464)
1 parent 7cdd4b5 commit 248045d

File tree

75 files changed

+369
-672
lines changed

Some content is hidden

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

75 files changed

+369
-672
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ internal CheckedPointer(UnmanagedMemoryStream stream)
6060
Debug.Assert(stream.Position == 0);
6161
unsafe { _pointer = stream.PositionPointer; }
6262
long length = stream.Length;
63-
if (length < 0 || length > int.MaxValue)
64-
throw new ArgumentOutOfRangeException();
63+
ArgumentOutOfRangeException.ThrowIfNegative(length);
64+
ArgumentOutOfRangeException.ThrowIfGreaterThan(length, int.MaxValue);
65+
6566
_size = (int)length;
6667
}
6768

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontFaceLayoutInfo.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -737,10 +737,9 @@ public void CopyTo(KeyValuePair<int, ushort>[] array, int arrayIndex)
737737
// The extra "arrayIndex >= array.Length" check in because even if _collection.Count
738738
// is 0 the index is not allowed to be equal or greater than the length
739739
// (from the MSDN ICollection docs)
740-
if (arrayIndex < 0 || arrayIndex >= array.Length || (arrayIndex + Count) > array.Length)
741-
{
742-
throw new ArgumentOutOfRangeException("arrayIndex");
743-
}
740+
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
741+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(arrayIndex, array.Length);
742+
ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);
744743

745744
foreach (KeyValuePair<int, ushort> pair in this)
746745
array[arrayIndex++] = pair;

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontFace/FontDriver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ internal void SetFace(int faceIndex)
168168
{
169169
if (_technology == FontTechnology.TrueTypeCollection)
170170
{
171-
if (faceIndex < 0 || faceIndex >= _numFaces)
172-
throw new ArgumentOutOfRangeException("faceIndex");
171+
ArgumentOutOfRangeException.ThrowIfNegative(faceIndex);
172+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(faceIndex, _numFaces);
173173
}
174174
else
175175
{

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontFace/FontFamilyIdentifier.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ internal CanonicalFontFamilyReference this[int tokenIndex]
202202
{
203203
get
204204
{
205-
if (tokenIndex < 0 || tokenIndex >= Count)
206-
throw new ArgumentOutOfRangeException("tokenIndex");
205+
ArgumentOutOfRangeException.ThrowIfNegative(tokenIndex);
206+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(tokenIndex, Count);
207207

208208
// Have we already been canonicalized?
209209
if (_canonicalReferences != null)

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontFace/TypefaceCollection.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ public void CopyTo(Typeface[] array, int arrayIndex)
7979
// The extra "arrayIndex >= array.Length" check in because even if _collection.Count
8080
// is 0 the index is not allowed to be equal or greater than the length
8181
// (from the MSDN ICollection docs)
82-
if (arrayIndex < 0 || arrayIndex >= array.Length || (arrayIndex + Count) > array.Length)
83-
{
84-
throw new ArgumentOutOfRangeException("arrayIndex");
85-
}
82+
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
83+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(arrayIndex, array.Length);
84+
ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);
8685

8786
foreach (Typeface t in this)
8887
{

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/PseudoWebRequest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,11 @@ public override int Timeout
278278
}
279279
set
280280
{
281-
// negative time that is not -1 (infinite) is an error case
282-
if (value < 0 && value != System.Threading.Timeout.Infinite)
283-
throw new ArgumentOutOfRangeException("value");
281+
if (value != System.Threading.Timeout.Infinite)
282+
{
283+
// negative time that is not -1 (infinite) is an error case
284+
ArgumentOutOfRangeException.ThrowIfNegative(value);
285+
}
284286

285287
_timeout = value;
286288
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/HuffModule.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,15 @@ internal HuffModule()
3535
/// </summary>
3636
internal HuffCodec GetDefCodec(uint index)
3737
{
38-
HuffCodec huffCodec = null;
39-
if (AlgoModule.DefaultBAACount > index)
40-
{
41-
huffCodec = _defaultHuffCodecs[index];
42-
if (huffCodec == null)
43-
{
44-
huffCodec = new HuffCodec(index);
45-
_defaultHuffCodecs[index] = huffCodec;
46-
}
47-
}
48-
else
38+
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, AlgoModule.DefaultBAACount);
39+
40+
HuffCodec huffCodec = _defaultHuffCodecs[index];
41+
if (huffCodec == null)
4942
{
50-
throw new ArgumentOutOfRangeException("index");
43+
huffCodec = new HuffCodec(index);
44+
_defaultHuffCodecs[index] = huffCodec;
5145
}
46+
5247
return huffCodec;
5348
}
5449

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/IO/Packaging/PackWebRequest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,11 @@ public override int Timeout
399399
}
400400
set
401401
{
402-
// negative time that is not -1 (infinite) is an error case
403-
if (value < 0 && value != System.Threading.Timeout.Infinite)
404-
throw new ArgumentOutOfRangeException("value");
402+
if (value != System.Threading.Timeout.Infinite)
403+
{
404+
// negative time that is not -1 (infinite) is an error case
405+
ArgumentOutOfRangeException.ThrowIfNegative(value);
406+
}
405407

406408
GetRequest().Timeout = value;
407409
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FreezableCollection.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,8 @@ public void CopyTo(T[] array, int index)
391391
// This will not throw in the case that we are copying
392392
// from an empty collection. This is consistent with the
393393
// BCL Collection implementations. (Windows 1587365)
394-
if (index < 0 || (index + _collection.Count) > array.Length)
395-
{
396-
throw new ArgumentOutOfRangeException("index");
397-
}
394+
ArgumentOutOfRangeException.ThrowIfNegative(index);
395+
ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - _collection.Count);
398396

399397
_collection.CopyTo(array, index);
400398
}
@@ -503,10 +501,8 @@ void ICollection.CopyTo(Array array, int index)
503501
// This will not throw in the case that we are copying
504502
// from an empty collection. This is consistent with the
505503
// BCL Collection implementations. (Windows 1587365)
506-
if (index < 0 || (index + _collection.Count) > array.Length)
507-
{
508-
throw new ArgumentOutOfRangeException("index");
509-
}
504+
ArgumentOutOfRangeException.ThrowIfNegative(index);
505+
ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - _collection.Count);
510506

511507
if (array.Rank != 1)
512508
{

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,8 @@ public void CopyTo(TextDecoration[] array, int index)
292292
// This will not throw in the case that we are copying
293293
// from an empty collection. This is consistent with the
294294
// BCL Collection implementations. (Windows 1587365)
295-
if (index < 0 || (index + _collection.Count) > array.Length)
296-
{
297-
throw new ArgumentOutOfRangeException("index");
298-
}
295+
ArgumentOutOfRangeException.ThrowIfNegative(index);
296+
ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - _collection.Count);
299297

300298
_collection.CopyTo(array, index);
301299
}
@@ -404,10 +402,8 @@ void ICollection.CopyTo(Array array, int index)
404402
// This will not throw in the case that we are copying
405403
// from an empty collection. This is consistent with the
406404
// BCL Collection implementations. (Windows 1587365)
407-
if (index < 0 || (index + _collection.Count) > array.Length)
408-
{
409-
throw new ArgumentOutOfRangeException("index");
410-
}
405+
ArgumentOutOfRangeException.ThrowIfNegative(index);
406+
ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - _collection.Count);
411407

412408
if (array.Rank != 1)
413409
{

0 commit comments

Comments
 (0)