Skip to content

Commit

Permalink
Issue #1164
Browse files Browse the repository at this point in the history
Fix unsigned types mapping arithmetic overflow
Add tests to reproduce issue #1164
  • Loading branch information
Dmytro Ohorodniichuk committed Nov 1, 2023
1 parent f0e61a7 commit 60cb4ec
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
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;
}
}
}

0 comments on commit 60cb4ec

Please sign in to comment.