Skip to content

Commit fa9ad22

Browse files
authored
Fix EventLog test CanReadAndWriteMessages (#89586)
* Fix EventLog bug discovered by the CanReadAndWriteMessages test where we were not trimming the null characters from the strings received from the P/Invokes.
1 parent 21f987c commit fa9ad22

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/NativeWrapper.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,13 @@ public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLo
967967
EventLogException.Throw(error);
968968
}
969969

970-
int len = bufferNeeded - 1; // buffer includes null terminator
970+
971+
// buffer may include null terminators
972+
int len = bufferNeeded;
973+
while (len > 0 && buffer[len - 1] == '\0')
974+
{
975+
len--;
976+
}
971977
if (len <= 0)
972978
return string.Empty;
973979

src/libraries/System.Diagnostics.EventLog/tests/EventLogMessagesTests.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Diagnostics.Eventing.Reader;
55
using System.IO;
66
using System.Reflection;
7-
using System.Runtime.InteropServices;
87
using Microsoft.Win32.SafeHandles;
98
using Xunit;
109

@@ -50,10 +49,7 @@ public unsafe void CanFormatMessage(uint messageId)
5049
}
5150
}
5251

53-
public static bool HasAssemblyFilesIsElevatedAndSupportsEventLogs => PlatformDetection.HasAssemblyFiles && Helpers.IsElevatedAndSupportsEventLogs;
54-
55-
[ConditionalFact(nameof(HasAssemblyFilesIsElevatedAndSupportsEventLogs))]
56-
[ActiveIssue("https://github.com/dotnet/runtime/issues/88224", typeof(PlatformDetection), nameof(PlatformDetection.IsWindows10Version22000OrGreater))]
52+
[ConditionalFact(typeof(Helpers), nameof(Helpers.HasAssemblyFilesIsElevatedAndSupportsEventLogs))]
5753
public void CanReadAndWriteMessages()
5854
{
5955
string messageDllPath = Path.Combine(Path.GetDirectoryName(typeof(EventLog).Assembly.Location), "System.Diagnostics.EventLog.Messages.dll");

src/libraries/System.Diagnostics.EventLog/tests/Helpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.ComponentModel;
54
using System.Diagnostics.Eventing.Reader;
65
using System.Threading;
76
using Xunit;
@@ -13,6 +12,7 @@ namespace System.Diagnostics.Tests
1312
{
1413
internal class Helpers
1514
{
15+
public static bool HasAssemblyFilesIsElevatedAndSupportsEventLogs => PlatformDetection.HasAssemblyFiles && IsElevatedAndSupportsEventLogs;
1616
public static bool NotElevatedAndSupportsEventLogs { get => !AdminHelpers.IsProcessElevated() && SupportsEventLogs; }
1717
public static bool IsElevatedAndSupportsEventLogs { get => AdminHelpers.IsProcessElevated() && SupportsEventLogs; }
1818
public static bool SupportsEventLogs { get => PlatformDetection.IsNotWindowsNanoNorServerCore && PlatformDetection.IsNotWindowsIoTCore; }

src/libraries/System.Diagnostics.EventLog/tests/System.Diagnostics.EventLog.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetFrameworkMinimum)</TargetFrameworks>
44
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

0 commit comments

Comments
 (0)