Skip to content

Commit 1ca0e6f

Browse files
author
Dmytro Ohorodniichuk
committed
Issue #1164
Fix unsigned types mapping arithmetic overflow Add tests to reproduce issue #1164
1 parent ca00fee commit 1ca0e6f

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

Dapper/SqlMapper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3588,20 +3588,20 @@ private static void FlexibleConvertBoxedFromHeadOfStack(ILGenerator il, Type fro
35883588
switch (Type.GetTypeCode(via ?? to))
35893589
{
35903590
case TypeCode.Byte:
3591-
opCode = OpCodes.Conv_Ovf_I1_Un; break;
3591+
opCode = OpCodes.Conv_Ovf_U1_Un; break;
35923592
case TypeCode.SByte:
35933593
opCode = OpCodes.Conv_Ovf_I1; break;
35943594
case TypeCode.UInt16:
3595-
opCode = OpCodes.Conv_Ovf_I2_Un; break;
3595+
opCode = OpCodes.Conv_Ovf_U2_Un; break;
35963596
case TypeCode.Int16:
35973597
opCode = OpCodes.Conv_Ovf_I2; break;
35983598
case TypeCode.UInt32:
3599-
opCode = OpCodes.Conv_Ovf_I4_Un; break;
3599+
opCode = OpCodes.Conv_Ovf_U4_Un; break;
36003600
case TypeCode.Boolean: // boolean is basically an int, at least at this level
36013601
case TypeCode.Int32:
36023602
opCode = OpCodes.Conv_Ovf_I4; break;
36033603
case TypeCode.UInt64:
3604-
opCode = OpCodes.Conv_Ovf_I8_Un; break;
3604+
opCode = OpCodes.Conv_Ovf_U8_Un; break;
36053605
case TypeCode.Int64:
36063606
opCode = OpCodes.Conv_Ovf_I8; break;
36073607
case TypeCode.Single:

tests/Dapper.Tests/MiscTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,5 +1273,42 @@ private class HazGetOnly
12731273
public int Id { get; }
12741274
public string Name { get; } = "abc";
12751275
}
1276+
1277+
[Fact]
1278+
public void Issue1164_OverflowExceptionForByte()
1279+
{
1280+
const string sql = "select cast(200 as smallint) as [value]"; // 200 more than sbyte.MaxValue but less than byte.MaxValue
1281+
Issue1164Object<byte> obj = connection.QuerySingle<Issue1164Object<byte>>(sql);
1282+
Assert.StrictEqual(200, obj.Value);
1283+
}
1284+
1285+
[Fact]
1286+
public void Issue1164_OverflowExceptionForUInt16()
1287+
{
1288+
const string sql = "select cast(40000 as bigint) as [value]"; // 40000 more than short.MaxValue but less than ushort.MaxValue
1289+
Issue1164Object<ushort> obj = connection.QuerySingle<Issue1164Object<ushort>>(sql);
1290+
Assert.StrictEqual(40000, obj.Value);
1291+
}
1292+
1293+
[Fact]
1294+
public void Issue1164_OverflowExceptionForUInt32()
1295+
{
1296+
const string sql = "select cast(4000000000 as bigint) as [value]"; // 4000000000 more than int.MaxValue but less than uint.MaxValue
1297+
Issue1164Object<uint> obj = connection.QuerySingle<Issue1164Object<uint>>(sql);
1298+
Assert.StrictEqual(4000000000, obj.Value);
1299+
}
1300+
1301+
[Fact]
1302+
public void Issue1164_OverflowExceptionForUInt64()
1303+
{
1304+
const string sql = "select cast(10000000000000000000.0 as float) as [value]"; // 10000000000000000000 more than long.MaxValue but less than ulong.MaxValue
1305+
Issue1164Object<ulong> obj = connection.QuerySingle<Issue1164Object<ulong>>(sql);
1306+
Assert.StrictEqual(10000000000000000000, obj.Value);
1307+
}
1308+
1309+
private class Issue1164Object<T>
1310+
{
1311+
public T Value;
1312+
}
12761313
}
12771314
}

0 commit comments

Comments
 (0)