未验证 提交 01a36b5a 编写于 作者: M Michal Strehovský 提交者: GitHub

Avoid Marshal.SizeOf in EventSource (#47521)

* Avoid Marshal.SizeOf in EventSource

We're getting the marshalling size of an enum underlying type - there are not that many of those. I chose not to treat `char` and `bool`, but we could (the only other remaining options).

Calling into `Marshal.SizeOf` increases the risk that `EventSource` is going call itself recursively - see the comment at the beginning of the method.

This was originally introduced in https://github.com/dotnet/coreclr/pull/19205.

* Update src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs

* Update src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs

* Update src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs
上级 5fc66480
...@@ -1696,9 +1696,16 @@ private static Guid GenerateGuidFromName(string name) ...@@ -1696,9 +1696,16 @@ private static Guid GenerateGuidFromName(string name)
{ {
dataType = Enum.GetUnderlyingType(dataType); dataType = Enum.GetUnderlyingType(dataType);
int dataTypeSize = System.Runtime.InteropServices.Marshal.SizeOf(dataType); // Enums less than 4 bytes in size should be treated as int.
if (dataTypeSize < sizeof(int)) switch (Type.GetTypeCode(dataType))
dataType = typeof(int); {
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.UInt16:
dataType = typeof(int);
break;
}
goto Again; goto Again;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册