Fix length calculation in AsciiString.indexOf(...) and so eliminate ArrayIndexOutOfBoundsException. (#8116)

Motivation:

We incorrectly calculated the length that was used for our for loop in AsciiString.indexOf(...). This lead to a possible ArrayIndexOutOfBoundsException.

Modifications:

- Not include the start in the length calculation
- Add unit test.

Result:

Fixes https://github.com/netty/netty/issues/8112.
This commit is contained in:
Norman Maurer 2018-07-11 10:21:17 +01:00 committed by GitHub
parent 93d2807ff0
commit 8186c9aaea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -722,7 +722,7 @@ public final class AsciiString implements CharSequence, Comparable<CharSequence>
} }
final byte chAsByte = c2b0(ch); final byte chAsByte = c2b0(ch);
final int len = offset + start + length; final int len = offset + length;
for (int i = start + offset; i < len; ++i) { for (int i = start + offset; i < len; ++i) {
if (value[i] == chAsByte) { if (value[i] == chAsByte) {
return i - offset; return i - offset;

View File

@ -398,4 +398,18 @@ public class AsciiStringCharacterTest {
//two "123"s //two "123"s
assertEquals(AsciiString.hashCode("123"), AsciiString.hashCode("a123".substring(1))); assertEquals(AsciiString.hashCode("123"), AsciiString.hashCode("a123".substring(1)));
} }
@Test
public void testIndexOf() {
AsciiString foo = AsciiString.of("This is a test");
int i1 = foo.indexOf(' ', 0);
assertEquals(4, i1);
int i2 = foo.indexOf(' ', i1 + 1);
assertEquals(7, i2);
int i3 = foo.indexOf(' ', i2 + 1);
assertEquals(9, i3);
assertTrue(i3 + 1 < foo.length());
int i4 = foo.indexOf(' ', i3 + 1);
assertEquals(i4, -1);
}
} }