Skip to content

Commit

Permalink
Infinite loop in TerminalLine constructor, fixes #751
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jun 3, 2022
1 parent 8b89ff5 commit 4dac9b0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2021, the original author or authors.
* Copyright (c) 2002-2022, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -5137,7 +5137,7 @@ public TerminalLine(String line, int startPos, int width) {
this.startPos = startPos;
endLine = line.substring(line.lastIndexOf('\n') + 1);
boolean first = true;
while (endLine.length() + (first ? startPos : 0) > width) {
while (endLine.length() + (first ? startPos : 0) > width && width > 0) {
if (first) {
endLine = endLine.substring(width - startPos);
} else {
Expand Down
23 changes: 22 additions & 1 deletion reader/src/test/java/org/jline/reader/impl/LineReaderTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2018, the original author or authors.
* Copyright (c) 2002-2022, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -28,6 +28,7 @@
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -164,4 +165,24 @@ public void testPreferAppNameFromConstructor() throws IOException {
assertEquals("Did not prefer appName from builder",
expectedAppName, lineReader.getAppName());
}

@Test
public void terminalLineInfiniteLoop() throws IOException
{
ByteArrayInputStream in = new ByteArrayInputStream( "hello\nworld\n".getBytes( StandardCharsets.UTF_8 ) );
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);

Terminal terminal = TerminalBuilder.builder().streams( in, out ).build();
terminal.setSize(new Size(0, 48));
LineReader lineReader = LineReaderBuilder.builder()
.terminal( terminal )
.variable( LineReader.SECONDARY_PROMPT_PATTERN, "%P " )
.build();

String read1 = lineReader.readLine("Input1: ");
String read2 = lineReader.readLine("Input2: ");

assertEquals( "hello", read1 );
assertEquals( "world", read2 );
}
}

0 comments on commit 4dac9b0

Please sign in to comment.