diff options
author | Dennis "D.C." Dietrich <dennisdietrich@users.noreply.github.com> | 2017-02-07 19:44:18 +0300 |
---|---|---|
committer | Tarek Mahmoud Sayed <tarekms@microsoft.com> | 2017-02-07 19:44:18 +0300 |
commit | 8f824ba00f5656436f7063da49fb0ac94eff8566 (patch) | |
tree | b713833014fffd8206f5628c66fd9a2e6013527f | |
parent | b9e51257e5409f77af2cb3d2566593e97eefba7a (diff) |
Marking code as unchecked (pt 2) (#15885)
Marking code that may intentionally lead to over or underflows with
unchecked in preparation of turning on CheckForOverflowUnderflow for
all projects (issue #3140)
25 files changed, 203 insertions, 128 deletions
diff --git a/src/Common/src/System/Numerics/Hashing/HashHelpers.cs b/src/Common/src/System/Numerics/Hashing/HashHelpers.cs index 166c49fe02..841eb04d87 100644 --- a/src/Common/src/System/Numerics/Hashing/HashHelpers.cs +++ b/src/Common/src/System/Numerics/Hashing/HashHelpers.cs @@ -10,11 +10,13 @@ namespace System.Numerics.Hashing public static int Combine(int h1, int h2) { - // RyuJIT optimizes this to use the ROL instruction - // Related GitHub pull request: dotnet/coreclr#1830 - uint rol5 = ((uint)h1 << 5) | ((uint)h1 >> 27); - return ((int)rol5 + h1) ^ h2; + unchecked + { + // RyuJIT optimizes this to use the ROL instruction + // Related GitHub pull request: dotnet/coreclr#1830 + uint rol5 = ((uint)h1 << 5) | ((uint)h1 >> 27); + return ((int)rol5 + h1) ^ h2; + } } } -} - +}
\ No newline at end of file diff --git a/src/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Syntax/NameTable.cs b/src/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Syntax/NameTable.cs index 0018816ba3..29e844062a 100644 --- a/src/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Syntax/NameTable.cs +++ b/src/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Syntax/NameTable.cs @@ -75,17 +75,22 @@ namespace Microsoft.CSharp.RuntimeBinder.Syntax private int ComputeHashCode(string key) { - int len = key.Length; - int hashCode = len + _hashCodeRandomizer; - // use key.Length to eliminate the range check - for (int i = 0; i < key.Length; i++) + int hashCode, len = key.Length; + + unchecked { - hashCode += (hashCode << 7) ^ key[i]; + hashCode = len + _hashCodeRandomizer; + // use key.Length to eliminate the range check + for (int i = 0; i < key.Length; i++) + { + hashCode += (hashCode << 7) ^ key[i]; + } + // mix it a bit more + hashCode -= hashCode >> 17; + hashCode -= hashCode >> 11; + hashCode -= hashCode >> 5; } - // mix it a bit more - hashCode -= hashCode >> 17; - hashCode -= hashCode >> 11; - hashCode -= hashCode >> 5; + return hashCode; } diff --git a/src/System.Collections/src/System/Collections/BitArray.cs b/src/System.Collections/src/System/Collections/BitArray.cs index 819bf18a2d..75bc51996e 100644 --- a/src/System.Collections/src/System/Collections/BitArray.cs +++ b/src/System.Collections/src/System/Collections/BitArray.cs @@ -527,7 +527,7 @@ namespace System.Collections Array.Copy(m_array, 0, intArray, index, GetArrayLength(m_length, BitsPerInt32) - 1); // the last int needs to be masked - intArray[index + last] = m_array[last] & ((1 << extraBits) - 1); + intArray[index + last] = m_array[last] & unchecked((1 << extraBits) - 1); } } else if (array is byte[]) diff --git a/src/System.Data.Common/src/System/Data/RbTree.cs b/src/System.Data.Common/src/System/Data/RbTree.cs index dd7549911c..230f4c514e 100644 --- a/src/System.Data.Common/src/System/Data/RbTree.cs +++ b/src/System.Data.Common/src/System/Data/RbTree.cs @@ -1991,10 +1991,10 @@ namespace System.Data segmentPos = _nextFreeSlotLine; while (segmentPos < _slotMap.Length) { - if (((uint)_slotMap[segmentPos]) < 0xFFFFFFFF) + if (unchecked((uint)_slotMap[segmentPos]) < 0xFFFFFFFF) { freeSlotId = 0; - freeSlot = (~(_slotMap[segmentPos])) & (_slotMap[segmentPos] + 1); + freeSlot = (~(_slotMap[segmentPos])) & unchecked(_slotMap[segmentPos] + 1); // avoid string concat to allow debug code to run faster Debug.Assert((_slotMap[segmentPos] & freeSlot) == 0, "Slot position segment[segmentPos ]: [freeSlot] is in use. Expected to be empty"); @@ -2006,7 +2006,7 @@ namespace System.Data tree._inUseNodeCount++; // convert freeSlotPos to int value giving number of 0's to its right i.e. freeSlotId - freeSlotId = GetIntValueFromBitMap((uint)freeSlot); + freeSlotId = GetIntValueFromBitMap(unchecked((uint)freeSlot)); _nextFreeSlotLine = segmentPos; freeSlotId = (segmentPos * TreePage.slotLineSize) + freeSlotId; diff --git a/src/System.Data.Common/src/System/Data/SQLTypes/SQLByte.cs b/src/System.Data.Common/src/System/Data/SQLTypes/SQLByte.cs index bd0d1fe420..68fcdc33e7 100644 --- a/src/System.Data.Common/src/System/Data/SQLTypes/SQLByte.cs +++ b/src/System.Data.Common/src/System/Data/SQLTypes/SQLByte.cs @@ -84,7 +84,7 @@ namespace System.Data.SqlTypes // Unary operators public static SqlByte operator ~(SqlByte x) { - return x.IsNull ? Null : new SqlByte((byte)~x._value); + return x.IsNull ? Null : new SqlByte(unchecked((byte)~x._value)); } diff --git a/src/System.Data.Common/src/System/Data/SQLTypes/SQLDecimal.cs b/src/System.Data.Common/src/System/Data/SQLTypes/SQLDecimal.cs index 1304d66db1..20f83d398e 100644 --- a/src/System.Data.Common/src/System/Data/SQLTypes/SQLDecimal.cs +++ b/src/System.Data.Common/src/System/Data/SQLTypes/SQLDecimal.cs @@ -488,12 +488,16 @@ namespace System.Data.SqlTypes // m_data2 = *pInt++; // mid part int[] bits = decimal.GetBits(value); + uint sgnscl; - uint sgnscl = (uint)bits[3]; - _data1 = (uint)bits[0]; - _data2 = (uint)bits[1]; - _data3 = (uint)bits[2]; - _data4 = s_uiZero; + unchecked + { + sgnscl = (uint)bits[3]; + _data1 = (uint)bits[0]; + _data2 = (uint)bits[1]; + _data3 = (uint)bits[2]; + _data4 = s_uiZero; + } // set the sign bit _bStatus |= ((sgnscl & 0x80000000) == 0x80000000) ? s_bNegative : (byte)0; @@ -523,7 +527,7 @@ namespace System.Data.SqlTypes // set the null bit _bStatus = s_bNotNull; - uint uiValue = (uint)value; + uint uiValue = unchecked((uint)value); // set the sign bit if (value < 0) @@ -548,7 +552,7 @@ namespace System.Data.SqlTypes // set the null bit _bStatus = s_bNotNull; - ulong dwl = (ulong)value; + ulong dwl = unchecked((ulong)value); // set the sign bit if (value < 0) @@ -861,7 +865,11 @@ namespace System.Data.SqlTypes { if (IsNull) throw new SqlNullValueException(); - return new int[4] { (int)_data1, (int)_data2, (int)_data3, (int)_data4 }; + + unchecked + { + return new int[4] { (int)_data1, (int)_data2, (int)_data3, (int)_data4 }; + } } } @@ -1135,7 +1143,10 @@ namespace System.Data.SqlTypes if ((int)_data4 != 0 || _bScale > 28) throw new OverflowException(SQLResource.s_conversionOverflowMessage); - return new decimal((int)_data1, (int)_data2, (int)_data3, !IsPositive, _bScale); + unchecked + { + return new decimal((int)_data1, (int)_data2, (int)_data3, !IsPositive, _bScale); + } } // Implicit conversion from Decimal to SqlDecimal @@ -1272,7 +1283,7 @@ namespace System.Data.SqlTypes if (iulData < culOp2) dwlAccum += rglData2[iulData]; - rglData1[iulData] = (uint)dwlAccum; // equiv to mod x_lInt32Base + rglData1[iulData] = unchecked((uint)dwlAccum); // equiv to mod x_lInt32Base dwlAccum >>= 32; // equiv to div x_lInt32Base } @@ -1317,7 +1328,7 @@ namespace System.Data.SqlTypes if (iulData < culOp2) dwlAccum -= rglData2[iulData]; - rglData1[iulData] = (uint)dwlAccum; // equiv to mod BaseUI4 + rglData1[iulData] = unchecked((uint)dwlAccum); // equiv to mod BaseUI4 if (rglData1[iulData] != 0) iulLastNonZero = iulData; dwlAccum >>= 32; // equiv to /= BaseUI4 @@ -1474,7 +1485,7 @@ namespace System.Data.SqlTypes dwlNextAccum = 0; // Update result and accum - rgulRes[idRes++] = (uint)(dwlAccum);// & x_ulInt32BaseForMod); // equiv to mod x_lInt32Base + rgulRes[idRes++] = unchecked((uint)dwlAccum);// & x_ulInt32BaseForMod); // equiv to mod x_lInt32Base dwlAccum = (dwlAccum >> 32) + dwlNextAccum; // equiv to div BaseUI4 + dwlNAccum // dwlNextAccum can't overflow next iteration @@ -2137,8 +2148,8 @@ namespace System.Data.SqlTypes dwlNextAccum = s_ulInt32Base; // how much to add to dwlAccum after div x_dwlBaseUI4 else dwlNextAccum = 0; - rguiData[iData] = (uint)dwlAccum; // equivalent to mod x_dwlBaseUI4 - dwlAccum = (dwlAccum >> 32) + dwlNextAccum; // equivalent to div x_dwlBaseUI4 + rguiData[iData] = unchecked((uint)dwlAccum); // equivalent to mod x_dwlBaseUI4 + dwlAccum = (dwlAccum >> 32) + dwlNextAccum; // equivalent to div x_dwlBaseUI4 } // If any carry, diff --git a/src/System.Data.Common/src/System/Data/SQLTypes/SQLInt16.cs b/src/System.Data.Common/src/System/Data/SQLTypes/SQLInt16.cs index 813fc41232..780bc216f1 100644 --- a/src/System.Data.Common/src/System/Data/SQLTypes/SQLInt16.cs +++ b/src/System.Data.Common/src/System/Data/SQLTypes/SQLInt16.cs @@ -173,7 +173,7 @@ namespace System.Data.SqlTypes public static SqlInt16 operator |(SqlInt16 x, SqlInt16 y) { - return (x.IsNull || y.IsNull) ? Null : new SqlInt16((short)((ushort)x._value | (ushort)y._value)); + return (x.IsNull || y.IsNull) ? Null : new SqlInt16(unchecked((short)((ushort)x._value | (ushort)y._value))); } public static SqlInt16 operator ^(SqlInt16 x, SqlInt16 y) diff --git a/src/System.Data.Common/src/System/Data/SQLTypes/SQLMoney.cs b/src/System.Data.Common/src/System/Data/SQLTypes/SQLMoney.cs index 60244ab530..0f28570582 100644 --- a/src/System.Data.Common/src/System/Data/SQLTypes/SQLMoney.cs +++ b/src/System.Data.Common/src/System/Data/SQLTypes/SQLMoney.cs @@ -137,7 +137,7 @@ namespace System.Data.SqlTypes if (_value < 0) { fNegative = true; - value = -_value; + value = unchecked(-_value); } return new decimal(unchecked((int)value), unchecked((int)(value >> 32)), 0, fNegative, (byte)s_iMoneyScale); diff --git a/src/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs b/src/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs index f391fc505e..3a04511b18 100644 --- a/src/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs +++ b/src/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs @@ -904,13 +904,13 @@ namespace System.Diagnostics bufferSize, out requiredSize); - if ((uint)status == Interop.NtDll.STATUS_INFO_LENGTH_MISMATCH) + if (unchecked((uint)status) == Interop.NtDll.STATUS_INFO_LENGTH_MISMATCH) { if (bufferHandle.IsAllocated) bufferHandle.Free(); buffer = null; bufferSize = GetNewBufferSize(bufferSize, requiredSize); } - } while ((uint)status == Interop.NtDll.STATUS_INFO_LENGTH_MISMATCH); + } while (unchecked((uint)status) == Interop.NtDll.STATUS_INFO_LENGTH_MISMATCH); if (status < 0) { // see definition of NT_SUCCESS(Status) in SDK diff --git a/src/System.Drawing.Primitives/src/System/Drawing/Point.cs b/src/System.Drawing.Primitives/src/System/Drawing/Point.cs index eb1de2208d..28b5a84c95 100644 --- a/src/System.Drawing.Primitives/src/System/Drawing/Point.cs +++ b/src/System.Drawing.Primitives/src/System/Drawing/Point.cs @@ -134,32 +134,32 @@ namespace System.Drawing /// Translates a <see cref='System.Drawing.Point'/> by a given <see cref='System.Drawing.Size'/> . /// </para> /// </summary> - public static Point Add(Point pt, Size sz) => new Point(pt.X + sz.Width, pt.Y + sz.Height); + public static Point Add(Point pt, Size sz) => new Point(unchecked(pt.X + sz.Width), unchecked(pt.Y + sz.Height)); /// <summary> /// <para> /// Translates a <see cref='System.Drawing.Point'/> by the negative of a given <see cref='System.Drawing.Size'/> . /// </para> /// </summary> - public static Point Subtract(Point pt, Size sz) => new Point(pt.X - sz.Width, pt.Y - sz.Height); + public static Point Subtract(Point pt, Size sz) => new Point(unchecked(pt.X - sz.Width), unchecked(pt.Y - sz.Height)); /// <summary> /// Converts a PointF to a Point by performing a ceiling operation on /// all the coordinates. /// </summary> - public static Point Ceiling(PointF value) => new Point((int)Math.Ceiling(value.X), (int)Math.Ceiling(value.Y)); + public static Point Ceiling(PointF value) => new Point(unchecked((int)Math.Ceiling(value.X)), unchecked((int)Math.Ceiling(value.Y))); /// <summary> /// Converts a PointF to a Point by performing a truncate operation on /// all the coordinates. /// </summary> - public static Point Truncate(PointF value) => new Point((int)value.X, (int)value.Y); + public static Point Truncate(PointF value) => new Point(unchecked((int)value.X), unchecked((int)value.Y)); /// <summary> /// Converts a PointF to a Point by performing a round operation on /// all the coordinates. /// </summary> - public static Point Round(PointF value) => new Point((int)Math.Round(value.X), (int)Math.Round(value.Y)); + public static Point Round(PointF value) => new Point(unchecked((int)Math.Round(value.X)), unchecked((int)Math.Round(value.Y))); /// <summary> /// <para> @@ -183,8 +183,11 @@ namespace System.Drawing /// </summary> public void Offset(int dx, int dy) { - X += dx; - Y += dy; + unchecked + { + X += dx; + Y += dy; + } } /// <summary> @@ -201,8 +204,8 @@ namespace System.Drawing /// </summary> public override string ToString() => "{X=" + X.ToString() + ",Y=" + Y.ToString() + "}"; - private static short HighInt16(int n) => (short)((n >> 16) & 0xffff); + private static short HighInt16(int n) => unchecked((short)((n >> 16) & 0xffff)); - private static short LowInt16(int n) => (short)(n & 0xffff); + private static short LowInt16(int n) => unchecked((short)(n & 0xffff)); } } diff --git a/src/System.Drawing.Primitives/src/System/Drawing/Rectangle.cs b/src/System.Drawing.Primitives/src/System/Drawing/Rectangle.cs index 8b3327d748..296796e14c 100644 --- a/src/System.Drawing.Primitives/src/System/Drawing/Rectangle.cs +++ b/src/System.Drawing.Primitives/src/System/Drawing/Rectangle.cs @@ -55,7 +55,7 @@ namespace System.Drawing /// the specified location and size. /// </summary> public static Rectangle FromLTRB(int left, int top, int right, int bottom) => - new Rectangle(left, top, right - left, bottom - top); + new Rectangle(left, top, unchecked(right - left), unchecked(bottom - top)); /// <summary> /// <para> @@ -148,7 +148,7 @@ namespace System.Drawing /// rectangular region defined by this <see cref='System.Drawing.Rectangle'/>. /// </para> /// </summary> - public int Right => X + Width; + public int Right => unchecked(X + Width); /// <summary> /// <para> @@ -156,7 +156,7 @@ namespace System.Drawing /// rectangular region defined by this <see cref='System.Drawing.Rectangle'/>. /// </para> /// </summary> - public int Bottom => Y + Height; + public int Bottom => unchecked(Y + Height); /// <summary> /// <para> @@ -197,34 +197,49 @@ namespace System.Drawing /// Converts a RectangleF to a Rectangle by performing a ceiling operation on /// all the coordinates. /// </summary> - public static Rectangle Ceiling(RectangleF value) => - new Rectangle( - (int)Math.Ceiling(value.X), - (int)Math.Ceiling(value.Y), - (int)Math.Ceiling(value.Width), - (int)Math.Ceiling(value.Height)); + public static Rectangle Ceiling(RectangleF value) + { + unchecked + { + return new Rectangle( + (int)Math.Ceiling(value.X), + (int)Math.Ceiling(value.Y), + (int)Math.Ceiling(value.Width), + (int)Math.Ceiling(value.Height)); + } + } /// <summary> /// Converts a RectangleF to a Rectangle by performing a truncate operation on /// all the coordinates. /// </summary> - public static Rectangle Truncate(RectangleF value) => - new Rectangle( - (int)value.X, - (int)value.Y, - (int)value.Width, - (int)value.Height); + public static Rectangle Truncate(RectangleF value) + { + unchecked + { + return new Rectangle( + (int)value.X, + (int)value.Y, + (int)value.Width, + (int)value.Height); + } + } /// <summary> /// Converts a RectangleF to a Rectangle by performing a round operation on /// all the coordinates. /// </summary> - public static Rectangle Round(RectangleF value) => - new Rectangle( - (int)Math.Round(value.X), - (int)Math.Round(value.Y), - (int)Math.Round(value.Width), - (int)Math.Round(value.Height)); + public static Rectangle Round(RectangleF value) + { + unchecked + { + return new Rectangle( + (int)Math.Round(value.X), + (int)Math.Round(value.Y), + (int)Math.Round(value.Width), + (int)Math.Round(value.Height)); + } + } /// <summary> /// <para> @@ -267,10 +282,14 @@ namespace System.Drawing /// </summary> public void Inflate(int width, int height) { - X -= width; - Y -= height; - Width += 2 * width; - Height += 2 * height; + unchecked + { + X -= width; + Y -= height; + + Width += 2 * width; + Height += 2 * height; + } } /// <summary> @@ -359,8 +378,11 @@ namespace System.Drawing /// </summary> public void Offset(int x, int y) { - X += x; - Y += y; + unchecked + { + X += x; + Y += y; + } } /// <summary> diff --git a/src/System.Drawing.Primitives/src/System/Drawing/Size.cs b/src/System.Drawing.Primitives/src/System/Drawing/Size.cs index 22a8a0a5f4..267a801f11 100644 --- a/src/System.Drawing.Primitives/src/System/Drawing/Size.cs +++ b/src/System.Drawing.Primitives/src/System/Drawing/Size.cs @@ -132,32 +132,36 @@ namespace System.Drawing /// Performs vector addition of two <see cref='System.Drawing.Size'/> objects. /// </para> /// </summary> - public static Size Add(Size sz1, Size sz2) => new Size(sz1.Width + sz2.Width, sz1.Height + sz2.Height); + public static Size Add(Size sz1, Size sz2) => + new Size(unchecked(sz1.Width + sz2.Width), unchecked(sz1.Height + sz2.Height)); /// <summary> /// Converts a SizeF to a Size by performing a ceiling operation on /// all the coordinates. /// </summary> - public static Size Ceiling(SizeF value) => new Size((int)Math.Ceiling(value.Width), (int)Math.Ceiling(value.Height)); + public static Size Ceiling(SizeF value) => + new Size(unchecked((int)Math.Ceiling(value.Width)), unchecked((int)Math.Ceiling(value.Height))); /// <summary> /// <para> /// Contracts a <see cref='System.Drawing.Size'/> by another <see cref='System.Drawing.Size'/> . /// </para> /// </summary> - public static Size Subtract(Size sz1, Size sz2) => new Size(sz1.Width - sz2.Width, sz1.Height - sz2.Height); + public static Size Subtract(Size sz1, Size sz2) => + new Size(unchecked(sz1.Width - sz2.Width), unchecked(sz1.Height - sz2.Height)); /// <summary> /// Converts a SizeF to a Size by performing a truncate operation on /// all the coordinates. /// </summary> - public static Size Truncate(SizeF value) => new Size((int)value.Width, (int)value.Height); + public static Size Truncate(SizeF value) => new Size(unchecked((int)value.Width), unchecked((int)value.Height)); /// <summary> /// Converts a SizeF to a Size by performing a round operation on /// all the coordinates. /// </summary> - public static Size Round(SizeF value) => new Size((int)Math.Round(value.Width), (int)Math.Round(value.Height)); + public static Size Round(SizeF value) => + new Size(unchecked((int)Math.Round(value.Width)), unchecked((int)Math.Round(value.Height))); /// <summary> /// <para> diff --git a/src/System.Drawing.Primitives/tests/PointTests.cs b/src/System.Drawing.Primitives/tests/PointTests.cs index 0f38a0ca64..520cd7c491 100644 --- a/src/System.Drawing.Primitives/tests/PointTests.cs +++ b/src/System.Drawing.Primitives/tests/PointTests.cs @@ -35,7 +35,7 @@ namespace System.Drawing.PrimitivesTests public void SingleIntConstructorTest(int x) { Point p1 = new Point(x); - Point p2 = new Point((short)(x & 0xFFFF), (short)((x >> 16) & 0xFFFF)); + Point p2 = new Point(unchecked((short)(x & 0xFFFF)), unchecked((short)((x >> 16) & 0xFFFF))); Assert.Equal(p1, p2); } @@ -98,11 +98,14 @@ namespace System.Drawing.PrimitivesTests [InlineData(0, 0)] public void ArithmeticTest(int x, int y) { - Point p = new Point(x, y); + Point addExpected, subExpected, p = new Point(x, y); Size s = new Size(y, x); - Point addExpected = new Point(x + y, y + x); - Point subExpected = new Point(x - y, y - x); + unchecked + { + addExpected = new Point(x + y, y + x); + subExpected = new Point(x - y, y - x); + } Assert.Equal(addExpected, p + s); Assert.Equal(subExpected, p - s); @@ -118,9 +121,14 @@ namespace System.Drawing.PrimitivesTests public void PointFMathematicalTest(float x, float y) { PointF pf = new PointF(x, y); - Point pCeiling = new Point((int)Math.Ceiling(x), (int)Math.Ceiling(y)); - Point pTruncate = new Point((int)x, (int)y); - Point pRound = new Point((int)Math.Round(x), (int)Math.Round(y)); + Point pCeiling, pTruncate, pRound; + + unchecked + { + pCeiling = new Point((int)Math.Ceiling(x), (int)Math.Ceiling(y)); + pTruncate = new Point((int)x, (int)y); + pRound = new Point((int)Math.Round(x), (int)Math.Round(y)); + } Assert.Equal(pCeiling, Point.Ceiling(pf)); Assert.Equal(pRound, Point.Round(pf)); @@ -139,7 +147,7 @@ namespace System.Drawing.PrimitivesTests p1.Offset(p2); - Assert.Equal(p2.X + p2.Y, p1.X); + Assert.Equal(unchecked(p2.X + p2.Y), p1.X); Assert.Equal(p1.X, p1.Y); p2.Offset(x, y); diff --git a/src/System.Drawing.Primitives/tests/RectangleTests.cs b/src/System.Drawing.Primitives/tests/RectangleTests.cs index b5bf6a0805..c04cdab224 100644 --- a/src/System.Drawing.Primitives/tests/RectangleTests.cs +++ b/src/System.Drawing.Primitives/tests/RectangleTests.cs @@ -35,7 +35,7 @@ namespace System.Drawing.PrimitivesTest [InlineData(0, int.MinValue, 0, int.MaxValue)] public void FromLTRBTest(int left, int top, int right, int bottom) { - Rectangle rect1 = new Rectangle(left, top, right - left, bottom - top); + Rectangle rect1 = new Rectangle(left, top, unchecked(right - left), unchecked(bottom - top)); Rectangle rect2 = Rectangle.FromLTRB(left, top, right, bottom); Assert.Equal(rect1, rect2); @@ -77,8 +77,8 @@ namespace System.Drawing.PrimitivesTest Assert.Equal(height, rect.Height); Assert.Equal(x, rect.Left); Assert.Equal(y, rect.Top); - Assert.Equal(x + width, rect.Right); - Assert.Equal(y + height, rect.Bottom); + Assert.Equal(unchecked(x + width), rect.Right); + Assert.Equal(unchecked(y + height), rect.Bottom); Point p = new Point(width, height); Size s = new Size(x, y); @@ -94,8 +94,8 @@ namespace System.Drawing.PrimitivesTest Assert.Equal(y, rect.Height); Assert.Equal(width, rect.Left); Assert.Equal(height, rect.Top); - Assert.Equal(x + width, rect.Right); - Assert.Equal(y + height, rect.Bottom); + Assert.Equal(unchecked(x + width), rect.Right); + Assert.Equal(unchecked(y + height), rect.Bottom); } [Theory] @@ -168,11 +168,16 @@ namespace System.Drawing.PrimitivesTest public void RectangleFConversionTest(float x, float y, float width, float height) { RectangleF rect = new RectangleF(x, y, width, height); - Rectangle rCeiling = new Rectangle((int)Math.Ceiling(x), (int)Math.Ceiling(y), - (int)Math.Ceiling(width), (int)Math.Ceiling(height)); - Rectangle rTruncate = new Rectangle((int)x, (int)y, (int)width, (int)height); - Rectangle rRound = new Rectangle((int)Math.Round(x), (int)Math.Round(y), - (int)Math.Round(width), (int)Math.Round(height)); + Rectangle rCeiling, rTruncate, rRound; + + unchecked + { + rCeiling = new Rectangle((int)Math.Ceiling(x), (int)Math.Ceiling(y), + (int)Math.Ceiling(width), (int)Math.Ceiling(height)); + rTruncate = new Rectangle((int)x, (int)y, (int)width, (int)height); + rRound = new Rectangle((int)Math.Round(x), (int)Math.Round(y), + (int)Math.Round(width), (int)Math.Round(height)); + } Assert.Equal(rCeiling, Rectangle.Ceiling(rect)); Assert.Equal(rTruncate, Rectangle.Truncate(rect)); @@ -184,7 +189,7 @@ namespace System.Drawing.PrimitivesTest [InlineData(0, int.MinValue, int.MaxValue, 0)] public void ContainsTest(int x, int y, int width, int height) { - Rectangle rect = new Rectangle(2 * x - width, 2 * y - height, width, height); + Rectangle rect = new Rectangle(unchecked(2 * x - width), unchecked(2 * y - height), width, height); Point p = new Point(x, y); Rectangle r = new Rectangle(x, y, width / 2, height / 2); @@ -199,8 +204,11 @@ namespace System.Drawing.PrimitivesTest [InlineData(0, int.MinValue, int.MaxValue, 0)] public void InflateTest(int x, int y, int width, int height) { - Rectangle rect = new Rectangle(x, y, width, height); - Rectangle inflatedRect = new Rectangle(x - width, y - height, width + 2 * width, height + 2 * height); + Rectangle inflatedRect, rect = new Rectangle(x, y, width, height); + unchecked + { + inflatedRect = new Rectangle(x - width, y - height, width + 2 * width, height + 2 * height); + } Assert.Equal(inflatedRect, Rectangle.Inflate(rect, width, height)); @@ -208,7 +216,10 @@ namespace System.Drawing.PrimitivesTest Assert.Equal(inflatedRect, rect); Size s = new Size(x, y); - inflatedRect = new Rectangle(rect.X - x, rect.Y - y, rect.Width + 2 * x, rect.Height + 2 * y); + unchecked + { + inflatedRect = new Rectangle(rect.X - x, rect.Y - y, rect.Width + 2 * x, rect.Height + 2 * y); + } rect.Inflate(s); Assert.Equal(inflatedRect, rect); diff --git a/src/System.Drawing.Primitives/tests/SizeFTests.cs b/src/System.Drawing.Primitives/tests/SizeFTests.cs index 4d5278b249..0caa768e26 100644 --- a/src/System.Drawing.Primitives/tests/SizeFTests.cs +++ b/src/System.Drawing.Primitives/tests/SizeFTests.cs @@ -130,7 +130,7 @@ namespace System.Drawing.PrimitivesTest { SizeF s1 = new SizeF(width, height); PointF p1 = (PointF)s1; - Size s2 = new Size((int)width, (int)height); + Size s2 = new Size(unchecked((int)width), unchecked((int)height)); Assert.Equal(new PointF(width, height), p1); Assert.Equal(p1, s1.ToPointF()); diff --git a/src/System.Drawing.Primitives/tests/SizeTests.cs b/src/System.Drawing.Primitives/tests/SizeTests.cs index 8eeefaf873..84460e1f2a 100644 --- a/src/System.Drawing.Primitives/tests/SizeTests.cs +++ b/src/System.Drawing.Primitives/tests/SizeTests.cs @@ -94,9 +94,13 @@ namespace System.Drawing.PrimitivesTests { Size sz1 = new Size(width, height); Size sz2 = new Size(height, width); + Size addExpected, subExpected; - Size addExpected = new Size(width + height, height + width); - Size subExpected = new Size(width - height, height - width); + unchecked + { + addExpected = new Size(width + height, height + width); + subExpected = new Size(width - height, height - width); + } Assert.Equal(addExpected, sz1 + sz2); Assert.Equal(subExpected, sz1 - sz2); @@ -112,9 +116,14 @@ namespace System.Drawing.PrimitivesTests public void PointFMathematicalTest(float width, float height) { SizeF szF = new SizeF(width, height); - Size pCeiling = new Size((int)Math.Ceiling(width), (int)Math.Ceiling(height)); - Size pTruncate = new Size((int)width, (int)height); - Size pRound = new Size((int)Math.Round(width), (int)Math.Round(height)); + Size pCeiling, pTruncate, pRound; + + unchecked + { + pCeiling = new Size((int)Math.Ceiling(width), (int)Math.Ceiling(height)); + pTruncate = new Size((int)width, (int)height); + pRound = new Size((int)Math.Round(width), (int)Math.Round(height)); + } Assert.Equal(pCeiling, Size.Ceiling(szF)); Assert.Equal(pRound, Size.Round(szF)); @@ -129,7 +138,7 @@ namespace System.Drawing.PrimitivesTests public void EqualityTest(int width, int height) { Size p1 = new Size(width, height); - Size p2 = new Size(width - 1, height - 1); + Size p2 = new Size(unchecked(width - 1), unchecked(height - 1)); Size p3 = new Size(width, height); Assert.True(p1 == p3); diff --git a/src/System.IO.Compression/src/System/IO/Compression/Crc32Helper.cs b/src/System.IO.Compression/src/System/IO/Compression/Crc32Helper.cs index 47fdac1b38..0e0ca4bcb3 100644 --- a/src/System.IO.Compression/src/System/IO/Compression/Crc32Helper.cs +++ b/src/System.IO.Compression/src/System/IO/Compression/Crc32Helper.cs @@ -493,7 +493,7 @@ namespace System.IO.Compression for (int i = 0; i < runningLength / 8; i++) { - crc32 ^= (uint)(buffer[offset] | buffer[offset + 1] << 8 | buffer[offset + 2] << 16 | buffer[offset + 3] << 24); + crc32 ^= unchecked((uint)(buffer[offset] | buffer[offset + 1] << 8 | buffer[offset + 2] << 16 | buffer[offset + 3] << 24)); offset += 4; term1 = s_crcTable_7[crc32 & 0x000000FF] ^ s_crcTable_6[(crc32 >> 8) & 0x000000FF]; @@ -503,7 +503,7 @@ namespace System.IO.Compression s_crcTable_4[(term2 >> 8) & 0x000000FF]; - term3 = (uint)(buffer[offset] | buffer[offset + 1] << 8 | buffer[offset + 2] << 16 | buffer[offset + 3] << 24); + term3 = unchecked((uint)(buffer[offset] | buffer[offset + 1] << 8 | buffer[offset + 2] << 16 | buffer[offset + 3] << 24)); offset += 4; term1 = s_crcTable_3[term3 & 0x000000FF] ^ s_crcTable_2[(term3 >> 8) & 0x000000FF]; diff --git a/src/System.IO.MemoryMappedFiles/tests/MemoryMappedFile.CrossProcess.cs b/src/System.IO.MemoryMappedFiles/tests/MemoryMappedFile.CrossProcess.cs index db8e827b2d..447057c259 100644 --- a/src/System.IO.MemoryMappedFiles/tests/MemoryMappedFile.CrossProcess.cs +++ b/src/System.IO.MemoryMappedFiles/tests/MemoryMappedFile.CrossProcess.cs @@ -22,7 +22,7 @@ namespace System.IO.MemoryMappedFiles.Tests long capacity = acc.Capacity; for (int i = 0; i < capacity; i++) { - acc.Write(i, (byte)i); + acc.Write(i, unchecked((byte)i)); } acc.Flush(); diff --git a/src/System.Linq.Expressions/src/System/Linq/Expressions/Compiler/ILGen.cs b/src/System.Linq.Expressions/src/System/Linq/Expressions/Compiler/ILGen.cs index feb6aaf475..effbd8f1eb 100644 --- a/src/System.Linq.Expressions/src/System/Linq/Expressions/Compiler/ILGen.cs +++ b/src/System.Linq.Expressions/src/System/Linq/Expressions/Compiler/ILGen.cs @@ -431,7 +431,7 @@ namespace System.Linq.Expressions.Compiler internal static void EmitUInt(this ILGenerator il, uint value) { - il.EmitInt((int)value); + il.EmitInt(unchecked((int)value)); il.Emit(OpCodes.Conv_U4); } @@ -450,7 +450,7 @@ namespace System.Linq.Expressions.Compiler internal static void EmitULong(this ILGenerator il, ulong value) { - il.Emit(OpCodes.Ldc_I8, (long)value); + il.Emit(OpCodes.Ldc_I8, unchecked((long)value)); il.Emit(OpCodes.Conv_U8); } @@ -1127,7 +1127,7 @@ namespace System.Linq.Expressions.Compiler il.EmitInt(bits[1]); il.EmitInt(bits[2]); il.EmitBoolean((bits[3] & 0x80000000) != 0); - il.EmitByte((byte)(bits[3] >> 16)); + il.EmitByte(unchecked((byte)(bits[3] >> 16))); il.EmitNew(Decimal_Ctor_Int32_Int32_Int32_Bool_Byte); } diff --git a/src/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/DivInstruction.cs b/src/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/DivInstruction.cs index 3aaaaaba79..f90587910f 100644 --- a/src/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/DivInstruction.cs +++ b/src/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/DivInstruction.cs @@ -30,7 +30,7 @@ namespace System.Linq.Expressions.Interpreter } else { - frame.Data[frame.StackIndex - 2] = (short)((short)l / (short)r); + frame.Data[frame.StackIndex - 2] = unchecked((short)((short)l / (short)r)); } frame.StackIndex--; return 1; diff --git a/src/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LeftShiftInstruction.cs b/src/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LeftShiftInstruction.cs index 24b4c25196..96d9451869 100644 --- a/src/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LeftShiftInstruction.cs +++ b/src/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LeftShiftInstruction.cs @@ -29,7 +29,7 @@ namespace System.Linq.Expressions.Interpreter } else { - frame.Push((sbyte)((sbyte)value << (int)shift)); + frame.Push(unchecked((sbyte)((sbyte)value << (int)shift))); } return 1; } @@ -47,7 +47,7 @@ namespace System.Linq.Expressions.Interpreter } else { - frame.Push((short)((short)value << (int)shift)); + frame.Push(unchecked((short)((short)value << (int)shift))); } return 1; } @@ -101,7 +101,7 @@ namespace System.Linq.Expressions.Interpreter } else { - frame.Push((byte)((byte)value << (int)shift)); + frame.Push(unchecked((byte)((byte)value << (int)shift))); } return 1; } @@ -119,7 +119,7 @@ namespace System.Linq.Expressions.Interpreter } else { - frame.Push((ushort)((ushort)value << (int)shift)); + frame.Push(unchecked((ushort)((ushort)value << (int)shift))); } return 1; } diff --git a/src/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/NotInstruction.cs b/src/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/NotInstruction.cs index e2fc2037d6..cd61d79e88 100644 --- a/src/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/NotInstruction.cs +++ b/src/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/NotInstruction.cs @@ -129,7 +129,7 @@ namespace System.Linq.Expressions.Interpreter } else { - frame.Push((ushort)(~(ushort)value)); + frame.Push(unchecked((ushort)(~(ushort)value))); } return 1; } @@ -146,7 +146,7 @@ namespace System.Linq.Expressions.Interpreter } else { - frame.Push((byte)(~(byte)value)); + frame.Push(unchecked((byte)(~(byte)value))); } return 1; } diff --git a/src/System.Private.Uri/src/System/Uri.cs b/src/System.Private.Uri/src/System/Uri.cs index d59e26bffc..d4015681b3 100644 --- a/src/System.Private.Uri/src/System/Uri.cs +++ b/src/System.Private.Uri/src/System/Uri.cs @@ -2690,9 +2690,9 @@ namespace System private string GetEscapedParts(UriComponents uriParts) { // Which Uri parts are not escaped canonically ? - // Notice that public UriPart and private Flags must me in Sync so below code can work + // Notice that public UriPart and private Flags must be in Sync so below code can work // - ushort nonCanonical = (ushort)(((ushort)_flags & ((ushort)Flags.CannotDisplayCanonical << 7)) >> 6); + ushort nonCanonical = unchecked((ushort)(((ushort)_flags & ((ushort)Flags.CannotDisplayCanonical << 7)) >> 6)); if (InFact(Flags.SchemeNotCanonical)) { nonCanonical |= (ushort)Flags.SchemeNotCanonical; @@ -2712,7 +2712,7 @@ namespace System } } - if (((ushort)uriParts & nonCanonical) == 0) + if ((unchecked((ushort)uriParts) & nonCanonical) == 0) { string ret = GetUriPartsFromUserString(uriParts); if ((object)ret != null) @@ -2745,7 +2745,7 @@ namespace System } } - if (((ushort)uriParts & nonCanonical) == 0) + if ((unchecked((ushort)uriParts) & nonCanonical) == 0) { string ret = GetUriPartsFromUserString(uriParts); if ((object)ret != null) diff --git a/src/System.Private.Xml/src/System/Xml/Schema/XsdDateTime.cs b/src/System.Private.Xml/src/System/Xml/Schema/XsdDateTime.cs index 2ccb65ea7c..b87cd3b2b2 100644 --- a/src/System.Private.Xml/src/System/Xml/Schema/XsdDateTime.cs +++ b/src/System.Private.Xml/src/System/Xml/Schema/XsdDateTime.cs @@ -987,7 +987,7 @@ namespace System.Xml.Schema while (++start < _length) { int d = _text[start] - '0'; - if (9u < (uint)d) + if (9u < unchecked((uint)d)) { // d < 0 || 9 < d break; } diff --git a/src/System.Runtime.Extensions/src/System/BitConverter.cs b/src/System.Runtime.Extensions/src/System/BitConverter.cs index 8b03ffe081..9dd8a4135a 100644 --- a/src/System.Runtime.Extensions/src/System/BitConverter.cs +++ b/src/System.Runtime.Extensions/src/System/BitConverter.cs @@ -236,13 +236,13 @@ namespace System { int i1 = (*pbyte) | (*(pbyte + 1) << 8) | (*(pbyte + 2) << 16) | (*(pbyte + 3) << 24); int i2 = (*(pbyte + 4)) | (*(pbyte + 5) << 8) | (*(pbyte + 6) << 16) | (*(pbyte + 7) << 24); - return (uint)i1 | ((long)i2 << 32); + return unchecked((uint)i1) | ((long)i2 << 32); } else { int i1 = (*pbyte << 24) | (*(pbyte + 1) << 16) | (*(pbyte + 2) << 8) | (*(pbyte + 3)); int i2 = (*(pbyte + 4) << 24) | (*(pbyte + 5) << 16) | (*(pbyte + 6) << 8) | (*(pbyte + 7)); - return (uint)i2 | ((long)i1 << 32); + return unchecked((uint)i2) | ((long)i1 << 32); } } } |