AsciiString contentEqualsIgnoreCase fails when arrayOffset is non-zero (#9477)

Motivation:

AsciiString.contentEqualsIgnoreCase may return true for non-matching strings of equal length when offset is non zero.

Modifications:

- Correctly take offset into account
- Add unit test

Result: 

Fixes #9475
This commit is contained in:
Antony T Curtis 2019-08-17 00:56:39 -07:00 committed by Norman Maurer
parent 6f616bb3cf
commit 188c927576
2 changed files with 11 additions and 2 deletions

View File

@ -532,7 +532,7 @@ public final class AsciiString implements CharSequence, Comparable<CharSequence>
if (string instanceof AsciiString) {
AsciiString rhs = (AsciiString) string;
for (int i = arrayOffset(), j = rhs.arrayOffset(); i < length(); ++i, ++j) {
for (int i = arrayOffset(), j = rhs.arrayOffset(), end = i + length(); i < end; ++i, ++j) {
if (!equalsIgnoreCase(value[i], rhs.value[j])) {
return false;
}
@ -540,7 +540,7 @@ public final class AsciiString implements CharSequence, Comparable<CharSequence>
return true;
}
for (int i = arrayOffset(), j = 0; i < length(); ++i, ++j) {
for (int i = arrayOffset(), j = 0, end = length(); j < end; ++i, ++j) {
if (!equalsIgnoreCase(b2c(value[i]), string.charAt(j))) {
return false;
}

View File

@ -46,6 +46,15 @@ public class AsciiStringCharacterTest {
private static final Charset[] CHARSETS = new Charset[]
{ UTF_16, UTF_16BE, UTF_16LE, UTF_8, ISO_8859_1, US_ASCII };
@Test
public void testContentEqualsIgnoreCase() {
byte[] bytes = { 32, 'a' };
AsciiString asciiString = new AsciiString(bytes, 1, 1, false);
// https://github.com/netty/netty/issues/9475
assertFalse(asciiString.contentEqualsIgnoreCase("b"));
assertFalse(asciiString.contentEqualsIgnoreCase(AsciiString.of("b")));
}
@Test
public void testGetBytesStringBuilder() {
final StringBuilder b = new StringBuilder();