This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-cli.git
The following commit(s) were added to refs/heads/master by this push:
new bce0f6a9 [CLI-341] HelpFormatter infinite loop with 0 width input
bce0f6a9 is described below
commit bce0f6a9899830eaa5161ea6fd2af04d5679257b
Author: Gary D. Gregory <[email protected]>
AuthorDate: Sat Jun 21 16:33:27 2025 -0400
[CLI-341] HelpFormatter infinite loop with 0 width input
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/cli/HelpFormatter.java | 3 +++
.../org/apache/commons/cli/HelpFormatterTest.java | 19 +++++++++++++++++++
3 files changed, 23 insertions(+)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 80256376..40476962 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -34,6 +34,7 @@
<action type="fix" dev="ggregory" due-to="Arnout Engelen">Get mockito
version from parent pom (#351).</action>
<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>
<action type="fix" dev="ggregory" due-to="Arnout Engelen">Deprecate
PatternOptionBuilder.PatternOptionBuilder().</action>
+ <action type="fix" issue="CLI-341" dev="ggregory" due-to="Gary
Gregory">HelpFormatter infinite loop with 0 width input.</action>
<!-- ADD -->
<action type="add" issue="CLI-339" dev="ggregory" due-to="Claude Warren,
Gary Gregory">Help formatter extension in the new package #314.</action>
<action type="add" dev="ggregory" due-to="Gary
Gregory">CommandLine.Builder implements Supplier<CommandLine>.</action>
diff --git a/src/main/java/org/apache/commons/cli/HelpFormatter.java
b/src/main/java/org/apache/commons/cli/HelpFormatter.java
index 04f71090..af57e6e6 100644
--- a/src/main/java/org/apache/commons/cli/HelpFormatter.java
+++ b/src/main/java/org/apache/commons/cli/HelpFormatter.java
@@ -499,6 +499,9 @@ public class HelpFormatter {
* @throws IOException if an I/O error occurs.
*/
<A extends Appendable> A appendWrappedText(final A appendable, final int
width, final int nextLineTabStop, final String text) throws IOException {
+ if (width <= 0) {
+ return appendable;
+ }
String render = text;
int nextLineTabStopPos = nextLineTabStop;
int pos = findWrapPos(render, width, 0);
diff --git a/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
b/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
index 83e84e6e..0d011223 100644
--- a/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
+++ b/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
@@ -19,6 +19,7 @@ package org.apache.commons.cli;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.spy;
@@ -44,6 +45,7 @@ import org.junit.jupiter.params.provider.MethodSource;
* Test case for the HelpFormatter class.
*/
class HelpFormatterTest {
+
private static final String EOL = System.lineSeparator();
static Stream<Arguments> deprecatedOptionsProvider() {
@@ -152,6 +154,23 @@ class HelpFormatterTest {
assertEquals("usage: app -f <argument>" + EOL, out.toString());
}
+ @Test
+ public void testDeprecatedFindWrapPosZeroWidth() {
+ final int pos = new HelpFormatter().findWrapPos("Hello World", 0, 0);
+ assertEquals(0, pos);
+ }
+
+ @Test
+ public void testDeprecatedPrintOptionsZeroWidth() {
+ final Options options = new Options();
+ options.addOption("h", "help", false, "Show help");
+ final StringWriter out = new StringWriter();
+ final PrintWriter pw = new PrintWriter(out);
+ new HelpFormatter().printOptions(pw, 0, options, 1, 3);
+ final String result = out.toString();
+ assertNotNull(result);
+ }
+
@Test
void testFindWrapPos() {
final HelpFormatter hf = new HelpFormatter();