Skip to content

Commit bce0f6a

Browse files
committed
[CLI-341] HelpFormatter infinite loop with 0 width input
1 parent 1328d05 commit bce0f6a

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<action type="fix" dev="ggregory" due-to="Arnout Engelen">Get mockito version from parent pom (#351).</action>
3535
<action type="fix" dev="ggregory" due-to="Gary Gregory">Remove -nouses directive from maven-bundle-plugin. OSGi package imports now state 'uses' definitions for package imports, this doesn't affect JPMS (from org.apache.commons:commons-parent:80).</action>
3636
<action type="fix" dev="ggregory" due-to="Arnout Engelen">Deprecate PatternOptionBuilder.PatternOptionBuilder().</action>
37+
<action type="fix" issue="CLI-341" dev="ggregory" due-to="Gary Gregory">HelpFormatter infinite loop with 0 width input.</action>
3738
<!-- ADD -->
3839
<action type="add" issue="CLI-339" dev="ggregory" due-to="Claude Warren, Gary Gregory">Help formatter extension in the new package #314.</action>
3940
<action type="add" dev="ggregory" due-to="Gary Gregory">CommandLine.Builder implements Supplier&lt;CommandLine&gt;.</action>

src/main/java/org/apache/commons/cli/HelpFormatter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,9 @@ <A extends Appendable> A appendOptions(final A sb, final int width, final Option
499499
* @throws IOException if an I/O error occurs.
500500
*/
501501
<A extends Appendable> A appendWrappedText(final A appendable, final int width, final int nextLineTabStop, final String text) throws IOException {
502+
if (width <= 0) {
503+
return appendable;
504+
}
502505
String render = text;
503506
int nextLineTabStopPos = nextLineTabStop;
504507
int pos = findWrapPos(render, width, 0);

src/test/java/org/apache/commons/cli/HelpFormatterTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1919

2020
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2223
import static org.junit.jupiter.api.Assertions.assertNull;
2324
import static org.junit.jupiter.api.Assertions.assertThrows;
2425
import static org.mockito.Mockito.spy;
@@ -44,6 +45,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
4445
* Test case for the HelpFormatter class.
4546
*/
4647
class HelpFormatterTest {
48+
4749
private static final String EOL = System.lineSeparator();
4850

4951
static Stream<Arguments> deprecatedOptionsProvider() {
@@ -152,6 +154,23 @@ void testDefaultArgName() {
152154
assertEquals("usage: app -f <argument>" + EOL, out.toString());
153155
}
154156

157+
@Test
158+
public void testDeprecatedFindWrapPosZeroWidth() {
159+
final int pos = new HelpFormatter().findWrapPos("Hello World", 0, 0);
160+
assertEquals(0, pos);
161+
}
162+
163+
@Test
164+
public void testDeprecatedPrintOptionsZeroWidth() {
165+
final Options options = new Options();
166+
options.addOption("h", "help", false, "Show help");
167+
final StringWriter out = new StringWriter();
168+
final PrintWriter pw = new PrintWriter(out);
169+
new HelpFormatter().printOptions(pw, 0, options, 1, 3);
170+
final String result = out.toString();
171+
assertNotNull(result);
172+
}
173+
155174
@Test
156175
void testFindWrapPos() {
157176
final HelpFormatter hf = new HelpFormatter();

0 commit comments

Comments
 (0)