Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Int128 checked-convert to signed IntX #100342

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Int128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ public static explicit operator checked short(Int128 value)
if (~value._upper == 0)
{
long lower = (long)value._lower;
if (lower >= 0)
{
ThrowHelper.ThrowOverflowException();
}
return checked((short)lower);
}

Expand All @@ -289,6 +293,10 @@ public static explicit operator checked int(Int128 value)
if (~value._upper == 0)
{
long lower = (long)value._lower;
if (lower >= 0)
{
ThrowHelper.ThrowOverflowException();
}
return checked((int)lower);
}

Expand All @@ -313,6 +321,10 @@ public static explicit operator checked long(Int128 value)
if (~value._upper == 0)
{
long lower = (long)value._lower;
if (lower >= 0)
{
ThrowHelper.ThrowOverflowException();
}
return lower;
}

Expand All @@ -337,6 +349,10 @@ public static explicit operator checked nint(Int128 value)
if (~value._upper == 0)
{
long lower = (long)value._lower;
if (lower >= 0)
{
ThrowHelper.ThrowOverflowException();
}
return checked((nint)lower);
}

Expand All @@ -363,6 +379,10 @@ public static explicit operator checked sbyte(Int128 value)
if (~value._upper == 0)
{
long lower = (long)value._lower;
if (lower >= 0)
{
ThrowHelper.ThrowOverflowException();
}
return checked((sbyte)lower);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,36 @@ public static void EqualsTest(Int128 i1, object obj, bool expected)
Assert.Equal(expected, i1.Equals(obj));
}

[Fact]
public static void CheckedConvertToInt64Overflow()
{
Assert.Throws<OverflowException>(() => checked((long)new Int128(ulong.MaxValue, 42)));
}

[Fact]
public static void CheckedConvertToInt32Overflow()
{
Assert.Throws<OverflowException>(() => checked((int)new Int128(ulong.MaxValue, 42)));
}

[Fact]
public static void CheckedConvertToInt16Overflow()
{
Assert.Throws<OverflowException>(() => checked((short)new Int128(ulong.MaxValue, 42)));
}

[Fact]
public static void CheckedConvertToSByteOverflow()
{
Assert.Throws<OverflowException>(() => checked((sbyte)new Int128(ulong.MaxValue, 42)));
}

[Fact]
public static void CheckedConvertToIntPtrOverflow()
{
Assert.Throws<OverflowException>(() => checked((nint)new Int128(ulong.MaxValue, 42)));
}

public static IEnumerable<object[]> ToString_TestData()
{
foreach (NumberFormatInfo defaultFormat in new[] { null, NumberFormatInfo.CurrentInfo })
Expand Down
Loading