#50 Out of bounds exception in WinstoneResponseWriter

open
nobody
None
5
2013-01-07
2013-01-07
Carey Flichel
No

We encountered this in version 0.9.10, but it still exists.
The write(String, int int) method is broken and can result in a StringIndexOutOfBoundsException if the offset is offset argument is not 0. We worked around this by wrapping the PrintWriter and only using the char[] write method, which does not have the issue.

Below is the problem code and the exception we hit.

public void write(String s, int off, int len) {
super.write(s, off, len);
if (s != null) {
appendByteCount(s.substring(off, len)); // should be s.substring(off, off + len)
}
}

java.lang.StringIndexOutOfBoundsException: String index out of range: -28
at java.lang.String.substring(String.java:1937)
at winstone.WinstoneResponseWriter.write(WinstoneResponseWriter.java:54)
at com.google.gson.stream.JsonWriter.string(JsonWriter.java:538)
at com.google.gson.stream.JsonWriter.value(JsonWriter.java:404)

Discussion