Skip to content

Commit 95ded2d

Browse files
committed
Fix infinite loop in HelpText.Addline; add unit test + validation
1 parent caf0ca3 commit 95ded2d

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/CommandLine/Text/HelpText.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public SentenceBuilder SentenceBuilder
206206
public static HelpText AutoBuild<T>(
207207
ParserResult<T> parserResult,
208208
Func<HelpText, HelpText> onError,
209-
Func<Example, Example> onExample,
209+
Func<Example, Example> onExample,
210210
bool verbsIndex = false,
211211
int maxDisplayWidth = DefaultMaximumLength)
212212
{
@@ -254,7 +254,7 @@ public static HelpText AutoBuild<T>(
254254

255255
usageAttr.Do(
256256
usage => usage.AddToHelpText(auto, true));
257-
257+
258258
usageLines.Do(
259259
lines => auto.AddPreOptionsLines(lines));
260260

@@ -519,7 +519,7 @@ public static IEnumerable<string> RenderParsingErrorsTextAsLines<T>(
519519
yield return line.ToString();
520520
}
521521

522-
var mutuallyErrs =
522+
var mutuallyErrs =
523523
formatMutuallyExclusiveSetErrors(
524524
meaningfulErrors.OfType<MutuallyExclusiveSetError>());
525525
if (mutuallyErrs.Length > 0)
@@ -619,8 +619,25 @@ public override string ToString()
619619
.ToString();
620620
}
621621

622-
private static void AddLine(StringBuilder builder, string value, int maximumLength)
622+
internal static void AddLine(StringBuilder builder, string value, int maximumLength)
623623
{
624+
if (builder == null)
625+
{
626+
throw new ArgumentNullException(nameof(builder));
627+
}
628+
629+
if (value == null)
630+
{
631+
throw new ArgumentNullException(nameof(value));
632+
}
633+
634+
if (maximumLength < 1)
635+
{
636+
throw new ArgumentOutOfRangeException(nameof(value));
637+
}
638+
639+
value = value.Trim();
640+
624641
builder.AppendWhen(builder.Length > 0, Environment.NewLine);
625642
do
626643
{

tests/CommandLine.Tests/Unit/Text/HelpTextTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using CommandLine.Text;
1313
using FluentAssertions;
1414
using Xunit;
15+
using System.Text;
1516

1617
namespace CommandLine.Tests.Unit.Text
1718
{
@@ -657,5 +658,16 @@ public void AutoBuild_with_assembly_company_attribute_only()
657658
ReflectionHelper.SetAttributeOverride(null);
658659
}
659660
}
661+
662+
[Fact]
663+
public void Add_line_with_two_empty_spaces_at_the_end()
664+
{
665+
StringBuilder b = new StringBuilder();
666+
HelpText.AddLine(b,
667+
"Test ",
668+
1);
669+
670+
Assert.Equal("T" + Environment.NewLine + "e" + Environment.NewLine + "s" + Environment.NewLine + "t", b.ToString());
671+
}
660672
}
661673
}

0 commit comments

Comments
 (0)