Skip to content

Commit 614a9e6

Browse files
jschick04NikTilton
authored andcommitted
Fixed another out of range error when 0 is used as a terminator
1 parent 94269d4 commit 614a9e6

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

src/EventLogExpert.Eventing.Tests/EventResolvers/EventResolverBaseTests.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,86 @@ public void ResolveEvent_WithZeroKeywords_ShouldReturnEmptyKeywordList()
779779
Assert.Empty(displayEvent.Keywords);
780780
}
781781

782+
[Fact]
783+
public void ResolveEvent_WithPercentZeroTerminator_ShouldSkipTerminatorAndResolveDescription()
784+
{
785+
// Arrange - %0 is a Windows Event Log message terminator that should be stripped
786+
var providerDetails = new ProviderDetails
787+
{
788+
ProviderName = Constants.TestProviderName,
789+
Events = [],
790+
Messages =
791+
[
792+
new MessageModel
793+
{
794+
ProviderName = Constants.TestProviderName,
795+
ShortId = 1000,
796+
Text = "Windows Search Service has created default configuration for new user '%1' .%n%0"
797+
}
798+
],
799+
Parameters = [],
800+
Keywords = new Dictionary<long, string>(),
801+
Tasks = new Dictionary<int, string>()
802+
};
803+
804+
var resolver = new TestEventResolver([providerDetails]);
805+
806+
var eventRecord = new EventRecord
807+
{
808+
ProviderName = Constants.TestProviderName,
809+
Id = 1000,
810+
Properties = ["TestUser", "ExtraProperty"]
811+
};
812+
813+
// Act - should not throw and should produce valid description
814+
var displayEvent = resolver.ResolveEvent(eventRecord);
815+
816+
// Assert
817+
Assert.NotNull(displayEvent);
818+
Assert.Contains("TestUser", displayEvent.Description);
819+
Assert.DoesNotContain("%0", displayEvent.Description);
820+
Assert.DoesNotContain("Failed to resolve", displayEvent.Description);
821+
}
822+
823+
[Fact]
824+
public void ResolveEvent_WithPropertyIndexOutOfRange_ShouldReturnFailedDescription()
825+
{
826+
// Arrange - template references %3 but only 2 properties exist
827+
var providerDetails = new ProviderDetails
828+
{
829+
ProviderName = Constants.TestProviderName,
830+
Events = [],
831+
Messages =
832+
[
833+
new MessageModel
834+
{
835+
ProviderName = Constants.TestProviderName,
836+
ShortId = 1000,
837+
Text = "Property1: %1, Property2: %2, Property3: %3"
838+
}
839+
],
840+
Parameters = [],
841+
Keywords = new Dictionary<long, string>(),
842+
Tasks = new Dictionary<int, string>()
843+
};
844+
845+
var resolver = new TestEventResolver([providerDetails]);
846+
847+
var eventRecord = new EventRecord
848+
{
849+
ProviderName = Constants.TestProviderName,
850+
Id = 1000,
851+
Properties = ["Value1", "Value2"] // Only 2 properties, but template expects 3
852+
};
853+
854+
// Act
855+
var displayEvent = resolver.ResolveEvent(eventRecord);
856+
857+
// Assert
858+
Assert.NotNull(displayEvent);
859+
Assert.Contains("Failed to resolve", displayEvent.Description);
860+
}
861+
782862
private class TestEventResolver : EventResolverBase, IEventResolver
783863
{
784864
public TestEventResolver(IEventResolverCache? cache = null, ITraceLogger? logger = null)

src/EventLogExpert.Eventing/EventResolvers/EventResolverBase.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,15 @@ private string FormatDescription(
371371

372372
if (!propString.StartsWith("%%") && int.TryParse(propString.Trim(['{', '}', '%']), out var propIndex))
373373
{
374-
if (propIndex - 1 >= properties.Count)
374+
// %0 is a Windows Event Log message terminator - skip it entirely
375+
if (propIndex == 0)
376+
{
377+
lastIndex = match.Index + match.Length;
378+
379+
continue;
380+
}
381+
382+
if (propIndex > properties.Count)
375383
{
376384
Logger?.Trace($"{nameof(FormatDescription)}: Property index out of range - RequestedIndex={propIndex}, PropertyCount={properties.Count}, Template={descriptionTemplate}",
377385
LogLevel.Warning);

0 commit comments

Comments
 (0)