Skip to content

Commit c20ba59

Browse files
committed
Fixed overflow problem
Added tests
1 parent e45338c commit c20ba59

3 files changed

Lines changed: 57 additions & 34 deletions

File tree

L2Net.PacketConverter.Core/Convert.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,34 @@ namespace L2Net.PacketConverter.Core
77
{
88
public static class Convert
99
{
10-
public static string FromByteToHex(string i)
10+
public static string FromByteToHex(byte i)
1111
{
12-
return FromNoToHex(System.Convert.ToInt32(i), 1);
12+
return FromNumberToHex(i, 1);
1313
}
1414

15-
public static string FromInt16ToHex(int i)
15+
public static string FromInt16ToHex(Int16 i)
1616
{
17-
return FromNoToHex(i, 2);
17+
return FromNumberToHex(i, 2);
1818
}
1919

20-
public static string FromInt32ToHex(int i)
20+
public static string FromInt32ToHex(Int32 i)
2121
{
22-
return FromNoToHex(i, 4);
22+
return FromNumberToHex(i, 4);
2323
}
2424

2525
public static string FromInt64ToHex(long i)
2626
{
27-
return FromNoToHex(i, 8);
27+
return FromNumberToHex(i, 8);
2828
}
2929

30-
private static string FromNoToHex(long i, int size)
30+
private static string FromNumberToHex(long i, int size)
3131
{
32-
return FromNoToHex(System.Convert.ToByte(i), size);
32+
return FromNumberToHex(BitConverter.GetBytes(i), size);
3333
}
3434

35-
private static string FromNoToHex(byte i, int size)
35+
private static string FromNumberToHex(byte[] i, int size)
3636
{
37-
var a = new byte[size];
38-
a[0] = i;
39-
return BitConverter.ToString(a).NormalizeString();
37+
return BitConverter.ToString(i.Take(size).ToArray()).NormalizeString();
4038
}
4139

4240
public static byte FromHexToByte(string hex)
@@ -46,20 +44,22 @@ public static byte FromHexToByte(string hex)
4644

4745
public static int FromHexToInt16(string hex)
4846
{
49-
return BitConverter.ToInt16(FromHexToNo(hex, 4), 0);
47+
return BitConverter.ToInt16(FromHexToBytes(hex, 4), 0) is Int16 n && n == -1 ? int.MaxValue : n;
5048
}
5149

5250
public static int FromHexToInt32(string hex)
5351
{
54-
return BitConverter.ToInt32(FromHexToNo(hex, 8), 0);
52+
return
53+
BitConverter.ToInt32(FromHexToBytes(hex, 8), 0) is Int32 n && n == -1 ? Int32.MaxValue : n;
5554
}
5655

5756
public static long FromHexToInt64(string hex)
5857
{
59-
return BitConverter.ToInt64(FromHexToNo(hex, 16), 0);
58+
return
59+
BitConverter.ToInt64(FromHexToBytes(hex, 16), 0) is Int64 n && n == -1 ? Int64.MaxValue : n;
6060
}
6161

62-
public static byte[] FromHexToNo(string hex, int size)
62+
public static byte[] FromHexToBytes(string hex, int size)
6363
{
6464
hex = hex.RemoveSeparators();
6565
if (hex.Length < size)
@@ -71,7 +71,7 @@ public static byte[] FromHexToNo(string hex, int size)
7171
}
7272
hex = sb.ToString();
7373
}
74-
byte[] raw = new byte[size / 2];
74+
byte[] raw = new byte[size];
7575
for (int i = 0; i < raw.Length / 2; i++)
7676
{
7777
raw[i] = System.Convert.ToByte(hex.Substring(i * 2, 2), 16);

L2Net.PacketConverter.Tests/ConvertTests.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,33 @@ namespace L2Net.PacketConverter.Tests
66
[TestClass]
77
public class ConvertTests
88
{
9-
[DataRow("52", "82")]
10-
public void FromByteToHexTest(string expected, string input)
9+
[DataRow("52", 82)]
10+
public void FromByteToHexTest(string expected, byte input)
1111
{
1212
Assert.AreEqual(expected, Convert.FromByteToHex(input));
1313
}
1414

15-
[TestMethod]
16-
public void FromInt16ToHexTest()
15+
[DataRow("52 00", 82)]
16+
[DataRow("FF FF", 99999999)]
17+
public void FromInt16ToHexTest(string expected, byte bytee)
1718
{
18-
Assert.AreEqual("52 00", Convert.FromInt16ToHex(82));
19+
Assert.AreEqual(expected, Convert.FromInt16ToHex(bytee));
1920
}
2021

21-
[TestMethod]
22-
public void FromInt32ToHexTest()
22+
[DataRow("52 00 00 00", 82)]
23+
[DataRow("52 00 6F 00", 7274578)]
24+
[DataRow("FF FF FF F7", 4294967295)]
25+
public void FromInt32ToHexTest(string expected, int int32)
2326
{
24-
Assert.AreEqual("52 00 00 00", Convert.FromInt32ToHex(82));
27+
Assert.AreEqual(expected, Convert.FromInt32ToHex(int32));
2528
}
2629

27-
[TestMethod]
28-
public void FromInt64ToHexTest()
30+
[DataRow("52 00 00 00 00 00 00 00", 7274578)]
31+
[DataRow("FF FF C0 6F F2 86 23 00", 9999999999999999)]
32+
[DataRow("FF FF FF FF FF FF FF F7", 9223372036854775807)]
33+
public void FromInt64ToHexTest(string expected, System.Int64 int64)
2934
{
30-
Assert.AreEqual("52 00 00 00 00 00 00 00", Convert.FromInt64ToHex(82));
35+
Assert.AreEqual(expected, Convert.FromInt64ToHex(int64));
3136
}
3237

3338
[DataTestMethod]
@@ -48,7 +53,7 @@ public void FromHexToInt16Test(int expected, string hex)
4853

4954
[DataTestMethod]
5055
[DataRow(76, "4C")]
51-
[DataRow(76, "4C 00 32 00 4E 00 65 00 74 00 52 00 6F 00 63 00 6B 00")]
56+
[DataRow(3276876, "4C 00 32 00 4E 00 65 00 74 00 52 00 6F 00 63 00 6B 00")]
5257
public void FromHexToInt32Test(int expected, string hex)
5358
{
5459
Assert.AreEqual(expected, Convert.FromHexToInt32(hex));

L2Net.PacketConverter.UI/PacketConverterForm.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,20 @@ private void rtbxPlain_KeyUp(object sender, KeyEventArgs e)
5454
switch (Mode)
5555
{
5656
case ConvertMode.Byte:
57-
value = Convert.FromByteToHex(rtbxPlain.Text);
57+
var bytee = GetIntSafe(rtbxPlain.Text, System.Convert.ToByte, byte.MaxValue);
58+
value = Convert.FromByteToHex(bytee);
5859
break;
5960
case ConvertMode.Int16:
60-
value = Convert.FromInt16ToHex(System.Convert.ToInt16(rtbxPlain.Text));
61+
var int16 = GetIntSafe(rtbxPlain.Text, System.Convert.ToInt16, Int16.MaxValue);
62+
value = Convert.FromInt16ToHex(int16);
6163
break;
6264
case ConvertMode.Int32:
63-
value = Convert.FromInt32ToHex(System.Convert.ToInt32(rtbxPlain.Text));
65+
var int32 = GetIntSafe(rtbxPlain.Text, System.Convert.ToInt32, int.MaxValue);
66+
value = Convert.FromInt32ToHex(int32);
6467
break;
6568
case ConvertMode.Int64:
66-
value = Convert.FromInt64ToHex(System.Convert.ToInt64(rtbxPlain.Text));
69+
var int64 = GetIntSafe(rtbxPlain.Text, System.Convert.ToInt64, Int64.MaxValue);
70+
value = Convert.FromInt64ToHex(int64);
6771
break;
6872
case ConvertMode.String:
6973
value = Convert.FromStringToHex(rtbxPlain.Text);
@@ -78,6 +82,20 @@ private void rtbxPlain_KeyUp(object sender, KeyEventArgs e)
7882
LastSender = sender as RichTextBox;
7983
}
8084

85+
private T GetIntSafe<T>(string text, Func<string, T> fun, T fallback)
86+
{
87+
T int16;
88+
try
89+
{
90+
int16 = fun.Invoke(text);
91+
}
92+
catch (OverflowException)
93+
{
94+
int16 = fallback;
95+
}
96+
return int16;
97+
}
98+
8199
private void rbtn_CheckedChanged(object sender, EventArgs e)
82100
{
83101
switch ((sender as RadioButton).Name)

0 commit comments

Comments
 (0)