Skip to content

Commit 01a36b5

Browse files
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 dotnet/coreclr#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
1 parent 5fc6648 commit 01a36b5

File tree

1 file changed

+10
-3
lines changed
  • src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing

1 file changed

+10
-3
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,9 +1696,16 @@ private static Guid GenerateGuidFromName(string name)
16961696
{
16971697
dataType = Enum.GetUnderlyingType(dataType);
16981698

1699-
int dataTypeSize = System.Runtime.InteropServices.Marshal.SizeOf(dataType);
1700-
if (dataTypeSize < sizeof(int))
1701-
dataType = typeof(int);
1699+
// Enums less than 4 bytes in size should be treated as int.
1700+
switch (Type.GetTypeCode(dataType))
1701+
{
1702+
case TypeCode.Byte:
1703+
case TypeCode.SByte:
1704+
case TypeCode.Int16:
1705+
case TypeCode.UInt16:
1706+
dataType = typeof(int);
1707+
break;
1708+
}
17021709
goto Again;
17031710
}
17041711

0 commit comments

Comments
 (0)