1

By running a simple code that run a command on remote server, below:

if (s.contains("CONNECTED") || s.endsWith("Enter command: ") || s.endsWith(":")) {
    //login command
    dataOutput.writeBytes(LoginCommand + "\r\n");
    Arrays.fill(line, '\n');

    lineSize = dataInput.read(line);
    s = String.copyValueOf(line, 0, lineSize);
    result = Integer.parseInt(s.substring(s.indexOf("RESP:") + 5, s.indexOf(";")));

    if (result == 0) {
        result = -101;
        //execution command
        String TelnetCommand = executedCommand.replace("%par1%", par1).replace("%par2%", par2).replace("%par3%", par3);
        dataOutput.writeBytes(TelnetCommand + "\r\n");
        int iCounter = 0;

    }

    connectionTrials = Integer.parseInt(properties.getProperty("NumberOfRetries"));
}

I am getting the below exception:

java.lang.StringIndexOutOfBoundsException: String index out of range: -5
    at java.lang.String.substring(String.java:1911)
    at com.test.tool.Creation.doGet(Creation.java:85)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:808)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:517)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:499)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
    at java.lang.Thread.run(Thread.java:722)

Noting that I tried the solutions mentioned here and in the official document but with no success.

P.S: the initial value of variable result is:

int result = -100;
Eng7
  • 632
  • 1
  • 8
  • 25

1 Answers1

1

You are manipulating your input without making sure it matches your expectation. It's always highly recommended to validate any input before processing them.

A very simple way is to use regular expression.

static final Pattern IS_CONNECTED = Pattern.compile("\\bCONNECTED\\b|\\bEnter command: $|:$");
static final Pattern RESPONSE = Pattern.compile("\\bRESP:(?<code>\\d+);")

if (IS_CONNECTED.matcher(s).matches()) {
    // ...
    Matcher matching_response = RESPONSE.matcher(s);
    if (matching_response.matches()) {
       String code_str = matching_response.group("code");
       result = Integer.parseInt(code_str); // Poor check, can still fail
    }
}

In your specific case, you have throw new StringIndexOutOfBoundsException(-5) because both RESP: and ; have not been found. Meaning you'are doing:

s.substring(s.indexOf("RESP:") + 5, s.indexOf(";"));
s.substring(-1 + 5, -1);
s.substring(/*beginIndex*/ 4, /*endIndex*/ -1);

Then substring compute length as:

sublength = -1 - 4
sublength = -5

As sublength is negative, it throw new StringIndexOutOfBoundsException(-5)

LoganMzz
  • 1,597
  • 3
  • 18
  • 31