Skip to content

Commit

Permalink
Fixed an out of range exception in the description formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
jschick04 authored and bill-long committed Aug 2, 2024
1 parent f3c3343 commit 1e30a39
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/EventLogExpert.Eventing/EventResolvers/EventResolverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,21 @@ protected string FormatDescription(
"Unable to resolve description, see XML for more details.";
}

var matches = _sectionsToReplace.Matches(descriptionTemplate);

ReadOnlySpan<char> description = descriptionTemplate
.Replace("\r\n%n", " \r\n")
.Replace("%n\r\n", "\r\n ")
.Replace("%n", "\r\n");

if (matches.Count <= 0)
{
return description.TrimEnd(['\0', '\r', '\n']).ToString();
}

try
{
StringBuilder updatedDescription = new();
int lastIndex = 0;

for (int i = 0; i < matches.Count; i++)
foreach (var match in _sectionsToReplace.EnumerateMatches(description))
{
updatedDescription.Append(description[lastIndex..matches[i].Index]);
updatedDescription.Append(description[lastIndex..match.Index]);

ReadOnlySpan<char> propString = matches[i].Value;
ReadOnlySpan<char> propString = description[match.Index..(match.Index + match.Length)];

if (!propString.StartsWith("%%"))
{
Expand Down Expand Up @@ -158,7 +151,7 @@ protected string FormatDescription(

updatedDescription.Append(propString);

lastIndex = matches[i].Index + matches[i].Length;
lastIndex = match.Index + match.Length;
}

if (lastIndex < description.Length)
Expand All @@ -168,6 +161,11 @@ protected string FormatDescription(

return updatedDescription.ToString();
}
catch (InvalidOperationException)
{
// If the regex fails to match, then we just return the original description.
return description.TrimEnd(['\0', '\r', '\n']).ToString();
}
catch (Exception ex)
{
_tracer($"FormatDescription exception was caught: {ex}", LogLevel.Information);
Expand Down

0 comments on commit 1e30a39

Please sign in to comment.