From cf4ccd47466cc067d660899f3273574b8666d724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Fri, 1 Apr 2022 17:24:31 +0200 Subject: [PATCH 01/12] Removed (uint) cast workaround in CoreLib, annotated missing places with TODOs --- .../src/System/Array.CoreCLR.cs | 4 +- .../System/Collections/Generic/BitHelper.cs | 17 ++- .../src/System/Text/ValueStringBuilder.cs | 11 +- .../src/System/Array.Enumerators.cs | 4 +- .../src/System/Array.cs | 6 +- .../src/System/BitConverter.cs | 6 +- .../src/System/Boolean.cs | 4 +- .../src/System/Buffers/StandardFormat.cs | 3 +- .../Utf8Formatter/Utf8Formatter.Boolean.cs | 4 +- .../Utf8Formatter/Utf8Formatter.Date.L.cs | 4 +- .../Utf8Formatter/Utf8Formatter.Date.R.cs | 4 +- .../Utf8Formatter.Integer.Unsigned.X.cs | 2 + .../TlsOverPerCoreLockedStacksArrayPool.cs | 10 +- .../System.Private.CoreLib/src/System/Char.cs | 35 +++-- .../Collections/Generic/ArraySortHelper.cs | 1 + .../System/Collections/Generic/Dictionary.cs | 42 +++--- .../src/System/Collections/Generic/List.cs | 2 +- .../Collections/Generic/ValueListBuilder.cs | 5 +- .../src/System/Decimal.cs | 4 +- .../System/Globalization/CompareInfo.Icu.cs | 2 +- .../src/System/Globalization/DateTimeParse.cs | 14 +- .../src/System/Globalization/StringInfo.cs | 4 +- .../src/System/Globalization/TimeSpanParse.cs | 27 ++-- .../System.Private.CoreLib/src/System/Guid.cs | 17 +-- .../src/System/IO/Stream.cs | 2 +- .../src/System/IO/TextReader.cs | 4 +- .../src/System/MemoryExtensions.cs | 2 +- .../src/System/Number.Parsing.cs | 121 +++++++++--------- .../DefaultInterpolatedStringHandler.cs | 9 +- .../src/System/String.Manipulation.cs | 3 +- .../src/System/Text/DecoderNLS.cs | 4 +- .../src/System/Text/Rune.cs | 28 ++-- .../src/System/Text/StringBuilder.cs | 4 +- 33 files changed, 213 insertions(+), 196 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs index 5538d58e8d969..3e55907c3c813 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs @@ -392,7 +392,7 @@ internal T get_Item(int index) // ! Warning: "this" is an array, not an SZArrayHelper. See comments above // ! or you may introduce a security hole! T[] _this = Unsafe.As(this); - if ((uint)index >= (uint)_this.Length) + if ((uint)index >= _this.Length) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } @@ -405,7 +405,7 @@ internal void set_Item(int index, T value) // ! Warning: "this" is an array, not an SZArrayHelper. See comments above // ! or you may introduce a security hole! T[] _this = Unsafe.As(this); - if ((uint)index >= (uint)_this.Length) + if ((uint)index >= _this.Length) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } diff --git a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs index 01f0c1cd6862f..5c66096f05b61 100644 --- a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs +++ b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Runtime.CompilerServices; + namespace System.Collections.Generic { internal ref struct BitHelper @@ -19,19 +21,22 @@ internal BitHelper(Span span, bool clear) internal void MarkBit(int bitPosition) { - int bitArrayIndex = bitPosition / IntSize; - if ((uint)bitArrayIndex < (uint)_span.Length) + (int bitArrayIndex, int position) = Math.DivRem(bitPosition, IntSize); + Span span = _span; + if ((uint)bitArrayIndex < (uint)span.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { - _span[bitArrayIndex] |= (1 << (bitPosition % IntSize)); + span[bitArrayIndex] |= 1 << position; } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal bool IsMarked(int bitPosition) { - int bitArrayIndex = bitPosition / IntSize; + (int bitArrayIndex, int position) = Math.DivRem(bitPosition, IntSize); + Span span = _span; return - (uint)bitArrayIndex < (uint)_span.Length && - (_span[bitArrayIndex] & (1 << (bitPosition % IntSize))) != 0; + (uint)bitArrayIndex < (uint)span.Length && // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + (span[bitArrayIndex] & (1 << position)) != 0; } /// How many ints must be allocated to represent n bits. Returns (n+31)/32, but avoids overflow. diff --git a/src/libraries/Common/src/System/Text/ValueStringBuilder.cs b/src/libraries/Common/src/System/Text/ValueStringBuilder.cs index 9cd033f3986ee..bbcee17ae9068 100644 --- a/src/libraries/Common/src/System/Text/ValueStringBuilder.cs +++ b/src/libraries/Common/src/System/Text/ValueStringBuilder.cs @@ -170,9 +170,10 @@ public void Insert(int index, string? s) public void Append(char c) { int pos = _pos; - if ((uint)pos < (uint)_chars.Length) + Span chars = _chars; + if ((uint)pos < (uint)chars.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { - _chars[pos] = c; + chars[pos] = c; _pos = pos + 1; } else @@ -190,9 +191,11 @@ public void Append(string? s) } int pos = _pos; - if (s.Length == 1 && (uint)pos < (uint)_chars.Length) // very common case, e.g. appending strings from NumberFormatInfo like separators, percent symbols, etc. + Span chars = _chars; + // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if (s.Length == 1 && (uint)pos < (uint)chars.Length) // very common case, e.g. appending strings from NumberFormatInfo like separators, percent symbols, etc. { - _chars[pos] = s[0]; + chars[pos] = s[0]; _pos = pos + 1; } else diff --git a/src/libraries/System.Private.CoreLib/src/System/Array.Enumerators.cs b/src/libraries/System.Private.CoreLib/src/System/Array.Enumerators.cs index 04c972c0a3c45..d20096eeaafe4 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Array.Enumerators.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Array.Enumerators.cs @@ -86,7 +86,7 @@ internal SZGenericArrayEnumerator(T[] array) public bool MoveNext() { int index = _index + 1; - if ((uint)index >= (uint)_array.Length) + if ((uint)index >= _array.Length) { _index = _array.Length; return false; @@ -102,7 +102,7 @@ public T Current int index = _index; T[] array = _array; - if ((uint)index >= (uint)array.Length) + if ((uint)index >= array.Length) { ThrowHelper.ThrowInvalidOperationException_EnumCurrent(index); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Array.cs b/src/libraries/System.Private.CoreLib/src/System/Array.cs index 663d5f9ba4d7f..35046bb4068ef 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Array.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Array.cs @@ -917,7 +917,7 @@ public static void Fill(T[] array, T value, int startIndex, int count) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } - if ((uint)startIndex > (uint)array.Length) + if ((uint)startIndex > array.Length) { ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index(); } @@ -1314,7 +1314,7 @@ public static int IndexOf(T[] array, T value, int startIndex, int count) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } - if ((uint)startIndex > (uint)array.Length) + if ((uint)startIndex > array.Length) { ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index(); } @@ -1570,7 +1570,7 @@ public static int LastIndexOf(T[] array, T value, int startIndex, int count) } // Make sure we're not out of range - if ((uint)startIndex >= (uint)array.Length) + if ((uint)startIndex >= array.Length) { ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index(); } diff --git a/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs b/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs index daedb49aaf659..712ed9fdb88e1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs +++ b/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs @@ -365,7 +365,7 @@ public static short ToInt16(byte[] value, int startIndex) { if (value == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); - if (unchecked((uint)startIndex) >= unchecked((uint)value.Length)) + if (unchecked((uint)startIndex) >= unchecked(value.Length)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); if (startIndex > value.Length - sizeof(short)) ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall, ExceptionArgument.value); @@ -403,7 +403,7 @@ public static int ToInt32(byte[] value, int startIndex) { if (value == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); - if (unchecked((uint)startIndex) >= unchecked((uint)value.Length)) + if (unchecked((uint)startIndex) >= unchecked(value.Length)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); if (startIndex > value.Length - sizeof(int)) ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall, ExceptionArgument.value); @@ -441,7 +441,7 @@ public static long ToInt64(byte[] value, int startIndex) { if (value == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); - if (unchecked((uint)startIndex) >= unchecked((uint)value.Length)) + if (unchecked((uint)startIndex) >= unchecked(value.Length)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); if (startIndex > value.Length - sizeof(long)) ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall, ExceptionArgument.value); diff --git a/src/libraries/System.Private.CoreLib/src/System/Boolean.cs b/src/libraries/System.Private.CoreLib/src/System/Boolean.cs index 5cc13227f6e30..b7b01190d8b9c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Boolean.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Boolean.cs @@ -99,7 +99,7 @@ public bool TryFormat(Span destination, out int charsWritten) { if (m_value) { - if ((uint)destination.Length > 3) // uint cast, per https://github.com/dotnet/runtime/issues/10596 + if (destination.Length > 3) { ulong true_val = BitConverter.IsLittleEndian ? 0x65007500720054ul : 0x54007200750065ul; // "True" MemoryMarshal.Write(MemoryMarshal.AsBytes(destination), ref true_val); @@ -109,7 +109,7 @@ public bool TryFormat(Span destination, out int charsWritten) } else { - if ((uint)destination.Length > 4) + if (destination.Length > 4) { ulong fals_val = BitConverter.IsLittleEndian ? 0x73006C00610046ul : 0x460061006C0073ul; // "Fals" MemoryMarshal.Write(MemoryMarshal.AsBytes(destination), ref fals_val); diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/StandardFormat.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/StandardFormat.cs index 0f2bf8a75004b..9989e34806d31 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/StandardFormat.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/StandardFormat.cs @@ -164,8 +164,7 @@ internal int Format(Span destination) int count = 0; char symbol = Symbol; - if (symbol != default && - (uint)destination.Length == FormatStringLength) // to eliminate bounds checks + if (symbol != default && destination.Length == FormatStringLength) { destination[0] = symbol; count = 1; diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Boolean.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Boolean.cs index b23cc3a28c879..220a49308ff4e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Boolean.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Boolean.cs @@ -65,7 +65,7 @@ public static bool TryFormat(bool value, Span destination, out int bytesWr { // This check can't be performed earlier because we need to throw if an invalid symbol is // provided, even if the buffer is too small. - if ((uint)4 >= (uint)destination.Length) + if (destination.Length <= 4) { goto BufferTooSmall; } @@ -75,7 +75,7 @@ public static bool TryFormat(bool value, Span destination, out int bytesWr } else if (symbol == 'l') { - if ((uint)4 >= (uint)destination.Length) + if (destination.Length <= 4) { goto BufferTooSmall; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.L.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.L.cs index 0f20a2ab55e7b..6d4dc6143dc5c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.L.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.L.cs @@ -13,9 +13,7 @@ public static partial class Utf8Formatter // private static bool TryFormatDateTimeL(DateTime value, Span destination, out int bytesWritten) { - // Writing the check in this fashion elides all bounds checks on 'buffer' - // for the remainder of the method. - if ((uint)28 >= (uint)destination.Length) + if (destination.Length <= 28) { bytesWritten = 0; return false; diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.R.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.R.cs index 9c9738dbb135b..d80b27cd26c8b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.R.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.R.cs @@ -13,9 +13,7 @@ public static partial class Utf8Formatter // private static bool TryFormatDateTimeR(DateTime value, Span destination, out int bytesWritten) { - // Writing the check in this fashion elides all bounds checks on 'buffer' - // for the remainder of the method. - if ((uint)28 >= (uint)destination.Length) + if (destination.Length <= 28) { bytesWritten = 0; return false; diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.X.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.X.cs index 00ea1d2e29b5d..cfe2a2dc58ef1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.X.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.X.cs @@ -35,6 +35,7 @@ private static bool TryFormatUInt64X(ulong value, byte precision, bool useLower, if (useLower) { + // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 while ((uint)(--computedOutputLength) < (uint)destination.Length) { destination[computedOutputLength] = (byte)HexConverter.ToCharLower((int)value); @@ -43,6 +44,7 @@ private static bool TryFormatUInt64X(ulong value, byte precision, bool useLower, } else { + // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 while ((uint)(--computedOutputLength) < (uint)destination.Length) { destination[computedOutputLength] = (byte)HexConverter.ToCharUpper((int)value); diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs index 9206dde5e5fa6..fdce9a0998a6b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs @@ -61,7 +61,7 @@ public override T[] Rent(int minimumLength) // First, try to get an array from TLS if possible. ThreadLocalArray[]? tlsBuckets = t_tlsBuckets; - if (tlsBuckets is not null && (uint)bucketIndex < (uint)tlsBuckets.Length) + if (tlsBuckets is not null && (uint)bucketIndex < tlsBuckets.Length) { buffer = tlsBuckets[bucketIndex].Array; if (buffer is not null) @@ -77,7 +77,7 @@ public override T[] Rent(int minimumLength) // Next, try to get an array from one of the per-core stacks. PerCoreLockedStacks?[] perCoreBuckets = _buckets; - if ((uint)bucketIndex < (uint)perCoreBuckets.Length) + if ((uint)bucketIndex < perCoreBuckets.Length) { PerCoreLockedStacks? b = perCoreBuckets[bucketIndex]; if (b is not null) @@ -140,7 +140,7 @@ public override void Return(T[] array, bool clearArray = false) bool haveBucket = false; bool returned = true; - if ((uint)bucketIndex < (uint)tlsBuckets.Length) + if ((uint)bucketIndex < tlsBuckets.Length) { haveBucket = true; @@ -374,7 +374,7 @@ public bool TryPush(T[] array) Monitor.Enter(this); T[]?[] arrays = _arrays; int count = _count; - if ((uint)count < (uint)arrays.Length) + if ((uint)count < arrays.Length) { if (count == 0) { @@ -398,7 +398,7 @@ public bool TryPush(T[] array) Monitor.Enter(this); T[]?[] arrays = _arrays; int count = _count - 1; - if ((uint)count < (uint)arrays.Length) + if ((uint)count < arrays.Length) { arr = arrays[count]; arrays[count] = null; diff --git a/src/libraries/System.Private.CoreLib/src/System/Char.cs b/src/libraries/System.Private.CoreLib/src/System/Char.cs index 117362d37759e..4d81144066dc8 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Char.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Char.cs @@ -508,7 +508,7 @@ public static bool IsControl(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -523,7 +523,7 @@ public static bool IsDigit(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -543,7 +543,7 @@ public static bool IsLetter(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -564,7 +564,7 @@ public static bool IsLetterOrDigit(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -584,8 +584,7 @@ public static bool IsLower(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -627,7 +626,7 @@ public static bool IsNumber(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -660,7 +659,7 @@ public static bool IsPunctuation(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -705,7 +704,7 @@ public static bool IsSeparator(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -730,7 +729,7 @@ public static bool IsSurrogate(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -762,7 +761,7 @@ public static bool IsSymbol(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -782,7 +781,7 @@ public static bool IsUpper(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -802,7 +801,7 @@ public static bool IsWhiteSpace(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -828,7 +827,7 @@ public static UnicodeCategory GetUnicodeCategory(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -852,7 +851,7 @@ public static double GetNumericValue(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -874,7 +873,7 @@ public static bool IsHighSurrogate(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -896,7 +895,7 @@ public static bool IsLowSurrogate(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -913,7 +912,7 @@ public static bool IsSurrogatePair(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if (((uint)index) >= ((uint)s.Length)) + if ((uint)index >= s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs index 201f52af7f853..55d670fb45342 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs @@ -1119,6 +1119,7 @@ public static int MoveNansToFront(Span keys, Span va keys[left] = keys[i]; keys[i] = temp; + // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 if ((uint)i < (uint)values.Length) // check to see if we have values { TValue tempValue = values[left]; diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs index 9107c4f375d35..433cfd692539d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs @@ -305,7 +305,7 @@ private void CopyTo(KeyValuePair[] array, int index) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } - if ((uint)index > (uint)array.Length) + if ((uint)index > array.Length) { ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); } @@ -377,7 +377,7 @@ internal ref TValue FindValue(TKey key) { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test in if to drop range check for following array access - if ((uint)i >= (uint)entries.Length) + if ((uint)i >= entries.Length) { goto ReturnNotFound; } @@ -391,7 +391,7 @@ internal ref TValue FindValue(TKey key) i = entry.next; collisionCount++; - } while (collisionCount <= (uint)entries.Length); + } while (collisionCount <= entries.Length); // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -409,7 +409,7 @@ internal ref TValue FindValue(TKey key) { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test in if to drop range check for following array access - if ((uint)i >= (uint)entries.Length) + if ((uint)i >= entries.Length) { goto ReturnNotFound; } @@ -423,7 +423,7 @@ internal ref TValue FindValue(TKey key) i = entry.next; collisionCount++; - } while (collisionCount <= (uint)entries.Length); + } while (collisionCount <= entries.Length); // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -441,7 +441,7 @@ internal ref TValue FindValue(TKey key) { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test in if to drop range check for following array access - if ((uint)i >= (uint)entries.Length) + if ((uint)i >= entries.Length) { goto ReturnNotFound; } @@ -455,7 +455,7 @@ internal ref TValue FindValue(TKey key) i = entry.next; collisionCount++; - } while (collisionCount <= (uint)entries.Length); + } while (collisionCount <= entries.Length); // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -528,7 +528,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior) { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test uint in if rather than loop condition to drop range check for following array access - if ((uint)i >= (uint)entries.Length) + if ((uint)i >= entries.Length) { break; } @@ -552,7 +552,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior) i = entries[i].next; collisionCount++; - if (collisionCount > (uint)entries.Length) + if (collisionCount > entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -570,7 +570,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior) { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test uint in if rather than loop condition to drop range check for following array access - if ((uint)i >= (uint)entries.Length) + if ((uint)i >= entries.Length) { break; } @@ -594,7 +594,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior) i = entries[i].next; collisionCount++; - if (collisionCount > (uint)entries.Length) + if (collisionCount > entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -609,7 +609,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior) { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test uint in if rather than loop condition to drop range check for following array access - if ((uint)i >= (uint)entries.Length) + if ((uint)i >= entries.Length) { break; } @@ -633,7 +633,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior) i = entries[i].next; collisionCount++; - if (collisionCount > (uint)entries.Length) + if (collisionCount > entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -725,7 +725,7 @@ internal static class CollectionsMarshalHelper { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test uint in if rather than loop condition to drop range check for following array access - if ((uint)i >= (uint)entries.Length) + if ((uint)i >= entries.Length) { break; } @@ -740,7 +740,7 @@ internal static class CollectionsMarshalHelper i = entries[i].next; collisionCount++; - if (collisionCount > (uint)entries.Length) + if (collisionCount > entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -758,7 +758,7 @@ internal static class CollectionsMarshalHelper { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test uint in if rather than loop condition to drop range check for following array access - if ((uint)i >= (uint)entries.Length) + if ((uint)i >= entries.Length) { break; } @@ -773,7 +773,7 @@ internal static class CollectionsMarshalHelper i = entries[i].next; collisionCount++; - if (collisionCount > (uint)entries.Length) + if (collisionCount > entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -788,7 +788,7 @@ internal static class CollectionsMarshalHelper { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test uint in if rather than loop condition to drop range check for following array access - if ((uint)i >= (uint)entries.Length) + if ((uint)i >= entries.Length) { break; } @@ -803,7 +803,7 @@ internal static class CollectionsMarshalHelper i = entries[i].next; collisionCount++; - if (collisionCount > (uint)entries.Length) + if (collisionCount > entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -1020,7 +1020,7 @@ public bool Remove(TKey key) i = entry.next; collisionCount++; - if (collisionCount > (uint)entries.Length) + if (collisionCount > entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -1090,7 +1090,7 @@ public bool Remove(TKey key, [MaybeNullWhen(false)] out TValue value) i = entry.next; collisionCount++; - if (collisionCount > (uint)entries.Length) + if (collisionCount > entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs index cc79cb9ab8e56..c31df96b1be0d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs @@ -198,7 +198,7 @@ public void Add(T item) _version++; T[] array = _items; int size = _size; - if ((uint)size < (uint)array.Length) + if ((uint)size < array.Length) { _size = size + 1; array[size] = item; diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs index 72b25bc2d9dd3..c191ff69c6141 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs @@ -44,9 +44,10 @@ public ref T this[int index] public void Append(T item) { int pos = _pos; - if ((uint)pos < (uint)_span.Length) + Span span = _span; // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)pos < (uint)span.Length) { - _span[pos] = item; + span[pos] = item; _pos = pos + 1; } else diff --git a/src/libraries/System.Private.CoreLib/src/System/Decimal.cs b/src/libraries/System.Private.CoreLib/src/System/Decimal.cs index f7e6f9d8b510e..c5c30926a9f0f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Decimal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Decimal.cs @@ -561,7 +561,7 @@ public static int[] GetBits(decimal d) /// The destination span was not long enough to store the binary representation. public static int GetBits(decimal d, Span destination) { - if ((uint)destination.Length <= 3) + if (destination.Length <= 3) { ThrowHelper.ThrowArgumentException_DestinationTooShort(); } @@ -582,7 +582,7 @@ public static int GetBits(decimal d, Span destination) /// true if the decimal's binary representation was written to the destination; false if the destination wasn't long enough. public static bool TryGetBits(decimal d, Span destination, out int valuesWritten) { - if ((uint)destination.Length <= 3) + if (destination.Length <= 3) { valuesWritten = 0; return false; diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs index f4d6ebf23d1d1..ca5bcfaa02484 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs @@ -659,7 +659,7 @@ private unsafe int IcuGetSortKey(ReadOnlySpan source, Span destinati // The check below also handles errors due to negative values / overflow being returned. - if ((uint)actualSortKeyLength > (uint)destination.Length) + if ((uint)actualSortKeyLength > (uint)destination.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { if (actualSortKeyLength > destination.Length) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs index a74b448775c62..a1aa99a666047 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs @@ -4392,7 +4392,7 @@ internal static bool TryParseQuoteString(ReadOnlySpan format, int pos, ref char quoteChar = format[pos++]; // Get the character used to quote the following string. bool foundQuote = false; - while ((uint)pos < (uint)format.Length) + while ((uint)pos < (uint)format.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { char ch = format[pos++]; if (ch == quoteChar) @@ -4407,7 +4407,7 @@ internal static bool TryParseQuoteString(ReadOnlySpan format, int pos, ref // Therefore, someone can use a format like "'minute:' mm\"" to display: // minute: 45" // because the second double quote is escaped. - if ((uint)pos < (uint)format.Length) + if ((uint)pos < (uint)format.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { result.Append(format[pos++]); } @@ -4671,7 +4671,7 @@ private static bool ParseFormatR(ReadOnlySpan source, ref ParsingInfo pars // Tue, 03 Jan 2017 08:08:05 GMT // The format is exactly 29 characters. - if ((uint)source.Length != 29) + if (source.Length != 29) { result.SetBadDateTimeFailure(); return false; @@ -4868,7 +4868,7 @@ private static bool ParseFormatO(ReadOnlySpan source, ref DateTimeResult r // 2017-06-12T05:30:45.7680000-7:00 (special-case of one-digit offset hour) // 2017-06-12T05:30:45.7680000-07:00 - if ((uint)source.Length < 27 || + if (source.Length < 27 || source[4] != '-' || source[7] != '-' || source[10] != 'T' || @@ -4989,7 +4989,7 @@ private static bool ParseFormatO(ReadOnlySpan source, ref DateTimeResult r return false; } - if ((uint)source.Length > 27) + if (source.Length > 27) { char offsetChar = source[27]; switch (offsetChar) @@ -5007,7 +5007,7 @@ private static bool ParseFormatO(ReadOnlySpan source, ref DateTimeResult r case '-': int offsetHours, colonIndex; - if ((uint)source.Length == 33) + if (source.Length == 33) { uint oh1 = (uint)(source[28] - '0'), oh2 = (uint)(source[29] - '0'); @@ -5020,7 +5020,7 @@ private static bool ParseFormatO(ReadOnlySpan source, ref DateTimeResult r offsetHours = (int)(oh1 * 10 + oh2); colonIndex = 30; } - else if ((uint)source.Length == 32) // special-case allowed for compat: only one offset hour digit + else if (source.Length == 32) // special-case allowed for compat: only one offset hour digit { offsetHours = source[28] - '0'; diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/StringInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/StringInfo.cs index 365bcb3f25d19..145df6078686d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/StringInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/StringInfo.cs @@ -75,7 +75,7 @@ public string SubstringByTextElements(int startingTextElement, int lengthInTextE { int[] indexes = Indexes ?? Array.Empty(); - if ((uint)startingTextElement >= (uint)indexes.Length) + if ((uint)startingTextElement >= indexes.Length) { throw new ArgumentOutOfRangeException(nameof(startingTextElement), startingTextElement, SR.Arg_ArgumentOutOfRangeException); } @@ -87,7 +87,7 @@ public string SubstringByTextElements(int startingTextElement, int lengthInTextE int start = indexes[startingTextElement]; Index end = ^0; // assume reading to end of the string unless the caller told us to stop early - if ((uint)(startingTextElement + lengthInTextElements) < (uint)indexes.Length) + if ((uint)(startingTextElement + lengthInTextElements) < indexes.Length) { end = indexes[startingTextElement + lengthInTextElements]; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanParse.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanParse.cs index ec5e39f76938d..8cf2b63ef0bb7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanParse.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanParse.cs @@ -170,14 +170,15 @@ internal TimeSpanToken GetNextToken() // Get the position of the next character to be processed. If there is no // next character, we're at the end. int pos = _pos; + ReadOnlySpan value = _value; Debug.Assert(pos > -1); - if (pos >= _value.Length) + if ((uint)pos >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { return new TimeSpanToken(TTT.End); } // Now retrieve that character. If it's a digit, we're processing a number. - int num = _value[pos] - '0'; + int num = value[pos] - '0'; if ((uint)num <= 9) { int zeroes = 0; @@ -188,8 +189,10 @@ internal TimeSpanToken GetNextToken() while (true) { int digit; - if (++_pos >= _value.Length || (uint)(digit = _value[_pos] - '0') > 9) + // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)(++pos) >= (uint)value.Length || (uint)(digit = value[pos] - '0') > 9) { + _pos = pos; return new TimeSpanToken(TTT.Num, 0, zeroes, default); } @@ -200,26 +203,30 @@ internal TimeSpanToken GetNextToken() } num = digit; + _pos = pos; break; } } // Continue to read as long as we're reading digits. - while (++_pos < _value.Length) + while ((uint)(++pos) < (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { - int digit = _value[_pos] - '0'; + int digit = value[pos] - '0'; if ((uint)digit > 9) { + _pos = pos; break; } num = num * 10 + digit; if ((num & 0xF0000000) != 0) // Max limit we can support 268435455 which is FFFFFFF { + _pos = pos; return new TimeSpanToken(TTT.NumOverflow); } } + _pos = pos; return new TimeSpanToken(TTT.Num, num, zeroes, default); } @@ -228,12 +235,14 @@ internal TimeSpanToken GetNextToken() int length = 1; while (true) { - if (++_pos >= _value.Length || (uint)(_value[_pos] - '0') <= 9) + // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)(++pos) >= (uint)value.Length || (uint)(value[pos] - '0') <= 9) { break; } length++; } + _pos = pos; // Return the separator. return new TimeSpanToken(TTT.Sep, 0, 0, _value.Slice(pos, length)); @@ -249,7 +258,7 @@ internal void BackOne() internal char NextChar() { int pos = ++_pos; - return (uint)pos < (uint)_value.Length? + return (uint)pos < (uint)_value.Length ? // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 _value[pos] : (char)0; } @@ -1258,7 +1267,7 @@ private static bool TryParseByFormat(ReadOnlySpan input, ReadOnlySpan '9') + if ((uint)(ch - '0') > '9' - '0') { tokenizer.BackOne(); break; diff --git a/src/libraries/System.Private.CoreLib/src/System/Guid.cs b/src/libraries/System.Private.CoreLib/src/System/Guid.cs index 150e0f94da9f6..de9b51beb6fad 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Guid.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Guid.cs @@ -48,7 +48,7 @@ public Guid(byte[] b!!) : // Creates a new guid from a read-only span. public Guid(ReadOnlySpan b) { - if ((uint)b.Length != 16) + if (b.Length != 16) { throw new ArgumentException(SR.Format(SR.Arg_GuidArrayCtor, "16"), nameof(b)); } @@ -366,7 +366,7 @@ private static bool TryParseExactB(ReadOnlySpan guidString, ref GuidResult { // e.g. "{d85b1407-351d-4694-9392-03acc5870eb1}" - if ((uint)guidString.Length != 38 || guidString[0] != '{' || guidString[37] != '}') + if (guidString.Length != 38 || guidString[0] != '{' || guidString[37] != '}') { result.SetFailure(overflow: false, nameof(SR.Format_GuidInvLen)); return false; @@ -379,7 +379,7 @@ private static bool TryParseExactD(ReadOnlySpan guidString, ref GuidResult { // e.g. "d85b1407-351d-4694-9392-03acc5870eb1" - if ((uint)guidString.Length != 36 || guidString[8] != '-' || guidString[13] != '-' || guidString[18] != '-' || guidString[23] != '-') + if (guidString.Length != 36 || guidString[8] != '-' || guidString[13] != '-' || guidString[18] != '-' || guidString[23] != '-') { result.SetFailure(overflow: false, guidString.Length != 36 ? nameof(SR.Format_GuidInvLen) : nameof(SR.Format_GuidDashes)); return false; @@ -465,7 +465,7 @@ private static bool TryParseExactN(ReadOnlySpan guidString, ref GuidResult { // e.g. "d85b1407351d4694939203acc5870eb1" - if ((uint)guidString.Length != 32) + if (guidString.Length != 32) { result.SetFailure(overflow: false, nameof(SR.Format_GuidInvLen)); return false; @@ -508,7 +508,7 @@ private static bool TryParseExactP(ReadOnlySpan guidString, ref GuidResult { // e.g. "(d85b1407-351d-4694-9392-03acc5870eb1)" - if ((uint)guidString.Length != 38 || guidString[0] != '(' || guidString[37] != ')') + if (guidString.Length != 38 || guidString[0] != '(' || guidString[37] != ')') { result.SetFailure(overflow: false, nameof(SR.Format_GuidInvLen)); return false; @@ -535,7 +535,7 @@ private static bool TryParseExactX(ReadOnlySpan guidString, ref GuidResult guidString = EatAllWhitespace(guidString); // Check for leading '{' - if ((uint)guidString.Length == 0 || guidString[0] != '{') + if (guidString.Length == 0 || guidString[0] != '{') { result.SetFailure(overflow: false, nameof(SR.Format_GuidBrace)); return false; @@ -609,6 +609,7 @@ private static bool TryParseExactX(ReadOnlySpan guidString, ref GuidResult } // Check for '{' + // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 if ((uint)guidString.Length <= (uint)(numStart + numLen + 1) || guidString[numStart + numLen + 1] != '{') { result.SetFailure(overflow: false, nameof(SR.Format_GuidBrace)); @@ -723,14 +724,14 @@ private static bool TryParseHex(ReadOnlySpan guidString, out uint result) private static bool TryParseHex(ReadOnlySpan guidString, out uint result, ref bool overflow) { - if ((uint)guidString.Length > 0) + if (guidString.Length > 0) { if (guidString[0] == '+') { guidString = guidString.Slice(1); } - if ((uint)guidString.Length > 1 && guidString[0] == '0' && (guidString[1] | 0x20) == 'x') + if (guidString.Length > 1 && guidString[0] == '0' && (guidString[1] | 0x20) == 'x') { guidString = guidString.Slice(2); } diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs index 53f5920b96703..67cd3e27e1f61 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs @@ -680,7 +680,7 @@ public virtual int Read(Span buffer) try { int numRead = Read(sharedBuffer, 0, buffer.Length); - if ((uint)numRead > (uint)buffer.Length) + if ((uint)numRead > (uint)buffer.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { throw new IOException(SR.IO_StreamTooLong); } diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/TextReader.cs b/src/libraries/System.Private.CoreLib/src/System/IO/TextReader.cs index 299103cf093eb..edc81e18e4645 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/TextReader.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/TextReader.cs @@ -104,7 +104,7 @@ public virtual int Read(Span buffer) try { int numRead = Read(array, 0, buffer.Length); - if ((uint)numRead > (uint)buffer.Length) + if ((uint)numRead > (uint)buffer.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { throw new IOException(SR.IO_InvalidReadLength); } @@ -154,7 +154,7 @@ public virtual int ReadBlock(Span buffer) try { int numRead = ReadBlock(array, 0, buffer.Length); - if ((uint)numRead > (uint)buffer.Length) + if ((uint)numRead > (uint)buffer.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { throw new IOException(SR.IO_InvalidReadLength); } diff --git a/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs b/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs index 6563f2ca697f3..f1bfa12f9a3ab 100644 --- a/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs +++ b/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs @@ -2110,7 +2110,7 @@ public bool AppendLiteral(string value) { Span destination = _destination; int pos = _pos; - if ((uint)pos < (uint)destination.Length) + if ((uint)pos < (uint)destination.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { destination[pos] = value[0]; _pos = pos + 1; diff --git a/src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs b/src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs index d9c0662e81d25..f87caaa224d33 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs @@ -554,7 +554,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -571,14 +571,14 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value { sign = -1; index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } else if (num == '+') { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -587,7 +587,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value { sign = -1; index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -599,7 +599,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value if (!string.IsNullOrEmpty(positiveSign) && value.StartsWith(positiveSign)) { index += positiveSign.Length; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -607,7 +607,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value { sign = -1; index += negativeSign.Length; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -625,7 +625,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; } while (num == '0'); @@ -638,7 +638,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value index++; for (int i = 0; i < 8; i++) // next 8 digits can't overflow { - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; if (!IsDigit(num)) @@ -647,7 +647,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value answer = 10 * answer + num - '0'; } - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; if (!IsDigit(num)) @@ -657,7 +657,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value overflow = answer > int.MaxValue / 10; answer = answer * 10 + num - '0'; overflow |= (uint)answer > int.MaxValue + (((uint)sign) >> 31); - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; // At this point, we're either overflowing or hitting a formatting error. @@ -667,7 +667,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value { overflow = true; index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto OverflowExit; num = value[index]; } @@ -706,7 +706,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value if (!IsWhite(value[index])) break; } - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; } @@ -733,7 +733,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -750,14 +750,14 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value { sign = -1; index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } else if (num == '+') { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -766,7 +766,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value { sign = -1; index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -778,7 +778,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value if (!string.IsNullOrEmpty(positiveSign) && value.StartsWith(positiveSign)) { index += positiveSign.Length; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -786,7 +786,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value { sign = -1; index += negativeSign.Length; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -804,7 +804,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; } while (num == '0'); @@ -817,7 +817,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value index++; for (int i = 0; i < 17; i++) // next 17 digits can't overflow { - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; if (!IsDigit(num)) @@ -826,7 +826,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value answer = 10 * answer + num - '0'; } - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; if (!IsDigit(num)) @@ -836,7 +836,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value overflow = answer > long.MaxValue / 10; answer = answer * 10 + num - '0'; overflow |= (ulong)answer > (ulong)long.MaxValue + (((uint)sign) >> 31); - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; // At this point, we're either overflowing or hitting a formatting error. @@ -846,7 +846,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value { overflow = true; index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto OverflowExit; num = value[index]; } @@ -885,7 +885,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value if (!IsWhite(value[index])) break; } - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; } @@ -985,7 +985,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1001,7 +1001,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu if (num == '+') { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1009,7 +1009,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu { overflow = true; index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1018,7 +1018,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu { overflow = true; index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1030,7 +1030,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu if (!string.IsNullOrEmpty(positiveSign) && value.StartsWith(positiveSign)) { index += positiveSign.Length; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1038,7 +1038,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu { overflow = true; index += negativeSign.Length; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1055,7 +1055,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; } while (num == '0'); @@ -1068,7 +1068,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu index++; for (int i = 0; i < 8; i++) // next 8 digits can't overflow { - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; num = value[index]; if (!IsDigit(num)) @@ -1077,7 +1077,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu answer = 10 * answer + num - '0'; } - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; num = value[index]; if (!IsDigit(num)) @@ -1086,7 +1086,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu // Potential overflow now processing the 10th digit. overflow |= (uint)answer > uint.MaxValue / 10 || ((uint)answer == uint.MaxValue / 10 && num > '5'); answer = answer * 10 + num - '0'; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; // At this point, we're either overflowing or hitting a formatting error. @@ -1096,7 +1096,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu { overflow = true; index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto OverflowExit; num = value[index]; } @@ -1137,7 +1137,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu if (!IsWhite(value[index])) break; } - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; } @@ -1164,7 +1164,7 @@ internal static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan va do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1182,7 +1182,7 @@ internal static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan va do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; } while (num == '0'); @@ -1195,7 +1195,7 @@ internal static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan va index++; for (int i = 0; i < 7; i++) // next 7 digits can't overflow { - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; @@ -1207,7 +1207,7 @@ internal static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan va } // If there's another digit, it's an overflow. - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; if (!HexConverter.IsHexChar(num)) @@ -1218,7 +1218,7 @@ internal static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan va do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto OverflowExit; num = value[index]; } while (HexConverter.IsHexChar(num)); @@ -1258,7 +1258,7 @@ internal static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan va if (!IsWhite(value[index])) break; } - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; } @@ -1321,7 +1321,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1337,7 +1337,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu if (num == '+') { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1345,7 +1345,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu { overflow = true; index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1354,7 +1354,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu { overflow = true; index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1366,7 +1366,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu if (!string.IsNullOrEmpty(positiveSign) && value.StartsWith(positiveSign)) { index += positiveSign.Length; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1374,7 +1374,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu { overflow = true; index += negativeSign.Length; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1391,7 +1391,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; } while (num == '0'); @@ -1404,7 +1404,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu index++; for (int i = 0; i < 18; i++) // next 18 digits can't overflow { - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; num = value[index]; if (!IsDigit(num)) @@ -1413,7 +1413,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu answer = 10 * answer + num - '0'; } - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; num = value[index]; if (!IsDigit(num)) @@ -1422,7 +1422,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu // Potential overflow now processing the 20th digit. overflow |= (ulong)answer > ulong.MaxValue / 10 || ((ulong)answer == ulong.MaxValue / 10 && num > '5'); answer = answer * 10 + num - '0'; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; // At this point, we're either overflowing or hitting a formatting error. @@ -1432,7 +1432,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu { overflow = true; index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto OverflowExit; num = value[index]; } @@ -1473,7 +1473,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu if (!IsWhite(value[index])) break; } - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; } @@ -1500,7 +1500,7 @@ private static ParsingStatus TryParseUInt64HexNumberStyle(ReadOnlySpan val do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto FalseExit; num = value[index]; } @@ -1518,7 +1518,7 @@ private static ParsingStatus TryParseUInt64HexNumberStyle(ReadOnlySpan val do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; } while (num == '0'); @@ -1531,7 +1531,7 @@ private static ParsingStatus TryParseUInt64HexNumberStyle(ReadOnlySpan val index++; for (int i = 0; i < 15; i++) // next 15 digits can't overflow { - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; @@ -1543,7 +1543,7 @@ private static ParsingStatus TryParseUInt64HexNumberStyle(ReadOnlySpan val } // If there's another digit, it's an overflow. - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEnd; num = value[index]; if (!HexConverter.IsHexChar(num)) @@ -1554,7 +1554,7 @@ private static ParsingStatus TryParseUInt64HexNumberStyle(ReadOnlySpan val do { index++; - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto OverflowExit; num = value[index]; } while (HexConverter.IsHexChar(num)); @@ -1594,7 +1594,7 @@ private static ParsingStatus TryParseUInt64HexNumberStyle(ReadOnlySpan val if (!IsWhite(value[index])) break; } - if ((uint)index >= (uint)value.Length) + if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 goto DoneAtEndButPotentialOverflow; } @@ -2014,6 +2014,7 @@ internal static unsafe bool TryStringToNumber(ReadOnlySpan value, NumberSt private static bool TrailingZeros(ReadOnlySpan value, int index) { // For compatibility, we need to allow trailing zeros at the end of a number string + // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 for (int i = index; (uint)i < (uint)value.Length; i++) { if (value[i] != '\0') diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/DefaultInterpolatedStringHandler.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/DefaultInterpolatedStringHandler.cs index 8e441b6885616..30b499e210d0d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/DefaultInterpolatedStringHandler.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/DefaultInterpolatedStringHandler.cs @@ -154,9 +154,9 @@ public void AppendLiteral(string value) if (value.Length == 1) { - Span chars = _chars; int pos = _pos; - if ((uint)pos < (uint)chars.Length) + Span chars = _chars; + if ((uint)pos < (uint)chars.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { chars[pos] = value[0]; _pos = pos + 1; @@ -170,12 +170,11 @@ public void AppendLiteral(string value) if (value.Length == 2) { - Span chars = _chars; int pos = _pos; - if ((uint)pos < chars.Length - 1) + if ((uint)pos < _chars.Length - 1) { Unsafe.WriteUnaligned( - ref Unsafe.As(ref Unsafe.Add(ref MemoryMarshal.GetReference(chars), pos)), + ref Unsafe.As(ref Unsafe.Add(ref MemoryMarshal.GetReference(_chars), pos)), Unsafe.ReadUnaligned(ref Unsafe.As(ref value.GetRawStringData()))); _pos = pos + 2; } diff --git a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs index d883782dbe5a3..a4de691e79b92 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs @@ -1235,7 +1235,7 @@ internal static int IndexOfNewlineChar(ReadOnlySpan text, out int stride) stride = default; int idx = text.IndexOfAny(needles); - if ((uint)idx < (uint)text.Length) + if ((uint)idx < (uint)text.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { stride = 1; // needle found @@ -1245,6 +1245,7 @@ internal static int IndexOfNewlineChar(ReadOnlySpan text, out int stride) if (text[idx] == '\r') { int nextCharIdx = idx + 1; + // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 if ((uint)nextCharIdx < (uint)text.Length && text[nextCharIdx] == '\n') { stride = 2; diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/DecoderNLS.cs b/src/libraries/System.Private.CoreLib/src/System/Text/DecoderNLS.cs index b93c99ebfe573..5eb5652b6a30a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/DecoderNLS.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/DecoderNLS.cs @@ -381,7 +381,7 @@ private static int ConcatInto(ReadOnlySpan srcLeft, ReadOnlySpan src for (int i = 0; i < srcLeft.Length; i++) { - if ((uint)total >= (uint)dest.Length) + if ((uint)total >= (uint)dest.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { goto Finish; } @@ -393,7 +393,7 @@ private static int ConcatInto(ReadOnlySpan srcLeft, ReadOnlySpan src for (int i = 0; i < srcRight.Length; i++) { - if ((uint)total >= (uint)dest.Length) + if ((uint)total >= (uint)dest.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { goto Finish; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/Rune.cs b/src/libraries/System.Private.CoreLib/src/System/Text/Rune.cs index b4e226b64d1fb..091c410bfe12c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/Rune.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/Rune.cs @@ -399,7 +399,7 @@ public static OperationStatus DecodeFromUtf8(ReadOnlySpan source, out Rune // Try reading input[0]. - if ((uint)index >= (uint)source.Length) + if ((uint)index >= (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { goto NeedsMoreData; } @@ -434,7 +434,7 @@ public static OperationStatus DecodeFromUtf8(ReadOnlySpan source, out Rune // Try reading input[1]. index++; - if ((uint)index >= (uint)source.Length) + if ((uint)index >= (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { goto NeedsMoreData; } @@ -488,7 +488,7 @@ public static OperationStatus DecodeFromUtf8(ReadOnlySpan source, out Rune // Try reading input[2]. index++; - if ((uint)index >= (uint)source.Length) + if ((uint)index >= (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { goto NeedsMoreData; } @@ -513,7 +513,7 @@ public static OperationStatus DecodeFromUtf8(ReadOnlySpan source, out Rune // Try reading input[3]. index++; - if ((uint)index >= (uint)source.Length) + if ((uint)index >= (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { goto NeedsMoreData; } @@ -563,7 +563,7 @@ public static OperationStatus DecodeFromUtf8(ReadOnlySpan source, out Rune public static OperationStatus DecodeLastFromUtf16(ReadOnlySpan source, out Rune result, out int charsConsumed) { int index = source.Length - 1; - if ((uint)index < (uint)source.Length) + if ((uint)index < (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { // First, check for the common case of a BMP scalar value. // If this is correct, return immediately. @@ -582,7 +582,7 @@ public static OperationStatus DecodeLastFromUtf16(ReadOnlySpan source, out // we have a standalone low surrogate, which is always invalid. index--; - if ((uint)index < (uint)source.Length) + if ((uint)index < (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { char penultimateChar = source[index]; if (TryCreate(penultimateChar, finalChar, out result)) @@ -623,7 +623,7 @@ public static OperationStatus DecodeLastFromUtf16(ReadOnlySpan source, out public static OperationStatus DecodeLastFromUtf8(ReadOnlySpan source, out Rune value, out int bytesConsumed) { int index = source.Length - 1; - if ((uint)index < (uint)source.Length) + if ((uint)index < (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { // The buffer contains at least one byte. Let's check the fast case where the // buffer ends with an ASCII byte. @@ -657,7 +657,7 @@ public static OperationStatus DecodeLastFromUtf8(ReadOnlySpan source, out for (int i = 3; i > 0; i--) { index--; - if ((uint)index >= (uint)source.Length) + if ((uint)index >= (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { goto Invalid; // out of data } @@ -847,7 +847,7 @@ private static int ReadRuneFromString(string input, int index) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input); } - if ((uint)index >= (uint)input!.Length) + if ((uint)index >= input.Length) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } @@ -870,7 +870,7 @@ private static int ReadRuneFromString(string input, int index) // the string terminates unexpectedly. index++; - if ((uint)index >= (uint)input.Length) + if ((uint)index >= input.Length) { return -1; // not an argument exception - just a "bad data" failure } @@ -1024,7 +1024,7 @@ private static bool TryEncodeToUtf16(Rune value, Span destination, out int charsWritten = 1; return true; } - else if (1 < (uint)destination.Length) + else if (destination.Length > 1) { UnicodeUtility.GetUtf16SurrogatesFromSupplementaryPlaneScalar((uint)value._value, out destination[0], out destination[1]); charsWritten = 2; @@ -1071,7 +1071,7 @@ private static bool TryEncodeToUtf8(Rune value, Span destination, out int return true; } - if (1 < (uint)destination.Length) + if (destination.Length > 1) { if (value.Value <= 0x7FFu) { @@ -1082,7 +1082,7 @@ private static bool TryEncodeToUtf8(Rune value, Span destination, out int return true; } - if (2 < (uint)destination.Length) + if (destination.Length > 2) { if (value.Value <= 0xFFFFu) { @@ -1094,7 +1094,7 @@ private static bool TryEncodeToUtf8(Rune value, Span destination, out int return true; } - if (3 < (uint)destination.Length) + if (destination.Length > 3) { // Scalar 000uuuuu zzzzyyyy yyxxxxxx -> bytes [ 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx ] destination[0] = (byte)((value._value + (0b11110 << 21)) >> 18); diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs index 6b7a085b1bcbb..64ce8fc7c224c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs @@ -1051,10 +1051,10 @@ public StringBuilder Append(char value) int nextCharIndex = m_ChunkLength; char[] chars = m_ChunkChars; - if ((uint)chars.Length > (uint)nextCharIndex) + if ((uint)nextCharIndex < chars.Length) { chars[nextCharIndex] = value; - m_ChunkLength++; + m_ChunkLength = nextCharIndex + 1; } else { From af9b2d6c7271a5403c7845ce8257be41c38a8c00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Fri, 1 Apr 2022 20:43:28 +0200 Subject: [PATCH 02/12] Fixed the bug I introduced in TimeSpanParse.TimeSpanTokenizer --- .../src/System/Globalization/TimeSpanParse.cs | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanParse.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanParse.cs index 8cf2b63ef0bb7..5a585dc83ff5e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanParse.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanParse.cs @@ -169,7 +169,8 @@ internal TimeSpanToken GetNextToken() { // Get the position of the next character to be processed. If there is no // next character, we're at the end. - int pos = _pos; + int startPos = _pos; + int pos = startPos; ReadOnlySpan value = _value; Debug.Assert(pos > -1); if ((uint)pos >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 @@ -190,7 +191,7 @@ internal TimeSpanToken GetNextToken() { int digit; // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 - if ((uint)(++pos) >= (uint)value.Length || (uint)(digit = value[pos] - '0') > 9) + if ((uint)++pos >= (uint)value.Length || (uint)(digit = value[pos] - '0') > 9) { _pos = pos; return new TimeSpanToken(TTT.Num, 0, zeroes, default); @@ -203,18 +204,18 @@ internal TimeSpanToken GetNextToken() } num = digit; - _pos = pos; break; } + + _pos = pos; } // Continue to read as long as we're reading digits. - while ((uint)(++pos) < (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + while ((uint)++pos < (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 { int digit = value[pos] - '0'; if ((uint)digit > 9) { - _pos = pos; break; } @@ -233,19 +234,15 @@ internal TimeSpanToken GetNextToken() // Otherwise, we're processing a separator, and we've already processed the first // character of it. Continue processing characters as long as they're not digits. int length = 1; - while (true) + // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + while ((uint)++pos < (uint)value.Length && (uint)(value[pos] - '0') > 9) { - // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 - if ((uint)(++pos) >= (uint)value.Length || (uint)(value[pos] - '0') <= 9) - { - break; - } length++; } _pos = pos; // Return the separator. - return new TimeSpanToken(TTT.Sep, 0, 0, _value.Slice(pos, length)); + return new TimeSpanToken(TTT.Sep, 0, 0, _value.Slice(startPos, length)); } internal bool EOL => _pos >= (_value.Length - 1); From 19b8e00288c246bda2c9081123734d496be9ab65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Sat, 2 Apr 2022 19:28:46 +0200 Subject: [PATCH 03/12] Use uint-cast on both sided for clarity Cf. https://github.com/dotnet/runtime/pull/67448#discussion_r841082992 --- .../src/System/Array.CoreCLR.cs | 4 +- .../System/Collections/Generic/BitHelper.cs | 6 +- .../src/System/Text/ValueStringBuilder.cs | 3 +- .../src/System/Array.Enumerators.cs | 4 +- .../src/System/Array.cs | 6 +- .../src/System/BitConverter.cs | 6 +- .../Utf8Formatter.Integer.Unsigned.X.cs | 2 - .../TlsOverPerCoreLockedStacksArrayPool.cs | 10 +- .../System.Private.CoreLib/src/System/Char.cs | 34 ++--- .../Collections/Generic/ArraySortHelper.cs | 1 - .../System/Collections/Generic/Dictionary.cs | 42 +++--- .../src/System/Collections/Generic/List.cs | 2 +- .../Collections/Generic/ValueListBuilder.cs | 2 +- .../System/Globalization/CompareInfo.Icu.cs | 2 +- .../src/System/Globalization/DateTimeParse.cs | 4 +- .../src/System/Globalization/StringInfo.cs | 4 +- .../src/System/Globalization/TimeSpanParse.cs | 10 +- .../System.Private.CoreLib/src/System/Guid.cs | 1 - .../src/System/IO/Stream.cs | 2 +- .../src/System/IO/TextReader.cs | 4 +- .../src/System/MemoryExtensions.cs | 2 +- .../src/System/Number.Parsing.cs | 121 +++++++++--------- .../DefaultInterpolatedStringHandler.cs | 2 +- .../src/System/String.Manipulation.cs | 3 +- .../src/System/Text/DecoderNLS.cs | 4 +- .../src/System/Text/Rune.cs | 20 +-- .../src/System/Text/StringBuilder.cs | 2 +- 27 files changed, 146 insertions(+), 157 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs index 3e55907c3c813..5538d58e8d969 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs @@ -392,7 +392,7 @@ internal T get_Item(int index) // ! Warning: "this" is an array, not an SZArrayHelper. See comments above // ! or you may introduce a security hole! T[] _this = Unsafe.As(this); - if ((uint)index >= _this.Length) + if ((uint)index >= (uint)_this.Length) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } @@ -405,7 +405,7 @@ internal void set_Item(int index, T value) // ! Warning: "this" is an array, not an SZArrayHelper. See comments above // ! or you may introduce a security hole! T[] _this = Unsafe.As(this); - if ((uint)index >= _this.Length) + if ((uint)index >= (uint)_this.Length) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } diff --git a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs index 5c66096f05b61..7a7b9067a6b15 100644 --- a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs +++ b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Runtime.CompilerServices; - namespace System.Collections.Generic { internal ref struct BitHelper @@ -23,7 +21,7 @@ internal void MarkBit(int bitPosition) { (int bitArrayIndex, int position) = Math.DivRem(bitPosition, IntSize); Span span = _span; - if ((uint)bitArrayIndex < (uint)span.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)bitArrayIndex < (uint)span.Length) { span[bitArrayIndex] |= 1 << position; } @@ -35,7 +33,7 @@ internal bool IsMarked(int bitPosition) (int bitArrayIndex, int position) = Math.DivRem(bitPosition, IntSize); Span span = _span; return - (uint)bitArrayIndex < (uint)span.Length && // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + (uint)bitArrayIndex < (uint)span.Length && (span[bitArrayIndex] & (1 << position)) != 0; } diff --git a/src/libraries/Common/src/System/Text/ValueStringBuilder.cs b/src/libraries/Common/src/System/Text/ValueStringBuilder.cs index bbcee17ae9068..6a08797c244b3 100644 --- a/src/libraries/Common/src/System/Text/ValueStringBuilder.cs +++ b/src/libraries/Common/src/System/Text/ValueStringBuilder.cs @@ -171,7 +171,7 @@ public void Append(char c) { int pos = _pos; Span chars = _chars; - if ((uint)pos < (uint)chars.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)pos < (uint)chars.Length) { chars[pos] = c; _pos = pos + 1; @@ -192,7 +192,6 @@ public void Append(string? s) int pos = _pos; Span chars = _chars; - // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 if (s.Length == 1 && (uint)pos < (uint)chars.Length) // very common case, e.g. appending strings from NumberFormatInfo like separators, percent symbols, etc. { chars[pos] = s[0]; diff --git a/src/libraries/System.Private.CoreLib/src/System/Array.Enumerators.cs b/src/libraries/System.Private.CoreLib/src/System/Array.Enumerators.cs index d20096eeaafe4..04c972c0a3c45 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Array.Enumerators.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Array.Enumerators.cs @@ -86,7 +86,7 @@ internal SZGenericArrayEnumerator(T[] array) public bool MoveNext() { int index = _index + 1; - if ((uint)index >= _array.Length) + if ((uint)index >= (uint)_array.Length) { _index = _array.Length; return false; @@ -102,7 +102,7 @@ public T Current int index = _index; T[] array = _array; - if ((uint)index >= array.Length) + if ((uint)index >= (uint)array.Length) { ThrowHelper.ThrowInvalidOperationException_EnumCurrent(index); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Array.cs b/src/libraries/System.Private.CoreLib/src/System/Array.cs index 35046bb4068ef..663d5f9ba4d7f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Array.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Array.cs @@ -917,7 +917,7 @@ public static void Fill(T[] array, T value, int startIndex, int count) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } - if ((uint)startIndex > array.Length) + if ((uint)startIndex > (uint)array.Length) { ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index(); } @@ -1314,7 +1314,7 @@ public static int IndexOf(T[] array, T value, int startIndex, int count) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } - if ((uint)startIndex > array.Length) + if ((uint)startIndex > (uint)array.Length) { ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index(); } @@ -1570,7 +1570,7 @@ public static int LastIndexOf(T[] array, T value, int startIndex, int count) } // Make sure we're not out of range - if ((uint)startIndex >= array.Length) + if ((uint)startIndex >= (uint)array.Length) { ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index(); } diff --git a/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs b/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs index 712ed9fdb88e1..daedb49aaf659 100644 --- a/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs +++ b/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs @@ -365,7 +365,7 @@ public static short ToInt16(byte[] value, int startIndex) { if (value == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); - if (unchecked((uint)startIndex) >= unchecked(value.Length)) + if (unchecked((uint)startIndex) >= unchecked((uint)value.Length)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); if (startIndex > value.Length - sizeof(short)) ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall, ExceptionArgument.value); @@ -403,7 +403,7 @@ public static int ToInt32(byte[] value, int startIndex) { if (value == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); - if (unchecked((uint)startIndex) >= unchecked(value.Length)) + if (unchecked((uint)startIndex) >= unchecked((uint)value.Length)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); if (startIndex > value.Length - sizeof(int)) ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall, ExceptionArgument.value); @@ -441,7 +441,7 @@ public static long ToInt64(byte[] value, int startIndex) { if (value == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); - if (unchecked((uint)startIndex) >= unchecked(value.Length)) + if (unchecked((uint)startIndex) >= unchecked((uint)value.Length)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index); if (startIndex > value.Length - sizeof(long)) ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall, ExceptionArgument.value); diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.X.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.X.cs index cfe2a2dc58ef1..00ea1d2e29b5d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.X.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.X.cs @@ -35,7 +35,6 @@ private static bool TryFormatUInt64X(ulong value, byte precision, bool useLower, if (useLower) { - // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 while ((uint)(--computedOutputLength) < (uint)destination.Length) { destination[computedOutputLength] = (byte)HexConverter.ToCharLower((int)value); @@ -44,7 +43,6 @@ private static bool TryFormatUInt64X(ulong value, byte precision, bool useLower, } else { - // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 while ((uint)(--computedOutputLength) < (uint)destination.Length) { destination[computedOutputLength] = (byte)HexConverter.ToCharUpper((int)value); diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs index fdce9a0998a6b..9206dde5e5fa6 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs @@ -61,7 +61,7 @@ public override T[] Rent(int minimumLength) // First, try to get an array from TLS if possible. ThreadLocalArray[]? tlsBuckets = t_tlsBuckets; - if (tlsBuckets is not null && (uint)bucketIndex < tlsBuckets.Length) + if (tlsBuckets is not null && (uint)bucketIndex < (uint)tlsBuckets.Length) { buffer = tlsBuckets[bucketIndex].Array; if (buffer is not null) @@ -77,7 +77,7 @@ public override T[] Rent(int minimumLength) // Next, try to get an array from one of the per-core stacks. PerCoreLockedStacks?[] perCoreBuckets = _buckets; - if ((uint)bucketIndex < perCoreBuckets.Length) + if ((uint)bucketIndex < (uint)perCoreBuckets.Length) { PerCoreLockedStacks? b = perCoreBuckets[bucketIndex]; if (b is not null) @@ -140,7 +140,7 @@ public override void Return(T[] array, bool clearArray = false) bool haveBucket = false; bool returned = true; - if ((uint)bucketIndex < tlsBuckets.Length) + if ((uint)bucketIndex < (uint)tlsBuckets.Length) { haveBucket = true; @@ -374,7 +374,7 @@ public bool TryPush(T[] array) Monitor.Enter(this); T[]?[] arrays = _arrays; int count = _count; - if ((uint)count < arrays.Length) + if ((uint)count < (uint)arrays.Length) { if (count == 0) { @@ -398,7 +398,7 @@ public bool TryPush(T[] array) Monitor.Enter(this); T[]?[] arrays = _arrays; int count = _count - 1; - if ((uint)count < arrays.Length) + if ((uint)count < (uint)arrays.Length) { arr = arrays[count]; arrays[count] = null; diff --git a/src/libraries/System.Private.CoreLib/src/System/Char.cs b/src/libraries/System.Private.CoreLib/src/System/Char.cs index 4d81144066dc8..39c872f07d4bf 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Char.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Char.cs @@ -508,7 +508,7 @@ public static bool IsControl(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -523,7 +523,7 @@ public static bool IsDigit(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -543,7 +543,7 @@ public static bool IsLetter(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -564,7 +564,7 @@ public static bool IsLetterOrDigit(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -584,7 +584,7 @@ public static bool IsLower(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -626,7 +626,7 @@ public static bool IsNumber(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -659,7 +659,7 @@ public static bool IsPunctuation(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -704,7 +704,7 @@ public static bool IsSeparator(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -729,7 +729,7 @@ public static bool IsSurrogate(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -761,7 +761,7 @@ public static bool IsSymbol(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -781,7 +781,7 @@ public static bool IsUpper(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -801,7 +801,7 @@ public static bool IsWhiteSpace(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -827,7 +827,7 @@ public static UnicodeCategory GetUnicodeCategory(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -851,7 +851,7 @@ public static double GetNumericValue(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -873,7 +873,7 @@ public static bool IsHighSurrogate(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -895,7 +895,7 @@ public static bool IsLowSurrogate(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } @@ -912,7 +912,7 @@ public static bool IsSurrogatePair(string s, int index) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - if ((uint)index >= s.Length) + if ((uint)index >= (uint)s.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs index 55d670fb45342..201f52af7f853 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs @@ -1119,7 +1119,6 @@ public static int MoveNansToFront(Span keys, Span va keys[left] = keys[i]; keys[i] = temp; - // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 if ((uint)i < (uint)values.Length) // check to see if we have values { TValue tempValue = values[left]; diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs index 433cfd692539d..9107c4f375d35 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs @@ -305,7 +305,7 @@ private void CopyTo(KeyValuePair[] array, int index) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } - if ((uint)index > array.Length) + if ((uint)index > (uint)array.Length) { ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); } @@ -377,7 +377,7 @@ internal ref TValue FindValue(TKey key) { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test in if to drop range check for following array access - if ((uint)i >= entries.Length) + if ((uint)i >= (uint)entries.Length) { goto ReturnNotFound; } @@ -391,7 +391,7 @@ internal ref TValue FindValue(TKey key) i = entry.next; collisionCount++; - } while (collisionCount <= entries.Length); + } while (collisionCount <= (uint)entries.Length); // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -409,7 +409,7 @@ internal ref TValue FindValue(TKey key) { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test in if to drop range check for following array access - if ((uint)i >= entries.Length) + if ((uint)i >= (uint)entries.Length) { goto ReturnNotFound; } @@ -423,7 +423,7 @@ internal ref TValue FindValue(TKey key) i = entry.next; collisionCount++; - } while (collisionCount <= entries.Length); + } while (collisionCount <= (uint)entries.Length); // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -441,7 +441,7 @@ internal ref TValue FindValue(TKey key) { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test in if to drop range check for following array access - if ((uint)i >= entries.Length) + if ((uint)i >= (uint)entries.Length) { goto ReturnNotFound; } @@ -455,7 +455,7 @@ internal ref TValue FindValue(TKey key) i = entry.next; collisionCount++; - } while (collisionCount <= entries.Length); + } while (collisionCount <= (uint)entries.Length); // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -528,7 +528,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior) { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test uint in if rather than loop condition to drop range check for following array access - if ((uint)i >= entries.Length) + if ((uint)i >= (uint)entries.Length) { break; } @@ -552,7 +552,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior) i = entries[i].next; collisionCount++; - if (collisionCount > entries.Length) + if (collisionCount > (uint)entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -570,7 +570,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior) { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test uint in if rather than loop condition to drop range check for following array access - if ((uint)i >= entries.Length) + if ((uint)i >= (uint)entries.Length) { break; } @@ -594,7 +594,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior) i = entries[i].next; collisionCount++; - if (collisionCount > entries.Length) + if (collisionCount > (uint)entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -609,7 +609,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior) { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test uint in if rather than loop condition to drop range check for following array access - if ((uint)i >= entries.Length) + if ((uint)i >= (uint)entries.Length) { break; } @@ -633,7 +633,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior) i = entries[i].next; collisionCount++; - if (collisionCount > entries.Length) + if (collisionCount > (uint)entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -725,7 +725,7 @@ internal static class CollectionsMarshalHelper { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test uint in if rather than loop condition to drop range check for following array access - if ((uint)i >= entries.Length) + if ((uint)i >= (uint)entries.Length) { break; } @@ -740,7 +740,7 @@ internal static class CollectionsMarshalHelper i = entries[i].next; collisionCount++; - if (collisionCount > entries.Length) + if (collisionCount > (uint)entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -758,7 +758,7 @@ internal static class CollectionsMarshalHelper { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test uint in if rather than loop condition to drop range check for following array access - if ((uint)i >= entries.Length) + if ((uint)i >= (uint)entries.Length) { break; } @@ -773,7 +773,7 @@ internal static class CollectionsMarshalHelper i = entries[i].next; collisionCount++; - if (collisionCount > entries.Length) + if (collisionCount > (uint)entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -788,7 +788,7 @@ internal static class CollectionsMarshalHelper { // Should be a while loop https://github.com/dotnet/runtime/issues/9422 // Test uint in if rather than loop condition to drop range check for following array access - if ((uint)i >= entries.Length) + if ((uint)i >= (uint)entries.Length) { break; } @@ -803,7 +803,7 @@ internal static class CollectionsMarshalHelper i = entries[i].next; collisionCount++; - if (collisionCount > entries.Length) + if (collisionCount > (uint)entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -1020,7 +1020,7 @@ public bool Remove(TKey key) i = entry.next; collisionCount++; - if (collisionCount > entries.Length) + if (collisionCount > (uint)entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. @@ -1090,7 +1090,7 @@ public bool Remove(TKey key, [MaybeNullWhen(false)] out TValue value) i = entry.next; collisionCount++; - if (collisionCount > entries.Length) + if (collisionCount > (uint)entries.Length) { // The chain of entries forms a loop; which means a concurrent update has happened. // Break out of the loop and throw, rather than looping forever. diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs index c31df96b1be0d..cc79cb9ab8e56 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs @@ -198,7 +198,7 @@ public void Add(T item) _version++; T[] array = _items; int size = _size; - if ((uint)size < array.Length) + if ((uint)size < (uint)array.Length) { _size = size + 1; array[size] = item; diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs index c191ff69c6141..4833c4d2e1d3f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs @@ -44,7 +44,7 @@ public ref T this[int index] public void Append(T item) { int pos = _pos; - Span span = _span; // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + Span span = _span; if ((uint)pos < (uint)span.Length) { span[pos] = item; diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs index ca5bcfaa02484..f4d6ebf23d1d1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs @@ -659,7 +659,7 @@ private unsafe int IcuGetSortKey(ReadOnlySpan source, Span destinati // The check below also handles errors due to negative values / overflow being returned. - if ((uint)actualSortKeyLength > (uint)destination.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)actualSortKeyLength > (uint)destination.Length) { if (actualSortKeyLength > destination.Length) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs index a1aa99a666047..c45247cfd1538 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs @@ -4392,7 +4392,7 @@ internal static bool TryParseQuoteString(ReadOnlySpan format, int pos, ref char quoteChar = format[pos++]; // Get the character used to quote the following string. bool foundQuote = false; - while ((uint)pos < (uint)format.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + while ((uint)pos < (uint)format.Length) { char ch = format[pos++]; if (ch == quoteChar) @@ -4407,7 +4407,7 @@ internal static bool TryParseQuoteString(ReadOnlySpan format, int pos, ref // Therefore, someone can use a format like "'minute:' mm\"" to display: // minute: 45" // because the second double quote is escaped. - if ((uint)pos < (uint)format.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)pos < (uint)format.Length) { result.Append(format[pos++]); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/StringInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/StringInfo.cs index 145df6078686d..365bcb3f25d19 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/StringInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/StringInfo.cs @@ -75,7 +75,7 @@ public string SubstringByTextElements(int startingTextElement, int lengthInTextE { int[] indexes = Indexes ?? Array.Empty(); - if ((uint)startingTextElement >= indexes.Length) + if ((uint)startingTextElement >= (uint)indexes.Length) { throw new ArgumentOutOfRangeException(nameof(startingTextElement), startingTextElement, SR.Arg_ArgumentOutOfRangeException); } @@ -87,7 +87,7 @@ public string SubstringByTextElements(int startingTextElement, int lengthInTextE int start = indexes[startingTextElement]; Index end = ^0; // assume reading to end of the string unless the caller told us to stop early - if ((uint)(startingTextElement + lengthInTextElements) < indexes.Length) + if ((uint)(startingTextElement + lengthInTextElements) < (uint)indexes.Length) { end = indexes[startingTextElement + lengthInTextElements]; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanParse.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanParse.cs index 5a585dc83ff5e..10f069a4d0468 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanParse.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanParse.cs @@ -173,7 +173,7 @@ internal TimeSpanToken GetNextToken() int pos = startPos; ReadOnlySpan value = _value; Debug.Assert(pos > -1); - if ((uint)pos >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)pos >= (uint)value.Length) { return new TimeSpanToken(TTT.End); } @@ -190,7 +190,6 @@ internal TimeSpanToken GetNextToken() while (true) { int digit; - // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 if ((uint)++pos >= (uint)value.Length || (uint)(digit = value[pos] - '0') > 9) { _pos = pos; @@ -211,7 +210,7 @@ internal TimeSpanToken GetNextToken() } // Continue to read as long as we're reading digits. - while ((uint)++pos < (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + while ((uint)++pos < (uint)value.Length) { int digit = value[pos] - '0'; if ((uint)digit > 9) @@ -234,7 +233,6 @@ internal TimeSpanToken GetNextToken() // Otherwise, we're processing a separator, and we've already processed the first // character of it. Continue processing characters as long as they're not digits. int length = 1; - // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 while ((uint)++pos < (uint)value.Length && (uint)(value[pos] - '0') > 9) { length++; @@ -255,7 +253,7 @@ internal void BackOne() internal char NextChar() { int pos = ++_pos; - return (uint)pos < (uint)_value.Length ? // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + return (uint)pos < (uint)_value.Length ? _value[pos] : (char)0; } @@ -1264,7 +1262,7 @@ private static bool TryParseByFormat(ReadOnlySpan input, ReadOnlySpan guidString, ref GuidResult } // Check for '{' - // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 if ((uint)guidString.Length <= (uint)(numStart + numLen + 1) || guidString[numStart + numLen + 1] != '{') { result.SetFailure(overflow: false, nameof(SR.Format_GuidBrace)); diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs index 67cd3e27e1f61..53f5920b96703 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs @@ -680,7 +680,7 @@ public virtual int Read(Span buffer) try { int numRead = Read(sharedBuffer, 0, buffer.Length); - if ((uint)numRead > (uint)buffer.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)numRead > (uint)buffer.Length) { throw new IOException(SR.IO_StreamTooLong); } diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/TextReader.cs b/src/libraries/System.Private.CoreLib/src/System/IO/TextReader.cs index edc81e18e4645..299103cf093eb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/TextReader.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/TextReader.cs @@ -104,7 +104,7 @@ public virtual int Read(Span buffer) try { int numRead = Read(array, 0, buffer.Length); - if ((uint)numRead > (uint)buffer.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)numRead > (uint)buffer.Length) { throw new IOException(SR.IO_InvalidReadLength); } @@ -154,7 +154,7 @@ public virtual int ReadBlock(Span buffer) try { int numRead = ReadBlock(array, 0, buffer.Length); - if ((uint)numRead > (uint)buffer.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)numRead > (uint)buffer.Length) { throw new IOException(SR.IO_InvalidReadLength); } diff --git a/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs b/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs index f1bfa12f9a3ab..6563f2ca697f3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs +++ b/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs @@ -2110,7 +2110,7 @@ public bool AppendLiteral(string value) { Span destination = _destination; int pos = _pos; - if ((uint)pos < (uint)destination.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)pos < (uint)destination.Length) { destination[pos] = value[0]; _pos = pos + 1; diff --git a/src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs b/src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs index f87caaa224d33..d9c0662e81d25 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs @@ -554,7 +554,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -571,14 +571,14 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value { sign = -1; index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } else if (num == '+') { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -587,7 +587,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value { sign = -1; index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -599,7 +599,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value if (!string.IsNullOrEmpty(positiveSign) && value.StartsWith(positiveSign)) { index += positiveSign.Length; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -607,7 +607,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value { sign = -1; index += negativeSign.Length; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -625,7 +625,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; } while (num == '0'); @@ -638,7 +638,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value index++; for (int i = 0; i < 8; i++) // next 8 digits can't overflow { - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; if (!IsDigit(num)) @@ -647,7 +647,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value answer = 10 * answer + num - '0'; } - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; if (!IsDigit(num)) @@ -657,7 +657,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value overflow = answer > int.MaxValue / 10; answer = answer * 10 + num - '0'; overflow |= (uint)answer > int.MaxValue + (((uint)sign) >> 31); - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; // At this point, we're either overflowing or hitting a formatting error. @@ -667,7 +667,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value { overflow = true; index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto OverflowExit; num = value[index]; } @@ -706,7 +706,7 @@ internal static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan value if (!IsWhite(value[index])) break; } - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; } @@ -733,7 +733,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -750,14 +750,14 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value { sign = -1; index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } else if (num == '+') { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -766,7 +766,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value { sign = -1; index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -778,7 +778,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value if (!string.IsNullOrEmpty(positiveSign) && value.StartsWith(positiveSign)) { index += positiveSign.Length; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -786,7 +786,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value { sign = -1; index += negativeSign.Length; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -804,7 +804,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; } while (num == '0'); @@ -817,7 +817,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value index++; for (int i = 0; i < 17; i++) // next 17 digits can't overflow { - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; if (!IsDigit(num)) @@ -826,7 +826,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value answer = 10 * answer + num - '0'; } - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; if (!IsDigit(num)) @@ -836,7 +836,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value overflow = answer > long.MaxValue / 10; answer = answer * 10 + num - '0'; overflow |= (ulong)answer > (ulong)long.MaxValue + (((uint)sign) >> 31); - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; // At this point, we're either overflowing or hitting a formatting error. @@ -846,7 +846,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value { overflow = true; index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto OverflowExit; num = value[index]; } @@ -885,7 +885,7 @@ internal static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan value if (!IsWhite(value[index])) break; } - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; } @@ -985,7 +985,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1001,7 +1001,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu if (num == '+') { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1009,7 +1009,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu { overflow = true; index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1018,7 +1018,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu { overflow = true; index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1030,7 +1030,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu if (!string.IsNullOrEmpty(positiveSign) && value.StartsWith(positiveSign)) { index += positiveSign.Length; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1038,7 +1038,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu { overflow = true; index += negativeSign.Length; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1055,7 +1055,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; } while (num == '0'); @@ -1068,7 +1068,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu index++; for (int i = 0; i < 8; i++) // next 8 digits can't overflow { - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; num = value[index]; if (!IsDigit(num)) @@ -1077,7 +1077,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu answer = 10 * answer + num - '0'; } - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; num = value[index]; if (!IsDigit(num)) @@ -1086,7 +1086,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu // Potential overflow now processing the 10th digit. overflow |= (uint)answer > uint.MaxValue / 10 || ((uint)answer == uint.MaxValue / 10 && num > '5'); answer = answer * 10 + num - '0'; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; // At this point, we're either overflowing or hitting a formatting error. @@ -1096,7 +1096,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu { overflow = true; index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto OverflowExit; num = value[index]; } @@ -1137,7 +1137,7 @@ internal static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan valu if (!IsWhite(value[index])) break; } - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; } @@ -1164,7 +1164,7 @@ internal static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan va do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1182,7 +1182,7 @@ internal static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan va do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; } while (num == '0'); @@ -1195,7 +1195,7 @@ internal static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan va index++; for (int i = 0; i < 7; i++) // next 7 digits can't overflow { - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; @@ -1207,7 +1207,7 @@ internal static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan va } // If there's another digit, it's an overflow. - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; if (!HexConverter.IsHexChar(num)) @@ -1218,7 +1218,7 @@ internal static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan va do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto OverflowExit; num = value[index]; } while (HexConverter.IsHexChar(num)); @@ -1258,7 +1258,7 @@ internal static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan va if (!IsWhite(value[index])) break; } - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; } @@ -1321,7 +1321,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1337,7 +1337,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu if (num == '+') { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1345,7 +1345,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu { overflow = true; index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1354,7 +1354,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu { overflow = true; index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1366,7 +1366,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu if (!string.IsNullOrEmpty(positiveSign) && value.StartsWith(positiveSign)) { index += positiveSign.Length; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1374,7 +1374,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu { overflow = true; index += negativeSign.Length; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1391,7 +1391,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; } while (num == '0'); @@ -1404,7 +1404,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu index++; for (int i = 0; i < 18; i++) // next 18 digits can't overflow { - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; num = value[index]; if (!IsDigit(num)) @@ -1413,7 +1413,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu answer = 10 * answer + num - '0'; } - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; num = value[index]; if (!IsDigit(num)) @@ -1422,7 +1422,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu // Potential overflow now processing the 20th digit. overflow |= (ulong)answer > ulong.MaxValue / 10 || ((ulong)answer == ulong.MaxValue / 10 && num > '5'); answer = answer * 10 + num - '0'; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; // At this point, we're either overflowing or hitting a formatting error. @@ -1432,7 +1432,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu { overflow = true; index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto OverflowExit; num = value[index]; } @@ -1473,7 +1473,7 @@ internal static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan valu if (!IsWhite(value[index])) break; } - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; } @@ -1500,7 +1500,7 @@ private static ParsingStatus TryParseUInt64HexNumberStyle(ReadOnlySpan val do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto FalseExit; num = value[index]; } @@ -1518,7 +1518,7 @@ private static ParsingStatus TryParseUInt64HexNumberStyle(ReadOnlySpan val do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; } while (num == '0'); @@ -1531,7 +1531,7 @@ private static ParsingStatus TryParseUInt64HexNumberStyle(ReadOnlySpan val index++; for (int i = 0; i < 15; i++) // next 15 digits can't overflow { - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; @@ -1543,7 +1543,7 @@ private static ParsingStatus TryParseUInt64HexNumberStyle(ReadOnlySpan val } // If there's another digit, it's an overflow. - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEnd; num = value[index]; if (!HexConverter.IsHexChar(num)) @@ -1554,7 +1554,7 @@ private static ParsingStatus TryParseUInt64HexNumberStyle(ReadOnlySpan val do { index++; - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto OverflowExit; num = value[index]; } while (HexConverter.IsHexChar(num)); @@ -1594,7 +1594,7 @@ private static ParsingStatus TryParseUInt64HexNumberStyle(ReadOnlySpan val if (!IsWhite(value[index])) break; } - if ((uint)index >= (uint)value.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)value.Length) goto DoneAtEndButPotentialOverflow; } @@ -2014,7 +2014,6 @@ internal static unsafe bool TryStringToNumber(ReadOnlySpan value, NumberSt private static bool TrailingZeros(ReadOnlySpan value, int index) { // For compatibility, we need to allow trailing zeros at the end of a number string - // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 for (int i = index; (uint)i < (uint)value.Length; i++) { if (value[i] != '\0') diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/DefaultInterpolatedStringHandler.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/DefaultInterpolatedStringHandler.cs index 30b499e210d0d..3b46f1909ebde 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/DefaultInterpolatedStringHandler.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/DefaultInterpolatedStringHandler.cs @@ -156,7 +156,7 @@ public void AppendLiteral(string value) { int pos = _pos; Span chars = _chars; - if ((uint)pos < (uint)chars.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)pos < (uint)chars.Length) { chars[pos] = value[0]; _pos = pos + 1; diff --git a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs index a4de691e79b92..d883782dbe5a3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs @@ -1235,7 +1235,7 @@ internal static int IndexOfNewlineChar(ReadOnlySpan text, out int stride) stride = default; int idx = text.IndexOfAny(needles); - if ((uint)idx < (uint)text.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)idx < (uint)text.Length) { stride = 1; // needle found @@ -1245,7 +1245,6 @@ internal static int IndexOfNewlineChar(ReadOnlySpan text, out int stride) if (text[idx] == '\r') { int nextCharIdx = idx + 1; - // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 if ((uint)nextCharIdx < (uint)text.Length && text[nextCharIdx] == '\n') { stride = 2; diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/DecoderNLS.cs b/src/libraries/System.Private.CoreLib/src/System/Text/DecoderNLS.cs index 5eb5652b6a30a..b93c99ebfe573 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/DecoderNLS.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/DecoderNLS.cs @@ -381,7 +381,7 @@ private static int ConcatInto(ReadOnlySpan srcLeft, ReadOnlySpan src for (int i = 0; i < srcLeft.Length; i++) { - if ((uint)total >= (uint)dest.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)total >= (uint)dest.Length) { goto Finish; } @@ -393,7 +393,7 @@ private static int ConcatInto(ReadOnlySpan srcLeft, ReadOnlySpan src for (int i = 0; i < srcRight.Length; i++) { - if ((uint)total >= (uint)dest.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)total >= (uint)dest.Length) { goto Finish; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/Rune.cs b/src/libraries/System.Private.CoreLib/src/System/Text/Rune.cs index 091c410bfe12c..32b0ade1cff09 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/Rune.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/Rune.cs @@ -399,7 +399,7 @@ public static OperationStatus DecodeFromUtf8(ReadOnlySpan source, out Rune // Try reading input[0]. - if ((uint)index >= (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)source.Length) { goto NeedsMoreData; } @@ -434,7 +434,7 @@ public static OperationStatus DecodeFromUtf8(ReadOnlySpan source, out Rune // Try reading input[1]. index++; - if ((uint)index >= (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)source.Length) { goto NeedsMoreData; } @@ -488,7 +488,7 @@ public static OperationStatus DecodeFromUtf8(ReadOnlySpan source, out Rune // Try reading input[2]. index++; - if ((uint)index >= (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)source.Length) { goto NeedsMoreData; } @@ -513,7 +513,7 @@ public static OperationStatus DecodeFromUtf8(ReadOnlySpan source, out Rune // Try reading input[3]. index++; - if ((uint)index >= (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)source.Length) { goto NeedsMoreData; } @@ -563,7 +563,7 @@ public static OperationStatus DecodeFromUtf8(ReadOnlySpan source, out Rune public static OperationStatus DecodeLastFromUtf16(ReadOnlySpan source, out Rune result, out int charsConsumed) { int index = source.Length - 1; - if ((uint)index < (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index < (uint)source.Length) { // First, check for the common case of a BMP scalar value. // If this is correct, return immediately. @@ -582,7 +582,7 @@ public static OperationStatus DecodeLastFromUtf16(ReadOnlySpan source, out // we have a standalone low surrogate, which is always invalid. index--; - if ((uint)index < (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index < (uint)source.Length) { char penultimateChar = source[index]; if (TryCreate(penultimateChar, finalChar, out result)) @@ -623,7 +623,7 @@ public static OperationStatus DecodeLastFromUtf16(ReadOnlySpan source, out public static OperationStatus DecodeLastFromUtf8(ReadOnlySpan source, out Rune value, out int bytesConsumed) { int index = source.Length - 1; - if ((uint)index < (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index < (uint)source.Length) { // The buffer contains at least one byte. Let's check the fast case where the // buffer ends with an ASCII byte. @@ -657,7 +657,7 @@ public static OperationStatus DecodeLastFromUtf8(ReadOnlySpan source, out for (int i = 3; i > 0; i--) { index--; - if ((uint)index >= (uint)source.Length) // TODO: https://github.com/dotnet/runtime/issues/67044#issuecomment-1085012303 + if ((uint)index >= (uint)source.Length) { goto Invalid; // out of data } @@ -847,7 +847,7 @@ private static int ReadRuneFromString(string input, int index) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.input); } - if ((uint)index >= input.Length) + if ((uint)index >= (uint)input.Length) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } @@ -870,7 +870,7 @@ private static int ReadRuneFromString(string input, int index) // the string terminates unexpectedly. index++; - if ((uint)index >= input.Length) + if ((uint)index >= (uint)input.Length) { return -1; // not an argument exception - just a "bad data" failure } diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs index 64ce8fc7c224c..6dacb00c2b78c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs @@ -1051,7 +1051,7 @@ public StringBuilder Append(char value) int nextCharIndex = m_ChunkLength; char[] chars = m_ChunkChars; - if ((uint)nextCharIndex < chars.Length) + if ((uint)nextCharIndex < (uint)chars.Length) { chars[nextCharIndex] = value; m_ChunkLength = nextCharIndex + 1; From 33cd7a1c587a64be1367f009fc3ff85747fb72ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Sat, 2 Apr 2022 20:32:46 +0200 Subject: [PATCH 04/12] Added missing namespace (oops) --- .../Common/src/System/Collections/Generic/BitHelper.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs index 7a7b9067a6b15..7982e6075f342 100644 --- a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs +++ b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Runtime.CompilerServices; + namespace System.Collections.Generic { internal ref struct BitHelper From 20842e6b470fdbf5f8cadfafa413fe88f3fa48a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Mon, 4 Apr 2022 12:04:31 +0200 Subject: [PATCH 05/12] Use uint-division in BitHelper as the JIT can produce more efficient code Cf. https://github.com/dotnet/runtime/pull/67448#discussion_r841239729 --- .../src/System/Collections/Generic/BitHelper.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs index 7982e6075f342..d45546abd3aa0 100644 --- a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs +++ b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs @@ -21,22 +21,22 @@ internal BitHelper(Span span, bool clear) internal void MarkBit(int bitPosition) { - (int bitArrayIndex, int position) = Math.DivRem(bitPosition, IntSize); + (uint bitArrayIndex, uint position) = Math.DivRem((uint)bitPosition, IntSize); Span span = _span; - if ((uint)bitArrayIndex < (uint)span.Length) + if (bitArrayIndex < (uint)span.Length) { - span[bitArrayIndex] |= 1 << position; + span[(int)bitArrayIndex] |= 1 << (int)position; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal bool IsMarked(int bitPosition) { - (int bitArrayIndex, int position) = Math.DivRem(bitPosition, IntSize); + (uint bitArrayIndex, uint position) = Math.DivRem((uint)bitPosition, IntSize); Span span = _span; return - (uint)bitArrayIndex < (uint)span.Length && - (span[bitArrayIndex] & (1 << position)) != 0; + bitArrayIndex < (uint)span.Length && + (span[(int)bitArrayIndex] & (1 << (int)position)) != 0; } /// How many ints must be allocated to represent n bits. Returns (n+31)/32, but avoids overflow. From da31c0b334046205da7aa776dceaecc2c82e56ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Mon, 4 Apr 2022 12:35:01 +0200 Subject: [PATCH 06/12] Don't have struct local in ValueStringBuilder due to hitting JIT optimization limits Cf. https://github.com/dotnet/runtime/pull/67448#discussion_r841129465 --- .../Common/src/System/Text/ValueStringBuilder.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/libraries/Common/src/System/Text/ValueStringBuilder.cs b/src/libraries/Common/src/System/Text/ValueStringBuilder.cs index 6a08797c244b3..9cd033f3986ee 100644 --- a/src/libraries/Common/src/System/Text/ValueStringBuilder.cs +++ b/src/libraries/Common/src/System/Text/ValueStringBuilder.cs @@ -170,10 +170,9 @@ public void Insert(int index, string? s) public void Append(char c) { int pos = _pos; - Span chars = _chars; - if ((uint)pos < (uint)chars.Length) + if ((uint)pos < (uint)_chars.Length) { - chars[pos] = c; + _chars[pos] = c; _pos = pos + 1; } else @@ -191,10 +190,9 @@ public void Append(string? s) } int pos = _pos; - Span chars = _chars; - if (s.Length == 1 && (uint)pos < (uint)chars.Length) // very common case, e.g. appending strings from NumberFormatInfo like separators, percent symbols, etc. + if (s.Length == 1 && (uint)pos < (uint)_chars.Length) // very common case, e.g. appending strings from NumberFormatInfo like separators, percent symbols, etc. { - chars[pos] = s[0]; + _chars[pos] = s[0]; _pos = pos + 1; } else From 7e816a5fcd04f877ff39a1d6a4eafe55ef0c1d00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Mon, 4 Apr 2022 19:44:18 +0200 Subject: [PATCH 07/12] BitHelper with uint division and without Math.DivRem for even better codegen AggressiveInlining isn't needed anymore, also the JIT recognizes the _span field, so no local Span is needed here. --- .../src/System/Collections/Generic/BitHelper.cs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs index d45546abd3aa0..ee71f1002a958 100644 --- a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs +++ b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Runtime.CompilerServices; - namespace System.Collections.Generic { internal ref struct BitHelper @@ -21,22 +19,19 @@ internal BitHelper(Span span, bool clear) internal void MarkBit(int bitPosition) { - (uint bitArrayIndex, uint position) = Math.DivRem((uint)bitPosition, IntSize); - Span span = _span; - if (bitArrayIndex < (uint)span.Length) + uint bitArrayIndex = (uint)bitPosition / IntSize; + if (bitArrayIndex < (uint)_span.Length) { - span[(int)bitArrayIndex] |= 1 << (int)position; + _span[(int)bitArrayIndex] |= (1 << (int)((uint)bitPosition % IntSize)); } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] internal bool IsMarked(int bitPosition) { - (uint bitArrayIndex, uint position) = Math.DivRem((uint)bitPosition, IntSize); - Span span = _span; + uint bitArrayIndex = (uint)bitPosition / IntSize; return - bitArrayIndex < (uint)span.Length && - (span[(int)bitArrayIndex] & (1 << (int)position)) != 0; + bitArrayIndex < (uint)_span.Length && + (_span[(int)bitArrayIndex] & ((int)((uint)bitPosition % IntSize))) != 0; } /// How many ints must be allocated to represent n bits. Returns (n+31)/32, but avoids overflow. From d9f4e01930f78f1c42c29514ea18fe05dee248f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Mon, 4 Apr 2022 19:50:47 +0200 Subject: [PATCH 08/12] BitHelper needs local Span due to regression in jit-diff otherwise --- .../Common/src/System/Collections/Generic/BitHelper.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs index ee71f1002a958..cf843526fb3c2 100644 --- a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs +++ b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs @@ -20,18 +20,20 @@ internal BitHelper(Span span, bool clear) internal void MarkBit(int bitPosition) { uint bitArrayIndex = (uint)bitPosition / IntSize; - if (bitArrayIndex < (uint)_span.Length) + Span span = _span; + if (bitArrayIndex < (uint)span.Length) { - _span[(int)bitArrayIndex] |= (1 << (int)((uint)bitPosition % IntSize)); + span[(int)bitArrayIndex] |= (1 << (int)((uint)bitPosition % IntSize)); } } internal bool IsMarked(int bitPosition) { uint bitArrayIndex = (uint)bitPosition / IntSize; + Span span = _span; return - bitArrayIndex < (uint)_span.Length && - (_span[(int)bitArrayIndex] & ((int)((uint)bitPosition % IntSize))) != 0; + bitArrayIndex < (uint)span.Length && + (span[(int)bitArrayIndex] & ((int)((uint)bitPosition % IntSize))) != 0; } /// How many ints must be allocated to represent n bits. Returns (n+31)/32, but avoids overflow. From 034dd89102a6ed32524e8fe5328108e8e509f850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Tue, 5 Apr 2022 11:08:37 +0200 Subject: [PATCH 09/12] Fixed dumb copy & paste failure in BitHelper.IsMarked --- .../Common/src/System/Collections/Generic/BitHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs index cf843526fb3c2..09e98d2729637 100644 --- a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs +++ b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs @@ -33,7 +33,7 @@ internal bool IsMarked(int bitPosition) Span span = _span; return bitArrayIndex < (uint)span.Length && - (span[(int)bitArrayIndex] & ((int)((uint)bitPosition % IntSize))) != 0; + (span[(int)bitArrayIndex] & (1 << ((int)((uint)bitPosition % IntSize)))) != 0; } /// How many ints must be allocated to represent n bits. Returns (n+31)/32, but avoids overflow. From 186fb580680f8a8fb10aa07bed81b3c42916f4a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Mon, 11 Jul 2022 20:13:14 +0200 Subject: [PATCH 10/12] Added Debug.Asserts to BitHelper's method that take bitPosition --- .../Common/src/System/Collections/Generic/BitHelper.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs index 09e98d2729637..fdcd2e2c39dc5 100644 --- a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs +++ b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics; + namespace System.Collections.Generic { internal ref struct BitHelper @@ -19,6 +21,8 @@ internal BitHelper(Span span, bool clear) internal void MarkBit(int bitPosition) { + Debug.Assert(bitPosition >= 0); + uint bitArrayIndex = (uint)bitPosition / IntSize; Span span = _span; if (bitArrayIndex < (uint)span.Length) @@ -29,6 +33,8 @@ internal void MarkBit(int bitPosition) internal bool IsMarked(int bitPosition) { + Debug.Assert(bitPosition >= 0); + uint bitArrayIndex = (uint)bitPosition / IntSize; Span span = _span; return From 6afa6c2654cced379d920d97a9e3578f87d3fb6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Tue, 12 Jul 2022 11:06:59 +0200 Subject: [PATCH 11/12] BitHelper: comment for workaround to tracking issue --- .../Common/src/System/Collections/Generic/BitHelper.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs index fdcd2e2c39dc5..05f353bb72d51 100644 --- a/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs +++ b/src/libraries/Common/src/System/Collections/Generic/BitHelper.cs @@ -24,6 +24,8 @@ internal void MarkBit(int bitPosition) Debug.Assert(bitPosition >= 0); uint bitArrayIndex = (uint)bitPosition / IntSize; + + // Workaround for https://github.com/dotnet/runtime/issues/72004 Span span = _span; if (bitArrayIndex < (uint)span.Length) { @@ -36,6 +38,8 @@ internal bool IsMarked(int bitPosition) Debug.Assert(bitPosition >= 0); uint bitArrayIndex = (uint)bitPosition / IntSize; + + // Workaround for https://github.com/dotnet/runtime/issues/72004 Span span = _span; return bitArrayIndex < (uint)span.Length && From 26f459f7a7881c9268d76a674cd411576a668220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Tue, 12 Jul 2022 11:14:36 +0200 Subject: [PATCH 12/12] Update src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs --- .../src/System/Collections/Generic/ValueListBuilder.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs index 4833c4d2e1d3f..a30e63947f183 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs @@ -44,6 +44,8 @@ public ref T this[int index] public void Append(T item) { int pos = _pos; + + // Workaround for https://github.com/dotnet/runtime/issues/72004 Span span = _span; if ((uint)pos < (uint)span.Length) {