Skip to content

Commit

Permalink
Fix EventLog test CanReadAndWriteMessages (#89586)
Browse files Browse the repository at this point in the history
* Fix EventLog bug discovered by the CanReadAndWriteMessages test where we were not trimming the null characters from the strings received from the P/Invokes.
  • Loading branch information
carlossanlop authored Jul 31, 2023
1 parent 21f987c commit fa9ad22
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,13 @@ public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLo
EventLogException.Throw(error);
}

int len = bufferNeeded - 1; // buffer includes null terminator

// buffer may include null terminators
int len = bufferNeeded;
while (len > 0 && buffer[len - 1] == '\0')
{
len--;
}
if (len <= 0)
return string.Empty;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Diagnostics.Eventing.Reader;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using Xunit;

Expand Down Expand Up @@ -50,10 +49,7 @@ public unsafe void CanFormatMessage(uint messageId)
}
}

public static bool HasAssemblyFilesIsElevatedAndSupportsEventLogs => PlatformDetection.HasAssemblyFiles && Helpers.IsElevatedAndSupportsEventLogs;

[ConditionalFact(nameof(HasAssemblyFilesIsElevatedAndSupportsEventLogs))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/88224", typeof(PlatformDetection), nameof(PlatformDetection.IsWindows10Version22000OrGreater))]
[ConditionalFact(typeof(Helpers), nameof(Helpers.HasAssemblyFilesIsElevatedAndSupportsEventLogs))]
public void CanReadAndWriteMessages()
{
string messageDllPath = Path.Combine(Path.GetDirectoryName(typeof(EventLog).Assembly.Location), "System.Diagnostics.EventLog.Messages.dll");
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Diagnostics.EventLog/tests/Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using System.Diagnostics.Eventing.Reader;
using System.Threading;
using Xunit;
Expand All @@ -13,6 +12,7 @@ namespace System.Diagnostics.Tests
{
internal class Helpers
{
public static bool HasAssemblyFilesIsElevatedAndSupportsEventLogs => PlatformDetection.HasAssemblyFiles && IsElevatedAndSupportsEventLogs;
public static bool NotElevatedAndSupportsEventLogs { get => !AdminHelpers.IsProcessElevated() && SupportsEventLogs; }
public static bool IsElevatedAndSupportsEventLogs { get => AdminHelpers.IsProcessElevated() && SupportsEventLogs; }
public static bool SupportsEventLogs { get => PlatformDetection.IsNotWindowsNanoNorServerCore && PlatformDetection.IsNotWindowsIoTCore; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetFrameworkMinimum)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down

0 comments on commit fa9ad22

Please sign in to comment.