Skip to content

Commit

Permalink
Fix possible StringIndexOutOfBoundsException in Buffer.substring, #101
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Mar 13, 2017
1 parent f6559d5 commit 9ec6a45
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion reader/src/main/java/org/jline/reader/impl/BufferImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public String substring(int start, int end) {
if (end <= g0) {
return new String(buffer, start, end - start);
} else if (start > g0) {
return new String(buffer, g1 - g0 + start, g1 - g0 + end - start);
return new String(buffer, g1 - g0 + start, end - start);
} else {
int[] b = buffer.clone();
System.arraycopy(b, g1, b, g0, b.length - g1);
Expand Down
15 changes: 15 additions & 0 deletions reader/src/test/java/org/jline/reader/impl/EditLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.jline.reader.LineReader.BACKWARD_KILL_WORD;
import static org.jline.reader.LineReader.BACKWARD_WORD;
import static org.jline.reader.LineReader.END_OF_LINE;
import static org.jline.reader.LineReader.FORWARD_WORD;
import static org.jline.reader.LineReader.KILL_WORD;

/**
Expand All @@ -27,6 +28,20 @@
public class EditLineTest
extends ReaderTestSupport
{
@Test
public void testIssue101() throws Exception {
TestBuffer b = new TestBuffer("config:property-set --pid org.ops4j.pax.url.mvn org.ops4j.pax.url.mvn.globalChecksumPolicy crash")
.op(BACKWARD_WORD)
.op(BACKWARD_WORD)
.append("odsa odsa ")
.op(BACKWARD_WORD)
.op(BACKWARD_KILL_WORD)
.op(FORWARD_WORD)
.op(FORWARD_WORD)
.op(BACKWARD_KILL_WORD);
assertBuffer("", b);
}

@Test
public void testDeletePreviousWord() throws Exception {
TestBuffer b = new TestBuffer("This is a test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static org.jline.reader.LineReader.COMPLETE_WORD;
import static org.jline.reader.LineReader.DOWN_HISTORY;
import static org.jline.reader.LineReader.END_OF_LINE;
import static org.jline.reader.LineReader.FORWARD_WORD;
import static org.jline.reader.LineReader.KILL_WORD;
import static org.jline.reader.LineReader.UP_HISTORY;
import static org.jline.reader.LineReader.YANK;
Expand Down Expand Up @@ -143,6 +144,7 @@ protected void assertLine(final String expected, final TestBuffer buffer,
private String getKeyForAction(final String key) {
switch (key) {
case BACKWARD_WORD: return "\u001Bb";
case FORWARD_WORD: return "\u001Bf";
case BEGINNING_OF_LINE: return "\033[H";
case END_OF_LINE: return "\u0005";
case KILL_WORD: return "\u001Bd";
Expand Down

0 comments on commit 9ec6a45

Please sign in to comment.