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 issue #1164 - arithmetic overflow for unsigned integer mapping #1795

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3788,20 +3788,20 @@ private static void FlexibleConvertBoxedFromHeadOfStack(ILGenerator il, Type fro
switch (Type.GetTypeCode(via ?? to))
{
case TypeCode.Byte:
opCode = OpCodes.Conv_Ovf_I1_Un; break;
opCode = OpCodes.Conv_Ovf_U1_Un; break;
case TypeCode.SByte:
opCode = OpCodes.Conv_Ovf_I1; break;
case TypeCode.UInt16:
opCode = OpCodes.Conv_Ovf_I2_Un; break;
opCode = OpCodes.Conv_Ovf_U2_Un; break;
case TypeCode.Int16:
opCode = OpCodes.Conv_Ovf_I2; break;
case TypeCode.UInt32:
opCode = OpCodes.Conv_Ovf_I4_Un; break;
opCode = OpCodes.Conv_Ovf_U4_Un; break;
case TypeCode.Boolean: // boolean is basically an int, at least at this level
case TypeCode.Int32:
opCode = OpCodes.Conv_Ovf_I4; break;
case TypeCode.UInt64:
opCode = OpCodes.Conv_Ovf_I8_Un; break;
opCode = OpCodes.Conv_Ovf_U8_Un; break;
case TypeCode.Int64:
opCode = OpCodes.Conv_Ovf_I8; break;
case TypeCode.Single:
Expand Down
37 changes: 37 additions & 0 deletions tests/Dapper.Tests/MiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,43 @@ public HazGetOnlyAndCtor(int idProperty, string nameProperty)
IdProperty = idProperty;
NameProperty = nameProperty;
}
}

[Fact]
public void Issue1164_OverflowExceptionForByte()
{
const string sql = "select cast(200 as smallint) as [value]"; // 200 more than sbyte.MaxValue but less than byte.MaxValue
Issue1164Object<byte> obj = connection.QuerySingle<Issue1164Object<byte>>(sql);
Assert.StrictEqual(200, obj.Value);
}

[Fact]
public void Issue1164_OverflowExceptionForUInt16()
{
const string sql = "select cast(40000 as bigint) as [value]"; // 40000 more than short.MaxValue but less than ushort.MaxValue
Issue1164Object<ushort> obj = connection.QuerySingle<Issue1164Object<ushort>>(sql);
Assert.StrictEqual(40000, obj.Value);
}

[Fact]
public void Issue1164_OverflowExceptionForUInt32()
{
const string sql = "select cast(4000000000 as bigint) as [value]"; // 4000000000 more than int.MaxValue but less than uint.MaxValue
Issue1164Object<uint> obj = connection.QuerySingle<Issue1164Object<uint>>(sql);
Assert.StrictEqual(4000000000, obj.Value);
}

[Fact]
public void Issue1164_OverflowExceptionForUInt64()
{
const string sql = "select cast(10000000000000000000.0 as float) as [value]"; // 10000000000000000000 more than long.MaxValue but less than ulong.MaxValue
Issue1164Object<ulong> obj = connection.QuerySingle<Issue1164Object<ulong>>(sql);
Assert.StrictEqual(10000000000000000000, obj.Value);
}

private class Issue1164Object<T>
{
public T Value;
}
}
}