Skip to content

Commit 880a9f6

Browse files
committed
Changing the current OutOfMemoryError to a RuntimeException per CLI-162. A new ticket for the RuntimeException is at CLI-174
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@735257 13f79535-47bb-0310-9956-ffa450edef68
1 parent b20a8e9 commit 880a9f6

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ protected StringBuffer renderWrappedText(StringBuffer sb, int width,
821821

822822
while (true)
823823
{
824+
int lastPos = pos;
824825
text = padding + text.substring(pos).trim();
825826
pos = findWrapPos(text, width, 0);
826827

@@ -829,6 +830,10 @@ protected StringBuffer renderWrappedText(StringBuffer sb, int width,
829830
sb.append(text);
830831

831832
return sb;
833+
} else
834+
if (pos == lastPos)
835+
{
836+
throw new RuntimeException("Text too long for line - throwing exception to avoid infinite loop [CLI-162]: " + text);
832837
}
833838

834839
sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
package org.apache.commons.cli.bug;
20+
21+
import org.apache.commons.cli.CommandLine;
22+
import org.apache.commons.cli.CommandLineParser;
23+
import org.apache.commons.cli.HelpFormatter;
24+
import org.apache.commons.cli.MissingArgumentException;
25+
import org.apache.commons.cli.Option;
26+
import org.apache.commons.cli.Options;
27+
import org.apache.commons.cli.PosixParser;
28+
29+
import junit.framework.TestCase;
30+
31+
public class BugCLI162Test extends TestCase {
32+
33+
private Options options;
34+
35+
public void setUp() {
36+
options = new Options();
37+
options.addOption("h", "help", false, "This is a looooong description");
38+
}
39+
40+
public void testInfiniteLoop() {
41+
HelpFormatter formatter = new HelpFormatter();
42+
formatter.setWidth(20);
43+
try {
44+
formatter.printHelp("app", options); // hang & crash
45+
} catch(RuntimeException re) {
46+
assertTrue(re.getMessage().startsWith("Text too long for line - throwing exception to avoid infinite loop [CLI-162]: "));
47+
}
48+
}
49+
50+
}

0 commit comments

Comments
 (0)