Fix AIOOBE

Bug: 6236912
Change-Id: Ie09e5ef1c23eb48621ac3f2f2dc28dc2e46ca288
This commit is contained in:
satok 2012-03-28 11:32:11 +09:00
parent 8d16a0c9a1
commit 9611b281e1
3 changed files with 20 additions and 15 deletions

View file

@ -28,17 +28,12 @@ import java.util.LinkedList;
* be searched for suggestions and valid words. * be searched for suggestions and valid words.
*/ */
public class ExpandableDictionary extends Dictionary { public class ExpandableDictionary extends Dictionary {
/**
* There is difference between what java and native code can handle.
* It uses 32 because Java stack overflows when greater value is used.
*/
protected static final int MAX_WORD_LENGTH = 32;
// Bigram frequency is a fixed point number with 1 meaning 1.2 and 255 meaning 1.8. // Bigram frequency is a fixed point number with 1 meaning 1.2 and 255 meaning 1.8.
protected static final int BIGRAM_MAX_FREQUENCY = 255; protected static final int BIGRAM_MAX_FREQUENCY = 255;
private Context mContext; private Context mContext;
private char[] mWordBuilder = new char[MAX_WORD_LENGTH]; private char[] mWordBuilder = new char[BinaryDictionary.MAX_WORD_LENGTH];
private int mDicTypeId; private int mDicTypeId;
private int mMaxDepth; private int mMaxDepth;
private int mInputLength; private int mInputLength;
@ -113,7 +108,7 @@ public class ExpandableDictionary extends Dictionary {
public ExpandableDictionary(Context context, int dicTypeId) { public ExpandableDictionary(Context context, int dicTypeId) {
mContext = context; mContext = context;
clearDictionary(); clearDictionary();
mCodes = new int[MAX_WORD_LENGTH][]; mCodes = new int[BinaryDictionary.MAX_WORD_LENGTH][];
mDicTypeId = dicTypeId; mDicTypeId = dicTypeId;
} }
@ -151,10 +146,13 @@ public class ExpandableDictionary extends Dictionary {
} }
public int getMaxWordLength() { public int getMaxWordLength() {
return MAX_WORD_LENGTH; return BinaryDictionary.MAX_WORD_LENGTH;
} }
public void addWord(String word, int frequency) { public void addWord(String word, int frequency) {
if (word.length() >= BinaryDictionary.MAX_WORD_LENGTH) {
return;
}
addWordRec(mRoots, word, 0, frequency, null); addWordRec(mRoots, word, 0, frequency, null);
} }
@ -201,6 +199,9 @@ public class ExpandableDictionary extends Dictionary {
// Currently updating contacts, don't return any results. // Currently updating contacts, don't return any results.
if (mUpdatingDictionary) return; if (mUpdatingDictionary) return;
} }
if (codes.size() >= BinaryDictionary.MAX_WORD_LENGTH) {
return;
}
getWordsInner(codes, callback, proximityInfo); getWordsInner(codes, callback, proximityInfo);
} }
@ -488,7 +489,7 @@ public class ExpandableDictionary extends Dictionary {
} }
// Local to reverseLookUp, but do not allocate each time. // Local to reverseLookUp, but do not allocate each time.
private final char[] mLookedUpString = new char[MAX_WORD_LENGTH]; private final char[] mLookedUpString = new char[BinaryDictionary.MAX_WORD_LENGTH];
/** /**
* reverseLookUp retrieves the full word given a list of terminal nodes and adds those words * reverseLookUp retrieves the full word given a list of terminal nodes and adds those words
@ -502,15 +503,15 @@ public class ExpandableDictionary extends Dictionary {
for (NextWord nextWord : terminalNodes) { for (NextWord nextWord : terminalNodes) {
node = nextWord.mWord; node = nextWord.mWord;
freq = nextWord.getFrequency(); freq = nextWord.getFrequency();
int index = MAX_WORD_LENGTH; int index = BinaryDictionary.MAX_WORD_LENGTH;
do { do {
--index; --index;
mLookedUpString[index] = node.mCode; mLookedUpString[index] = node.mCode;
node = node.mParent; node = node.mParent;
} while (node != null); } while (node != null);
callback.addWord(mLookedUpString, index, MAX_WORD_LENGTH - index, freq, mDicTypeId, callback.addWord(mLookedUpString, index, BinaryDictionary.MAX_WORD_LENGTH - index,
Dictionary.BIGRAM); freq, mDicTypeId, Dictionary.BIGRAM);
} }
} }

View file

@ -247,8 +247,8 @@ public class UserHistoryDictionary extends ExpandableDictionary {
// to recursive lookup // to recursive lookup
if (null == word1) { if (null == word1) {
super.addWord(word2, frequency); super.addWord(word2, frequency);
} else if (word1.length() < MAX_WORD_LENGTH } else if (word1.length() < BinaryDictionary.MAX_WORD_LENGTH
&& word2.length() < MAX_WORD_LENGTH) { && word2.length() < BinaryDictionary.MAX_WORD_LENGTH) {
super.setBigram(word1, word2, frequency); super.setBigram(word1, word2, frequency);
} }
cursor.moveToNext(); cursor.moveToNext();

View file

@ -104,7 +104,11 @@ public class WordComposer {
return size() > 0; return size() > 0;
} }
// TODO: make sure that the index should not exceed MAX_WORD_LENGTH
public int getCodeAt(int index) { public int getCodeAt(int index) {
if (index >= BinaryDictionary.MAX_WORD_LENGTH) {
return -1;
}
return mPrimaryKeyCodes[index]; return mPrimaryKeyCodes[index];
} }
@ -153,8 +157,8 @@ public class WordComposer {
final int newIndex = size(); final int newIndex = size();
mTypedWord.appendCodePoint(primaryCode); mTypedWord.appendCodePoint(primaryCode);
refreshSize(); refreshSize();
mPrimaryKeyCodes[newIndex] = codes[0];
if (newIndex < BinaryDictionary.MAX_WORD_LENGTH) { if (newIndex < BinaryDictionary.MAX_WORD_LENGTH) {
mPrimaryKeyCodes[newIndex] = codes[0];
mXCoordinates[newIndex] = keyX; mXCoordinates[newIndex] = keyX;
mYCoordinates[newIndex] = keyY; mYCoordinates[newIndex] = keyY;
} }