Skip to content

Commit 9f9944d

Browse files
jschick04NikTilton
authored andcommitted
Fixed index out of range issue when event contains a trailing %n
1 parent c7645e4 commit 9f9944d

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,45 @@ public void ResolveEvent_WithFormattingCharacters_ShouldCleanupDescription()
170170
Assert.Contains("\t", displayEvent.Description);
171171
}
172172

173+
[Fact]
174+
public void ResolveEvent_WithTrailingPercentN_ShouldNotThrowIndexOutOfRange()
175+
{
176+
// Arrange
177+
var providerDetails = new ProviderDetails
178+
{
179+
ProviderName = Constants.TestProviderName,
180+
Events = [],
181+
Messages =
182+
[
183+
new MessageModel
184+
{
185+
ProviderName = Constants.TestProviderName,
186+
ShortId = 1001,
187+
Text = "Message ends with newline%n"
188+
}
189+
],
190+
Parameters = [],
191+
Keywords = new Dictionary<long, string>(),
192+
Tasks = new Dictionary<int, string>()
193+
};
194+
195+
var resolver = new TestEventResolver([providerDetails]);
196+
197+
var eventRecord = new EventRecord
198+
{
199+
ProviderName = Constants.TestProviderName,
200+
Id = 1001,
201+
Properties = []
202+
};
203+
204+
// Act - should not throw IndexOutOfRangeException
205+
var displayEvent = resolver.ResolveEvent(eventRecord);
206+
207+
// Assert
208+
Assert.NotNull(displayEvent);
209+
Assert.EndsWith("\r\n", displayEvent.Description);
210+
}
211+
173212
[Fact]
174213
public void ResolveEvent_WithHexPropertyType_ShouldFormatAsHex()
175214
{

src/EventLogExpert.Eventing/EventResolvers/EventResolverBase.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,18 @@ private static void CleanupFormatting(ReadOnlySpan<char> unformattedString, ref
134134
switch (unformattedString[i])
135135
{
136136
case '%' when i + 1 < unformattedString.Length:
137-
switch (unformattedString[i + 1])
138-
{
139-
case 'n':
140-
if (unformattedString[i + 2] != '\r')
141-
{
142-
buffer[bufferIndex++] = '\r';
143-
buffer[bufferIndex++] = '\n';
144-
}
137+
switch (unformattedString[i + 1])
138+
{
139+
case 'n':
140+
if (i + 2 >= unformattedString.Length || unformattedString[i + 2] != '\r')
141+
{
142+
buffer[bufferIndex++] = '\r';
143+
buffer[bufferIndex++] = '\n';
144+
}
145145

146-
i++;
146+
i++;
147147

148-
break;
148+
break;
149149
case 't':
150150
buffer[bufferIndex++] = '\t';
151151
i++;

0 commit comments

Comments
 (0)