8000 Fixed packet corruption issue when enum was not 4 bytes by daebo01 · Pull Request #1421 · mysql-net/MySqlConnector · GitHub
[go: up one dir, main page]

Skip to content

Fixed packet corruption issue when enum was not 4 bytes #1421

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

Merged
merged 1 commit into from
Dec 18, 2023
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
40 changes: 39 additions & 1 deletion src/MySqlConnector/MySqlParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,45 @@ internal void AppendBinary(ByteBufferWriter writer, StatementPreparerOptions opt
}
else if (Value is Enum)
{
writer.Write(Convert.ToInt32(Value, CultureInfo.InvariantCulture));
var enumType = Value.GetType();
var enumUnderlyingType = enumType.GetEnumUnderlyingType();

if (enumUnderlyingType == typeof(byte))
{
writer.Write((byte) Value);
}
else if (enumUnderlyingType == typeof(sbyte))
{
writer.Write(unchecked((byte) (sbyte) Value));
}
else if (enumUnderlyingType == typeof(short))
{
writer.Write(unchecked((ushort) (short) Value));
}
else if (enumUnderlyingType == typeof(ushort))
{
writer.Write((ushort) Value);
}
else if (enumUnderlyingType == typeof(int))
{
writer.Write((int) Value);
}
else if (enumUnderlyingType == typeof(uint))
{
writer.Write((uint) Value);
}
else if (enumUnderlyingType == typeof(long))
{
writer.Write(unchecked((ulong) (long) Value));
}
else if (enumUnderlyingType == typeof(ulong))
{
writer.Write((ulong) Value);
}
else
{
throw new NotSupportedException($"Parameter type {Value.GetType().Name} is not supported; see https://fl.vu/mysql-param-type. Value: {Value}");
}
}
else if (MySqlDbType == MySqlDbType.Int16)
{
Expand Down
50 changes: 49 additions & 1 deletion tests/MySqlConnector.Tests/DummyEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,52 @@ internal enum DummyEnum
{
FirstValue,
SecondValue
}
}

internal enum DummyByteEnum : byte
{
FirstValue,
SecondValue = 0x11,
}

internal enum DummySByteEnum : sbyte
{
FirstValue,
SecondValue = 0x11,
}

internal enum DummyShortEnum : short
{
FirstValue,
SecondValue = 0x1122,
}

internal enum DummyUShortEnum : ushort
{
FirstValue,
SecondValue = 0x1122,
}

internal enum DummyIntEnum : int
{
FirstValue,
SecondValue = 0x11223344,
}

internal enum DummyUIntEnum : uint
{
FirstValue,
SecondValue = 0x11223344,
}

internal enum DummyLongEnum : long
{
FirstValue,
SecondValue = 0x11223344_55667788,
}

internal enum DummyULongEnum : ulong
{
FirstValue,
SecondValue = 0x11223344_55667788,
}
2 changes: 1 addition & 1 deletion tests/MySqlConnector.Tests/MySqlConnector.Tests.csproj
8000
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

<ItemGroup Condition=" '$(Configuration)' == 'MySqlData' ">
<PackageReference Include="MySql.Data" />
<Compile Remove="ByteBufferWriterTests.cs;CachedProcedureTests.cs;CancellationTests.cs;ColumnCountPayloadTests.cs;ColumnReaderTests.cs;ConnectionTests.cs;FakeMySqlServer.cs;FakeMySqlServerConnection.cs;LoadBalancerTests.cs;MySqlDecimalTests.cs;MySqlExceptionTests.cs;MySqlParameterCollectionNameToIndexTests.cs;NormalizeTests.cs;ServerVersionTests.cs;StatementPreparerTests.cs;TypeMapperTests.cs;UtilityTests.cs" />
<Compile Remove="ByteBufferWriterTests.cs;CachedProcedureTests.cs;CancellationTests.cs;ColumnCountPayloadTests.cs;ColumnReaderTests.cs;ConnectionTests.cs;FakeMySqlServer.cs;FakeMySqlServerConnection.cs;LoadBalancerTests.cs;MySqlDecimalTests.cs;MySqlExceptionTests.cs;MySqlParameterAppendBinaryTests.cs;MySqlParameterCollectionNameToIndexTests.cs;NormalizeTests.cs;ServerVersionTests.cs;StatementPreparerTests.cs;TypeMapperTests.cs;UtilityTests.cs" />
<Compile Remove="Metrics\*.cs" />
<Using Include="MySql.Data.MySqlClient" />
<Using Include="MySql.Data.Types" />
Expand Down
27 changes: 27 additions & 0 deletions tests/MySqlConnector.Tests/MySqlParameterAppendBinaryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using MySqlConnector.Protocol.Serialization;

namespace MySqlConnector.Tests;

public class MySqlParameterAppendBinaryTests
{
[Theory]
[InlineData(DummySByteEnum.SecondValue, MySqlDbType.Byte, new byte[] { 0x11 })]
[InlineData(DummyByteEnum.SecondValue, MySqlDbType.UByte, new byte[] { 0x11 })]
[InlineData(DummyShortEnum.SecondValue, MySqlDbType.Int16, new byte[] { 0x22, 0x11 })]
[InlineData(DummyUShortEnum.SecondValue, MySqlDbType.UInt16, new byte[] { 0x22, 0x11 })]
[InlineData(DummyIntEnum.SecondValue, MySqlDbType.Int32, new byte[] { 0x44, 0x33, 0x22, 0x11 })]
[InlineData(DummyUIntEnum.SecondValue, MySqlDbType.UInt32, new byte[] { 0x44, 0x33, 0x22, 0x11 })]
[InlineData(DummyEnum.SecondValue, MySqlDbType.Int32, new byte[] { 0x01, 0x00, 0x00, 0x00 })]
[InlineData(DummyLongEnum.SecondValue, MySqlDbType.Int64, new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 })]
[InlineData(DummyULongEnum.SecondValue, MySqlDbType.UInt64, new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 })]
public void WriteBinaryEnumType(object value, MySqlDbType expectedMySqlDbType, byte[] expectedBinary)
{
var parameter = new MySqlParameter { Value = value };
var writer = new ByteBufferWriter();
parameter.AppendBinary(writer, StatementPreparerOptions.None);

Assert.Equal(parameter.MySqlDbType, expectedMySqlDbType);
Assert.Equal(writer.Position, expectedBinary.Length);
Assert.Equal(writer.ArraySegment, expectedBinary);
}
}
0