LUCENE-5042: Refuse to convert if the length is negative.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1492543 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrien Grand 2013-06-13 07:06:15 +00:00
parent 14d7d11d59
commit e7cdd1959f
2 changed files with 7 additions and 3 deletions

View File

@ -135,6 +135,9 @@ public abstract class CharacterUtils {
/** Converts a sequence of Java characters to a sequence of unicode code points.
* @return the number of code points written to the destination buffer */
public final int toCodePoints(char[] src, int srcOff, int srcLen, int[] dest, int destOff) {
if (srcLen < 0) {
throw new IllegalArgumentException("srcLen must be >= 0");
}
int codePointCount = 0;
for (int i = 0; i < srcLen; ) {
final int cp = codePointAt(src, srcOff + i, srcOff + srcLen);
@ -148,6 +151,9 @@ public abstract class CharacterUtils {
/** Converts a sequence of unicode code points to a sequence of Java characters.
* @return the number of chars written to the destination buffer */
public final int toChars(int[] src, int srcOff, int srcLen, char[] dest, int destOff) {
if (srcLen < 0) {
throw new IllegalArgumentException("srcLen must be >= 0");
}
int written = 0;
for (int i = 0; i < srcLen; ++i) {
written += Character.toChars(src[srcOff + i], dest, destOff + written);

View File

@ -23,9 +23,7 @@ import java.io.StringReader;
import java.util.Arrays;
import org.apache.lucene.analysis.util.CharacterUtils.CharacterBuffer;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.Version;
import org.apache.lucene.util._TestUtil;
import org.junit.Test;
@ -130,7 +128,7 @@ public class TestCharacterUtils extends LuceneTestCase {
final char[] orig = _TestUtil.randomUnicodeString(random(), 100).toCharArray();
final int[] buf = new int[orig.length];
final char[] restored = new char[buf.length];
final int o1 = random().nextInt(5);
final int o1 = _TestUtil.nextInt(random(), 0, Math.min(5, orig.length));
final int o2 = _TestUtil.nextInt(random(), 0, o1);
final int o3 = _TestUtil.nextInt(random(), 0, o1);
final int codePointCount = charUtils.toCodePoints(orig, o1, orig.length - o1, buf, o2);