Fix a crash when MAX_WORD_LENGTH is too short.

Change-Id: Idcb5aa2685321b8d0ac7d846caecbd1c79e4dd77
main
satok 2010-12-06 21:28:24 +09:00
parent 48e432ceb8
commit f5cded1c6c
4 changed files with 23 additions and 12 deletions

View File

@ -170,12 +170,12 @@ public class BinaryDictionary extends Dictionary {
mOutputChars_bigrams, mFrequencies_bigrams, MAX_WORD_LENGTH, MAX_BIGRAMS,
MAX_ALTERNATIVES);
for (int j = 0; j < count; j++) {
for (int j = 0; j < count; ++j) {
if (mFrequencies_bigrams[j] < 1) break;
int start = j * MAX_WORD_LENGTH;
final int start = j * MAX_WORD_LENGTH;
int len = 0;
while (mOutputChars_bigrams[start + len] != 0) {
len++;
while (len < MAX_WORD_LENGTH && mOutputChars_bigrams[start + len] != 0) {
++len;
}
if (len > 0) {
callback.addWord(mOutputChars_bigrams, start, len, mFrequencies_bigrams[j],
@ -204,12 +204,12 @@ public class BinaryDictionary extends Dictionary {
mFrequencies, nextLettersFrequencies,
nextLettersFrequencies != null ? nextLettersFrequencies.length : 0);
for (int j = 0; j < count; j++) {
for (int j = 0; j < count; ++j) {
if (mFrequencies[j] < 1) break;
int start = j * MAX_WORD_LENGTH;
final int start = j * MAX_WORD_LENGTH;
int len = 0;
while (mOutputChars[start + len] != 0) {
len++;
while (len < MAX_WORD_LENGTH && mOutputChars[start + len] != 0) {
++len;
}
if (len > 0) {
callback.addWord(mOutputChars, start, len, mFrequencies[j], mDicTypeId,

View File

@ -146,7 +146,7 @@ void BigramDictionary::searchForTerminalNode(int addressLookingFor, int frequenc
bool firstAddress = true;
bool haveToSearchAll = true;
if (depth >= 0) {
if (depth < MAX_WORD_LENGTH && depth >= 0) {
word[depth] = (unsigned short) followingChar;
}
pos = followDownBranchAddress; // pos start at count

View File

@ -50,8 +50,12 @@
#define SUGGEST_MISSING_CHARACTERS true
#define SUGGEST_MISSING_CHARACTERS_THRESHOLD 5
#define MAX_WORD_LENGTH_INTERNAL 64
// This should be greater than or equal to MAX_WORD_LENGTH defined in BinaryDictionary.java
// This is only used for the size of array. Not to be used in c functions.
#define MAX_WORD_LENGTH_INTERNAL 48
#define MAX_DEPTH_MULTIPLIER 3
#define min(a,b) ((a)<(b)?(a):(b))
#endif // LATINIME_DEFINES_H

View File

@ -16,8 +16,8 @@
*/
#include <assert.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#define LOG_TAG "LatinIME: unigram_dictionary.cpp"
@ -78,6 +78,7 @@ int UnigramDictionary::getSuggestions(int *codes, int codesSize, unsigned short
void UnigramDictionary::initSuggestions(int *codes, int codesSize, unsigned short *outWords,
int *frequencies) {
if (DEBUG_DICT) LOGI("initSuggest");
mFrequencies = frequencies;
mOutputChars = outWords;
mInputCodes = codes;
@ -87,6 +88,7 @@ void UnigramDictionary::initSuggestions(int *codes, int codesSize, unsigned shor
int UnigramDictionary::getSuggestionCandidates(int inputLength, int skipPos,
int *nextLetters, int nextLettersSize) {
if (DEBUG_DICT) LOGI("getSuggestionCandidates");
int initialPos = 0;
if (IS_LATEST_DICT_VERSION) {
initialPos = DICTIONARY_HEADER_SIZE;
@ -115,6 +117,10 @@ bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency)
for (int i = 0; i <= length; i++) s[i] = word[i];
LOGI("Found word = %s, freq = %d : \n", s, frequency);
}
if (length > MAX_WORD_LENGTH) {
if (DEBUG_DICT) LOGI("Exceeded max word length.");
return false;
}
// Find the right insertion point
int insertAt = 0;
@ -177,7 +183,8 @@ void UnigramDictionary::getWords(const int initialPos, const int inputLength, co
int *nextLetters, const int nextLettersSize) {
int initialPosition = initialPos;
const int count = Dictionary::getCount(DICT, &initialPosition);
getWordsRec(count, initialPosition, 0, inputLength * MAX_DEPTH_MULTIPLIER,
getWordsRec(count, initialPosition, 0,
min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH),
mInputLength <= 0, 1, 0, 0, skipPos, nextLetters, nextLettersSize);
}