LUCENE-7946: Fix CharTermAttribute.setLength's bounds check

This commit is contained in:
Robert Muir 2017-08-31 22:29:38 -04:00
parent ded726ad82
commit d7379f1dd2
2 changed files with 13 additions and 0 deletions

View File

@ -71,6 +71,9 @@ public class CharTermAttributeImpl extends AttributeImpl implements CharTermAttr
@Override
public final CharTermAttribute setLength(int length) {
if (length < 0) {
throw new IllegalArgumentException("length " + length + " must not be negative");
}
if (length > termBuffer.length)
throw new IllegalArgumentException("length " + length + " exceeds the size of the termBuffer (" + termBuffer.length + ")");
termLength = length;

View File

@ -42,6 +42,16 @@ public class TestCharTermAttributeImpl extends LuceneTestCase {
}
}
public void testSetLength() {
CharTermAttributeImpl t = new CharTermAttributeImpl();
char[] content = "hello".toCharArray();
t.copyBuffer(content, 0, content.length);
IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
t.setLength(-1);
});
assertTrue(expected.getMessage().contains("must not be negative"));
}
public void testGrow() {
CharTermAttributeImpl t = new CharTermAttributeImpl();
StringBuilder buf = new StringBuilder("ab");