From 7fec94d1cb78b93f8ea54b9c8694def4cc7947e2 Mon Sep 17 00:00:00 2001 From: Eric StJohn Date: Mon, 22 Jul 2024 22:23:52 +0000 Subject: [PATCH 1/3] Workaround bug in EvtFormatMessage --- .../src/System/Diagnostics/Reader/NativeWrapper.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs index 79e8eac8e4e06..2376671519e4a 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs +++ b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs @@ -482,7 +482,8 @@ internal static EventLogHandle EvtGetPublisherMetadataPropertyHandle(EventLogHan public static string EvtFormatMessage(EventLogHandle handle, uint msgId) { int bufferNeeded; - bool status = UnsafeNativeMethods.EvtFormatMessage(handle, EventLogHandle.Zero, msgId, 0, null, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageId, 0, null, out bufferNeeded); + char[] emptyBuffer = { '\0' }; + bool status = UnsafeNativeMethods.EvtFormatMessage(handle, EventLogHandle.Zero, msgId, 0, null, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageId, 0, emptyBuffer, out bufferNeeded); int error = Marshal.GetLastWin32Error(); // ERROR_EVT_UNRESOLVED_VALUE_INSERT and its cousins are commonly returned for raw message text. @@ -933,7 +934,8 @@ public static IList EvtRenderBufferWithContextUserOrValues(EventLogHandl public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLogHandle eventHandle, UnsafeNativeMethods.EvtFormatMessageFlags flag) { int bufferNeeded; - bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, null, out bufferNeeded); + char[] emptyBuffer = { '\0' }; + bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, emptyBuffer, out bufferNeeded); int error = Marshal.GetLastWin32Error(); if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT @@ -985,11 +987,12 @@ public static IEnumerable EvtFormatMessageRenderKeywords(EventLogHandle { IntPtr buffer = IntPtr.Zero; int bufferNeeded; + char[] emptyBuffer = { '\0' }; try { List keywordsList = new List(); - bool status = UnsafeNativeMethods.EvtFormatMessageBuffer(pmHandle, eventHandle, 0, 0, IntPtr.Zero, flag, 0, IntPtr.Zero, out bufferNeeded); + bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, emptyBuffer, out bufferNeeded); int error = Marshal.GetLastWin32Error(); if (!status) @@ -1071,6 +1074,7 @@ public static string EvtRenderBookmark(EventLogHandle eventHandle) public static string EvtFormatMessageFormatDescription(EventLogHandle handle, EventLogHandle eventHandle, string[] values) { int bufferNeeded; + char[] emptyBuffer = { '\0' }; UnsafeNativeMethods.EvtStringVariant[] stringVariants = new UnsafeNativeMethods.EvtStringVariant[values.Length]; for (int i = 0; i < values.Length; i++) @@ -1079,7 +1083,7 @@ public static string EvtFormatMessageFormatDescription(EventLogHandle handle, Ev stringVariants[i].StringVal = values[i]; } - bool status = UnsafeNativeMethods.EvtFormatMessage(handle, eventHandle, 0xffffffff, values.Length, stringVariants, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageEvent, 0, null, out bufferNeeded); + bool status = UnsafeNativeMethods.EvtFormatMessage(handle, eventHandle, 0xffffffff, values.Length, stringVariants, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageEvent, 0, emptyBuffer, out bufferNeeded); int error = Marshal.GetLastWin32Error(); if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT From 6059a7a41cf0b83619e4ab8541c88e57efe25ada Mon Sep 17 00:00:00 2001 From: Eric StJohn Date: Mon, 29 Jul 2024 10:22:01 -0700 Subject: [PATCH 2/3] Add issue comment for 100198 --- .../src/System/Diagnostics/Reader/NativeWrapper.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs index 2376671519e4a..62e534e792601 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs +++ b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs @@ -482,7 +482,7 @@ internal static EventLogHandle EvtGetPublisherMetadataPropertyHandle(EventLogHan public static string EvtFormatMessage(EventLogHandle handle, uint msgId) { int bufferNeeded; - char[] emptyBuffer = { '\0' }; + char[] emptyBuffer = { '\0' }; // issue: https://github.com/dotnet/runtime/issues/100198 bool status = UnsafeNativeMethods.EvtFormatMessage(handle, EventLogHandle.Zero, msgId, 0, null, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageId, 0, emptyBuffer, out bufferNeeded); int error = Marshal.GetLastWin32Error(); @@ -934,7 +934,7 @@ public static IList EvtRenderBufferWithContextUserOrValues(EventLogHandl public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLogHandle eventHandle, UnsafeNativeMethods.EvtFormatMessageFlags flag) { int bufferNeeded; - char[] emptyBuffer = { '\0' }; + char[] emptyBuffer = { '\0' }; // issue: https://github.com/dotnet/runtime/issues/100198 bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, emptyBuffer, out bufferNeeded); int error = Marshal.GetLastWin32Error(); @@ -987,7 +987,7 @@ public static IEnumerable EvtFormatMessageRenderKeywords(EventLogHandle { IntPtr buffer = IntPtr.Zero; int bufferNeeded; - char[] emptyBuffer = { '\0' }; + char[] emptyBuffer = { '\0' }; // issue: https://github.com/dotnet/runtime/issues/100198 try { @@ -1074,7 +1074,7 @@ public static string EvtRenderBookmark(EventLogHandle eventHandle) public static string EvtFormatMessageFormatDescription(EventLogHandle handle, EventLogHandle eventHandle, string[] values) { int bufferNeeded; - char[] emptyBuffer = { '\0' }; + char[] emptyBuffer = { '\0' }; // issue: https://github.com/dotnet/runtime/issues/100198 UnsafeNativeMethods.EvtStringVariant[] stringVariants = new UnsafeNativeMethods.EvtStringVariant[values.Length]; for (int i = 0; i < values.Length; i++) From 7a9dc7d2bf03b82deee1544856fda3fb9478827c Mon Sep 17 00:00:00 2001 From: Eric StJohn Date: Mon, 29 Jul 2024 14:59:13 -0700 Subject: [PATCH 3/3] Use stackalloc buffer --- .../src/System/Diagnostics/Reader/NativeWrapper.cs | 8 ++++---- .../src/System/Diagnostics/Reader/UnsafeNativeMethods.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs index 62e534e792601..401c40e866f77 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs +++ b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs @@ -482,7 +482,7 @@ internal static EventLogHandle EvtGetPublisherMetadataPropertyHandle(EventLogHan public static string EvtFormatMessage(EventLogHandle handle, uint msgId) { int bufferNeeded; - char[] emptyBuffer = { '\0' }; // issue: https://github.com/dotnet/runtime/issues/100198 + Span emptyBuffer = [ '\0' ]; // issue: https://github.com/dotnet/runtime/issues/100198 bool status = UnsafeNativeMethods.EvtFormatMessage(handle, EventLogHandle.Zero, msgId, 0, null, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageId, 0, emptyBuffer, out bufferNeeded); int error = Marshal.GetLastWin32Error(); @@ -934,7 +934,7 @@ public static IList EvtRenderBufferWithContextUserOrValues(EventLogHandl public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLogHandle eventHandle, UnsafeNativeMethods.EvtFormatMessageFlags flag) { int bufferNeeded; - char[] emptyBuffer = { '\0' }; // issue: https://github.com/dotnet/runtime/issues/100198 + Span emptyBuffer = [ '\0' ]; // issue: https://github.com/dotnet/runtime/issues/100198 bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, emptyBuffer, out bufferNeeded); int error = Marshal.GetLastWin32Error(); @@ -987,7 +987,7 @@ public static IEnumerable EvtFormatMessageRenderKeywords(EventLogHandle { IntPtr buffer = IntPtr.Zero; int bufferNeeded; - char[] emptyBuffer = { '\0' }; // issue: https://github.com/dotnet/runtime/issues/100198 + Span emptyBuffer = [ '\0' ]; // issue: https://github.com/dotnet/runtime/issues/100198 try { @@ -1074,7 +1074,7 @@ public static string EvtRenderBookmark(EventLogHandle eventHandle) public static string EvtFormatMessageFormatDescription(EventLogHandle handle, EventLogHandle eventHandle, string[] values) { int bufferNeeded; - char[] emptyBuffer = { '\0' }; // issue: https://github.com/dotnet/runtime/issues/100198 + Span emptyBuffer = [ '\0' ]; // issue: https://github.com/dotnet/runtime/issues/100198 UnsafeNativeMethods.EvtStringVariant[] stringVariants = new UnsafeNativeMethods.EvtStringVariant[values.Length]; for (int i = 0; i < values.Length; i++) diff --git a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/UnsafeNativeMethods.cs b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/UnsafeNativeMethods.cs index a8f8fd6336969..9f9e2862e0293 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/UnsafeNativeMethods.cs +++ b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/UnsafeNativeMethods.cs @@ -762,7 +762,7 @@ internal static partial bool EvtFormatMessage( EvtStringVariant[] values, EvtFormatMessageFlags flags, int bufferSize, - [Out] char[]? buffer, + Span buffer, out int bufferUsed); [LibraryImport(Interop.Libraries.Wevtapi, EntryPoint = "EvtFormatMessage", SetLastError = true)]