Menu

#45 StringIndexOutOfBoundsException when pressing Escape

open
nobody
None
5
2008-06-02
2008-06-02
No

On Windows (using the Scala interpreter):

* type one char (or more) on an empty command line
* go left one char with cursor keys
* press escape

Stack trace:
Exception in thread "Thread-1" java.lang.StringIndexOutOfBoundsException:
String index out of range: -1
at java.lang.AbstractStringBuilder.deleteCharAt(AbstractStringBuilder.java:770)
at java.lang.StringBuffer.deleteCharAt(StringBuffer.java:382)
at jline.ConsoleReader.killLine(ConsoleReader.java:742)
at jline.ConsoleReader.readLine(ConsoleReader.java:569)
at jline.ConsoleReader.readLine(ConsoleReader.java:416)
at scala.tools.nsc.interpreter.JLineReader.readLine(JLineReader.scala:24)
at scala.tools.nsc.InterpreterLoop$$anonfun$1.apply(InterpreterLoop.scala:135)
..

Discussion

  • Charles Oliver Nutter

    I could not reproduce this in JRuby's console (jirb) on OS X. Do not have Windows handy right now.

     
  • Vitaliy Garnashevich

    Here is a test for Windows, to detect this bug:

    --- ConsoleReaderTest.java Fri Mar 9 17:04:54 2007
    +++ ConsoleReaderTest.java Wed Jul 14 14:56:10 2010
    @@ -134,6 +134,19 @@
    assertWindowsKeyBehavior("oops", characters);
    }

    + public void testClearLineWhenNotAtEndOnWindowsTerminal() throws Exception {
    + // test only works on Windows
    + if (!(Terminal.getTerminal() instanceof WindowsTerminal))
    + return;
    +
    + char[] characters = new char[] { '1', '2', '3',
    + WindowsTerminal.SPECIAL_KEY_INDICATOR,
    + WindowsTerminal.LEFT_ARROW_KEY,
    + WindowsTerminal.SPECIAL_KEY_INDICATOR,
    + WindowsTerminal.ESCAPE_KEY, '4', '5', '\r', '\n' };
    + assertWindowsKeyBehavior("45", characters);
    + }
    +
    private void assertWindowsKeyBehavior(String expected, char[] input)
    throws Exception {
    StringBuffer buffer = new StringBuffer();

     
  • Vitaliy Garnashevich

    Here is a patch to fix this:

    --- ConsoleReader.java Fri Feb 22 01:59:14 2008
    +++ ConsoleReader.java Wed Jul 14 15:03:54 2010
    @@ -599,7 +599,7 @@
    break;

    case CLEAR_LINE:
    - moveInternal(-(buf.buffer.length()));
    + moveInternal(-buf.cursor);
    killLine();
    break;

     

Log in to post a comment.