Fix a bug for counting code points in WordComposer.java

This bug threw an ArrayIndexOutOfBoundsException when the word length
is 49 (maxSize + 1) when calling StringUtils.copyCodePointsAndReturnCodePointCount(...)
in the same function. This bug is discovered by running SKETCH.

The intent is to count the code points from index 0 to index i (included).
The original code only counted the code points from index 0 to index (i-1).

Bug: 13969542
Change-Id: Idbf596aba2379ba552dbe580c83c42044d505aaf
main
Xiaojun Bi 2014-04-18 10:52:15 -07:00
parent 7cb0cc1b29
commit 11b7076168
2 changed files with 11 additions and 8 deletions

View File

@ -260,7 +260,7 @@ public final class BinaryDictionary extends Dictionary {
final int inputSize; final int inputSize;
if (!isGesture) { if (!isGesture) {
inputSize = composer.copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount( inputSize = composer.copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount(
mInputCodePoints, MAX_WORD_LENGTH); mInputCodePoints);
if (inputSize < 0) { if (inputSize < 0) {
return null; return null;
} }

View File

@ -129,22 +129,25 @@ public final class WordComposer {
* -1 is returned. * -1 is returned.
* *
* @param destination the array of ints. * @param destination the array of ints.
* @param maxSize the size of the array.
* @return the number of copied code points. * @return the number of copied code points.
*/ */
public int copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount( public int copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount(
final int[] destination, final int maxSize) { final int[] destination) {
final int i = mTypedWordCache.length() - 1 - trailingSingleQuotesCount(); // lastIndex is exclusive
if (i < 0) { final int lastIndex = mTypedWordCache.length() - trailingSingleQuotesCount();
if (lastIndex <= 0) {
// The string is empty or contains only single quotes. // The string is empty or contains only single quotes.
return 0; return 0;
} }
final int codePointSize = Character.codePointCount(mTypedWordCache, 0, i);
if (codePointSize > maxSize) { // The following function counts the number of code points in the text range which begins
// at index 0 and extends to the character at lastIndex.
final int codePointSize = Character.codePointCount(mTypedWordCache, 0, lastIndex);
if (codePointSize > destination.length) {
return -1; return -1;
} }
return StringUtils.copyCodePointsAndReturnCodePointCount(destination, mTypedWordCache, 0, return StringUtils.copyCodePointsAndReturnCodePointCount(destination, mTypedWordCache, 0,
i + 1, true /* downCase */); lastIndex, true /* downCase */);
} }
public boolean isSingleLetter() { public boolean isSingleLetter() {