From b651aecb5bc3abeb841b214b9fdc8d8605d9b4f2 Mon Sep 17 00:00:00 2001 From: Emmanuel ANDRE <2341261+manandre@users.noreply.github.com> Date: Sat, 11 Nov 2023 22:42:44 +0100 Subject: [PATCH 1/2] UInt128: Throw DivideByZeroException when divisor is 0 --- .../System.Private.CoreLib/src/System/UInt128.cs | 5 +++++ .../System/Int128Tests.GenericMath.cs | 4 ++++ .../System.Runtime.Tests/System/Int16Tests.GenericMath.cs | 4 ++++ .../System.Runtime.Tests/System/Int32Tests.GenericMath.cs | 4 ++++ .../System.Runtime.Tests/System/Int64Tests.GenericMath.cs | 4 ++++ .../System/IntPtrTests.GenericMath.cs | 8 ++++++++ .../System/UInt128Tests.GenericMath.cs | 4 ++++ .../System/UInt16Tests.GenericMath.cs | 4 ++++ .../System/UInt32Tests.GenericMath.cs | 4 ++++ .../System/UInt64Tests.GenericMath.cs | 4 ++++ .../System/UIntPtrTests.GenericMath.cs | 8 ++++++++ 11 files changed, 53 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt128.cs b/src/libraries/System.Private.CoreLib/src/System/UInt128.cs index 7159db5439bde..ca06719da03fb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt128.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt128.cs @@ -1091,6 +1091,11 @@ public static UInt128 Log2(UInt128 value) /// public static UInt128 operator /(UInt128 left, UInt128 right) { + if ((right._lower == 0) && (right._upper == 0)) + { + throw new DivideByZeroException(); + } + if ((right._upper == 0) && (left._upper == 0)) { // left and right are both uint64 diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int128Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int128Tests.GenericMath.cs index 4d3dc2bd5dae8..10c49230717d4 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int128Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int128Tests.GenericMath.cs @@ -1082,7 +1082,9 @@ public static void op_DivisionTest() Assert.Equal(new Int128(0xC000_0000_0000_0000, 0x0000_0000_0000_0000), DivisionOperatorsHelper.op_Division(MinValue, 2)); Assert.Equal(Zero, DivisionOperatorsHelper.op_Division(NegativeOne, 2)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division(Zero, 0)); Assert.Throws(() => DivisionOperatorsHelper.op_Division(One, 0)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division(MaxValue, 0)); } [Fact] @@ -1094,7 +1096,9 @@ public static void op_CheckedDivisionTest() Assert.Equal(new Int128(0xC000_0000_0000_0000, 0x0000_0000_0000_0000), DivisionOperatorsHelper.op_CheckedDivision(MinValue, 2)); Assert.Equal(Zero, DivisionOperatorsHelper.op_CheckedDivision(NegativeOne, 2)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(Zero, 0)); Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(One, 0)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(MaxValue, 0)); } // diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int16Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int16Tests.GenericMath.cs index 130aa6ac41414..5e1a78029f01e 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int16Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int16Tests.GenericMath.cs @@ -1031,7 +1031,9 @@ public static void op_DivisionTest() Assert.Equal(unchecked((short)0xC000), DivisionOperatorsHelper.op_Division(unchecked((short)0x8000), (short)2)); Assert.Equal((short)0x0000, DivisionOperatorsHelper.op_Division(unchecked((short)0xFFFF), (short)2)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division((short)0x0000, (short)0)); Assert.Throws(() => DivisionOperatorsHelper.op_Division((short)0x0001, (short)0)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division(unchecked((short)0xFFFF), (short)0)); } [Fact] @@ -1043,7 +1045,9 @@ public static void op_CheckedDivisionTest() Assert.Equal(unchecked((short)0xC000), DivisionOperatorsHelper.op_CheckedDivision(unchecked((short)0x8000), (short)2)); Assert.Equal((short)0x0000, DivisionOperatorsHelper.op_CheckedDivision(unchecked((short)0xFFFF), (short)2)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((short)0x0000, (short)0)); Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((short)0x0001, (short)0)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(unchecked((short)0xFFFF), (short)0)); } // diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int32Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int32Tests.GenericMath.cs index 5ef1e2c8d6afc..e915e1d58df63 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int32Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int32Tests.GenericMath.cs @@ -1031,7 +1031,9 @@ public static void op_DivisionTest() Assert.Equal(unchecked((int)0xC0000000), DivisionOperatorsHelper.op_Division(unchecked((int)0x80000000), 2)); Assert.Equal((int)0x00000000, DivisionOperatorsHelper.op_Division(unchecked((int)0xFFFFFFFF), 2)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division((int)0x00000000, 0)); Assert.Throws(() => DivisionOperatorsHelper.op_Division((int)0x00000001, 0)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division(unchecked((int)0xFFFFFFFF), 0)); } [Fact] @@ -1043,7 +1045,9 @@ public static void op_CheckedDivisionTest() Assert.Equal(unchecked((int)0xC0000000), DivisionOperatorsHelper.op_CheckedDivision(unchecked((int)0x80000000), 2)); Assert.Equal((int)0x00000000, DivisionOperatorsHelper.op_CheckedDivision(unchecked((int)0xFFFFFFFF), 2)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((int)0x00000000, 0)); Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((int)0x00000001, 0)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(unchecked((int)0xFFFFFFFF), 0)); } // diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int64Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int64Tests.GenericMath.cs index 7a940ebc8827c..aa8d7893f73c4 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int64Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Int64Tests.GenericMath.cs @@ -1031,7 +1031,9 @@ public static void op_DivisionTest() Assert.Equal(unchecked((long)0xC000000000000000), DivisionOperatorsHelper.op_Division(unchecked((long)0x8000000000000000), 2)); Assert.Equal((long)0x0000000000000000, DivisionOperatorsHelper.op_Division(unchecked((long)0xFFFFFFFFFFFFFFFF), 2)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division((long)0x0000000000000000, 0)); Assert.Throws(() => DivisionOperatorsHelper.op_Division((long)0x0000000000000001, 0)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division(unchecked((long)0xFFFFFFFFFFFFFFFF), 0)); } [Fact] @@ -1043,7 +1045,9 @@ public static void op_CheckedDivisionTest() Assert.Equal(unchecked((long)0xC000000000000000), DivisionOperatorsHelper.op_CheckedDivision(unchecked((long)0x8000000000000000), 2)); Assert.Equal((long)0x0000000000000000, DivisionOperatorsHelper.op_CheckedDivision(unchecked((long)0xFFFFFFFFFFFFFFFF), 2)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((long)0x0000000000000000, 0)); Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((long)0x0000000000000001, 0)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(unchecked((long)0xFFFFFFFFFFFFFFFF), 0)); } // diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IntPtrTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IntPtrTests.GenericMath.cs index b544fc4c277c7..4e4d6c59fbf88 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IntPtrTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IntPtrTests.GenericMath.cs @@ -1448,7 +1448,9 @@ public static void op_DivisionTest() Assert.Equal(unchecked((nint)0xC000000000000000), DivisionOperatorsHelper.op_Division(unchecked((nint)0x8000000000000000), (nint)2)); Assert.Equal(unchecked((nint)0x0000000000000000), DivisionOperatorsHelper.op_Division(unchecked((nint)0xFFFFFFFFFFFFFFFF), (nint)2)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division(unchecked((nint)0x0000000000000000), (nint)0)); Assert.Throws(() => DivisionOperatorsHelper.op_Division(unchecked((nint)0x0000000000000001), (nint)0)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division(unchecked((nint)0xFFFFFFFFFFFFFFFF), (nint)0)); } else { @@ -1458,7 +1460,9 @@ public static void op_DivisionTest() Assert.Equal(unchecked((nint)0xC0000000), DivisionOperatorsHelper.op_Division(unchecked((nint)0x80000000), (nint)2)); Assert.Equal((nint)0x00000000, DivisionOperatorsHelper.op_Division(unchecked((nint)0xFFFFFFFF), (nint)2)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division((nint)0x00000000, (nint)0)); Assert.Throws(() => DivisionOperatorsHelper.op_Division((nint)0x00000001, (nint)0)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division(unchecked((nint)0xFFFFFFFF), (nint)0)); } } @@ -1473,7 +1477,9 @@ public static void op_CheckedDivisionTest() Assert.Equal(unchecked((nint)0xC000000000000000), DivisionOperatorsHelper.op_CheckedDivision(unchecked((nint)0x8000000000000000), (nint)2)); Assert.Equal(unchecked((nint)0x0000000000000000), DivisionOperatorsHelper.op_CheckedDivision(unchecked((nint)0xFFFFFFFFFFFFFFFF), (nint)2)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(unchecked((nint)0x0000000000000000), (nint)0)); Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(unchecked((nint)0x0000000000000001), (nint)0)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(unchecked((nint)0xFFFFFFFFFFFFFFFF), (nint)0)); } else { @@ -1483,7 +1489,9 @@ public static void op_CheckedDivisionTest() Assert.Equal(unchecked((nint)0xC0000000), DivisionOperatorsHelper.op_CheckedDivision(unchecked((nint)0x80000000), (nint)2)); Assert.Equal((nint)0x00000000, DivisionOperatorsHelper.op_CheckedDivision(unchecked((nint)0xFFFFFFFF), (nint)2)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((nint)0x00000000, (nint)0)); Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((nint)0x00000001, (nint)0)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(unchecked((nint)0xFFFFFFFF), (nint)0)); } } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt128Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt128Tests.GenericMath.cs index 362ddee44150b..d8366cf6542d7 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt128Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt128Tests.GenericMath.cs @@ -1082,7 +1082,9 @@ public static void op_DivisionTest() Assert.Equal(new UInt128(0x4000_0000_0000_0000, 0x0000_0000_0000_0000), DivisionOperatorsHelper.op_Division(Int128MaxValuePlusOne, 2U)); Assert.Equal(Int128MaxValue, DivisionOperatorsHelper.op_Division(MaxValue, 2U)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division(Zero, 0U)); Assert.Throws(() => DivisionOperatorsHelper.op_Division(One, 0U)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division(MaxValue, 0U)); } [Fact] @@ -1094,7 +1096,9 @@ public static void op_CheckedDivisionTest() Assert.Equal(new UInt128(0x4000_0000_0000_0000, 0x0000_0000_0000_0000), DivisionOperatorsHelper.op_CheckedDivision(Int128MaxValuePlusOne, 2U)); Assert.Equal(Int128MaxValue, DivisionOperatorsHelper.op_CheckedDivision(MaxValue, 2U)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(Zero, 0U)); Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(One, 0U)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(MaxValue, 0U)); } // diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt16Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt16Tests.GenericMath.cs index da3aeff275bb4..7aa8954dbc193 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt16Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt16Tests.GenericMath.cs @@ -1032,7 +1032,9 @@ public static void op_DivisionTest() Assert.Equal((ushort)0x4000, DivisionOperatorsHelper.op_Division((ushort)0x8000, (ushort)2)); Assert.Equal((ushort)0x7FFF, DivisionOperatorsHelper.op_Division((ushort)0xFFFF, (ushort)2)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division((ushort)0x0000, (ushort)0)); Assert.Throws(() => DivisionOperatorsHelper.op_Division((ushort)0x0001, (ushort)0)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division((ushort)0xFFFF, (ushort)0)); } [Fact] @@ -1044,7 +1046,9 @@ public static void op_CheckedDivisionTest() Assert.Equal((ushort)0x4000, DivisionOperatorsHelper.op_CheckedDivision((ushort)0x8000, (ushort)2)); Assert.Equal((ushort)0x7FFF, DivisionOperatorsHelper.op_CheckedDivision((ushort)0xFFFF, (ushort)2)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((ushort)0x0000, (ushort)0)); Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((ushort)0x0001, (ushort)0)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((ushort)0xFFFF, (ushort)0)); } // diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt32Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt32Tests.GenericMath.cs index 3a323bcd61f38..04de00647e55f 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt32Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt32Tests.GenericMath.cs @@ -1033,7 +1033,9 @@ public static void op_DivisionTest() Assert.Equal((uint)0x40000000, DivisionOperatorsHelper.op_Division((uint)0x80000000, 2)); Assert.Equal((uint)0x7FFFFFFF, DivisionOperatorsHelper.op_Division((uint)0xFFFFFFFF, 2)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division((uint)0x00000000, 0)); Assert.Throws(() => DivisionOperatorsHelper.op_Division((uint)0x00000001, 0)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division((uint)0xFFFFFFFF, 0)); } [Fact] @@ -1045,7 +1047,9 @@ public static void op_CheckedDivisionTest() Assert.Equal((uint)0x40000000, DivisionOperatorsHelper.op_CheckedDivision((uint)0x80000000, 2)); Assert.Equal((uint)0x7FFFFFFF, DivisionOperatorsHelper.op_CheckedDivision((uint)0xFFFFFFFF, 2)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((uint)0x00000000, 0)); Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((uint)0x00000001, 0)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((uint)0xFFFFFFFF, 0)); } // diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt64Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt64Tests.GenericMath.cs index 89639038fd4ee..f93a032e68d59 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt64Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UInt64Tests.GenericMath.cs @@ -1032,7 +1032,9 @@ public static void op_DivisionTest() Assert.Equal((ulong)0x4000000000000000, DivisionOperatorsHelper.op_Division((ulong)0x8000000000000000, 2)); Assert.Equal((ulong)0x7FFFFFFFFFFFFFFF, DivisionOperatorsHelper.op_Division((ulong)0xFFFFFFFFFFFFFFFF, 2)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division((ulong)0x0000000000000000, 0)); Assert.Throws(() => DivisionOperatorsHelper.op_Division((ulong)0x0000000000000001, 0)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division((ulong)0xFFFFFFFFFFFFFFFF, 0)); } [Fact] @@ -1044,7 +1046,9 @@ public static void op_CheckedDivisionTest() Assert.Equal((ulong)0x4000000000000000, DivisionOperatorsHelper.op_CheckedDivision((ulong)0x8000000000000000, 2)); Assert.Equal((ulong)0x7FFFFFFFFFFFFFFF, DivisionOperatorsHelper.op_CheckedDivision((ulong)0xFFFFFFFFFFFFFFFF, 2)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((ulong)0x0000000000000000, 0)); Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((ulong)0x0000000000000001, 0)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((ulong)0xFFFFFFFFFFFFFFFF, 0)); } // diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UIntPtrTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UIntPtrTests.GenericMath.cs index 10b1f760d9362..02d4365c107f8 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UIntPtrTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/UIntPtrTests.GenericMath.cs @@ -1427,7 +1427,9 @@ public static void op_DivisionTest() Assert.Equal(unchecked((nuint)0x4000000000000000), DivisionOperatorsHelper.op_Division(unchecked((nuint)0x8000000000000000), (nuint)2)); Assert.Equal(unchecked((nuint)0x7FFFFFFFFFFFFFFF), DivisionOperatorsHelper.op_Division(unchecked((nuint)0xFFFFFFFFFFFFFFFF), (nuint)2)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division(unchecked((nuint)0x0000000000000000), (nuint)0)); Assert.Throws(() => DivisionOperatorsHelper.op_Division(unchecked((nuint)0x0000000000000001), (nuint)0)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division(unchecked((nuint)0xFFFFFFFFFFFFFFFF), (nuint)0)); } else { @@ -1437,7 +1439,9 @@ public static void op_DivisionTest() Assert.Equal((nuint)0x40000000, DivisionOperatorsHelper.op_Division((nuint)0x80000000, (nuint)2)); Assert.Equal((nuint)0x7FFFFFFF, DivisionOperatorsHelper.op_Division((nuint)0xFFFFFFFF, (nuint)2)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division((nuint)0x00000000, (nuint)0)); Assert.Throws(() => DivisionOperatorsHelper.op_Division((nuint)0x00000001, (nuint)0)); + Assert.Throws(() => DivisionOperatorsHelper.op_Division((nuint)0xFFFFFFFF, (nuint)0)); } } @@ -1452,7 +1456,9 @@ public static void op_CheckedDivisionTest() Assert.Equal(unchecked((nuint)0x4000000000000000), DivisionOperatorsHelper.op_CheckedDivision(unchecked((nuint)0x8000000000000000), (nuint)2)); Assert.Equal(unchecked((nuint)0x7FFFFFFFFFFFFFFF), DivisionOperatorsHelper.op_CheckedDivision(unchecked((nuint)0xFFFFFFFFFFFFFFFF), (nuint)2)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(unchecked((nuint)0x0000000000000000), (nuint)0)); Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(unchecked((nuint)0x0000000000000001), (nuint)0)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision(unchecked((nuint)0xFFFFFFFFFFFFFFFF), (nuint)0)); } else { @@ -1462,7 +1468,9 @@ public static void op_CheckedDivisionTest() Assert.Equal((nuint)0x40000000, DivisionOperatorsHelper.op_CheckedDivision((nuint)0x80000000, (nuint)2)); Assert.Equal((nuint)0x7FFFFFFF, DivisionOperatorsHelper.op_CheckedDivision((nuint)0xFFFFFFFF, (nuint)2)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((nuint)0x00000000, (nuint)0)); Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((nuint)0x00000001, (nuint)0)); + Assert.Throws(() => DivisionOperatorsHelper.op_CheckedDivision((nuint)0xFFFFFFFF, (nuint)0)); } } From 4631b7b5c7a06f5f69236869336e3d2faa477471 Mon Sep 17 00:00:00 2001 From: Emmanuel ANDRE <2341261+manandre@users.noreply.github.com> Date: Sun, 12 Nov 2023 15:36:17 +0100 Subject: [PATCH 2/2] Use ThrowHelper and avoid double check --- .../src/System/ThrowHelper.cs | 6 ++++++ .../src/System/UInt128.cs | 17 ++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs b/src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs index 991af446a477f..79f12c11feb40 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs @@ -470,6 +470,12 @@ internal static void ThrowOutOfMemoryException() throw new OutOfMemoryException(); } + [DoesNotReturn] + internal static void ThrowDivideByZeroException() + { + throw new DivideByZeroException(); + } + [DoesNotReturn] internal static void ThrowOutOfMemoryException_StringTooLong() { diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt128.cs b/src/libraries/System.Private.CoreLib/src/System/UInt128.cs index ca06719da03fb..ec195239ca983 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt128.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt128.cs @@ -1091,15 +1091,18 @@ public static UInt128 Log2(UInt128 value) /// public static UInt128 operator /(UInt128 left, UInt128 right) { - if ((right._lower == 0) && (right._upper == 0)) + if (right._upper == 0) { - throw new DivideByZeroException(); - } + if (right._lower == 0) + { + ThrowHelper.ThrowDivideByZeroException(); + } - if ((right._upper == 0) && (left._upper == 0)) - { - // left and right are both uint64 - return left._lower / right._lower; + if (left._upper == 0) + { + // left and right are both uint64 + return left._lower / right._lower; + } } if (right >= left)