Merge "Use sizeof() more safely."

main
Ken Wakasa 2012-11-01 01:55:02 -07:00 committed by Android (Google) Code Review
commit d42af9da10
5 changed files with 18 additions and 18 deletions

View File

@ -70,7 +70,7 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jobject object,
adjust = static_cast<int>(dictOffset) % pagesize; adjust = static_cast<int>(dictOffset) % pagesize;
int adjDictOffset = static_cast<int>(dictOffset) - adjust; int adjDictOffset = static_cast<int>(dictOffset) - adjust;
int adjDictSize = static_cast<int>(dictSize) + adjust; int adjDictSize = static_cast<int>(dictSize) + adjust;
dictBuf = mmap(0, sizeof(char) * adjDictSize, PROT_READ, MAP_PRIVATE, fd, adjDictOffset); dictBuf = mmap(0, adjDictSize, PROT_READ, MAP_PRIVATE, fd, adjDictOffset);
if (dictBuf == MAP_FAILED) { if (dictBuf == MAP_FAILED) {
AKLOGE("DICT: Can't mmap dictionary. errno=%d", errno); AKLOGE("DICT: Can't mmap dictionary. errno=%d", errno);
return 0; return 0;
@ -84,7 +84,7 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jobject object,
AKLOGE("DICT: Can't fopen sourceDir. sourceDirChars=%s errno=%d", sourceDirChars, errno); AKLOGE("DICT: Can't fopen sourceDir. sourceDirChars=%s errno=%d", sourceDirChars, errno);
return 0; return 0;
} }
dictBuf = malloc(sizeof(char) * dictSize); dictBuf = malloc(dictSize);
if (!dictBuf) { if (!dictBuf) {
AKLOGE("DICT: Can't allocate memory region for dictionary. errno=%d", errno); AKLOGE("DICT: Can't allocate memory region for dictionary. errno=%d", errno);
return 0; return 0;
@ -94,7 +94,7 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jobject object,
AKLOGE("DICT: Failure in fseek. ret=%d errno=%d", ret, errno); AKLOGE("DICT: Failure in fseek. ret=%d errno=%d", ret, errno);
return 0; return 0;
} }
ret = fread(dictBuf, sizeof(char) * dictSize, 1, file); ret = fread(dictBuf, dictSize, 1, file);
if (ret != 1) { if (ret != 1) {
AKLOGE("DICT: Failure in fread. ret=%d errno=%d", ret, errno); AKLOGE("DICT: Failure in fread. ret=%d errno=%d", ret, errno);
return 0; return 0;

View File

@ -36,7 +36,7 @@ static inline void safeGetOrFillZeroIntArrayRegion(JNIEnv *env, jintArray jArray
if (jArray && buffer) { if (jArray && buffer) {
env->GetIntArrayRegion(jArray, 0, len, buffer); env->GetIntArrayRegion(jArray, 0, len, buffer);
} else if (buffer) { } else if (buffer) {
memset(buffer, 0, len * sizeof(jint)); memset(buffer, 0, len * sizeof(buffer[0]));
} }
} }
@ -45,7 +45,7 @@ static inline void safeGetOrFillZeroFloatArrayRegion(JNIEnv *env, jfloatArray jA
if (jArray && buffer) { if (jArray && buffer) {
env->GetFloatArrayRegion(jArray, 0, len, buffer); env->GetFloatArrayRegion(jArray, 0, len, buffer);
} else if (buffer) { } else if (buffer) {
memset(buffer, 0, len * sizeof(jfloat)); memset(buffer, 0, len * sizeof(buffer[0]));
} }
} }

View File

@ -45,8 +45,7 @@ UnigramDictionary::UnigramDictionary(const uint8_t *const streamStart, int fullW
int maxWordLength, int maxWords, const unsigned int flags) int maxWordLength, int maxWords, const unsigned int flags)
: DICT_ROOT(streamStart), MAX_WORD_LENGTH(maxWordLength), MAX_WORDS(maxWords), : DICT_ROOT(streamStart), MAX_WORD_LENGTH(maxWordLength), MAX_WORDS(maxWords),
FULL_WORD_MULTIPLIER(fullWordMultiplier), // TODO : remove this variable. FULL_WORD_MULTIPLIER(fullWordMultiplier), // TODO : remove this variable.
ROOT_POS(0), BYTES_IN_ONE_CHAR(sizeof(int)), ROOT_POS(0), MAX_DIGRAPH_SEARCH_DEPTH(DEFAULT_MAX_DIGRAPH_SEARCH_DEPTH), FLAGS(flags) {
MAX_DIGRAPH_SEARCH_DEPTH(DEFAULT_MAX_DIGRAPH_SEARCH_DEPTH), FLAGS(flags) {
if (DEBUG_DICT) { if (DEBUG_DICT) {
AKLOGI("UnigramDictionary - constructor"); AKLOGI("UnigramDictionary - constructor");
} }
@ -103,6 +102,9 @@ void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximit
const int codesRemain, const int currentDepth, int *codesDest, Correction *correction, const int codesRemain, const int currentDepth, int *codesDest, Correction *correction,
WordsPriorityQueuePool *queuePool, WordsPriorityQueuePool *queuePool,
const digraph_t *const digraphs, const unsigned int digraphsSize) const { const digraph_t *const digraphs, const unsigned int digraphsSize) const {
assert(sizeof(codesDest[0]) == sizeof(codesSrc[0]));
assert(sizeof(xCoordinatesBuffer[0]) == sizeof(xcoordinates[0]));
assert(sizeof(yCoordinatesBuffer[0]) == sizeof(ycoordinates[0]));
const int startIndex = static_cast<int>(codesDest - codesBuffer); const int startIndex = static_cast<int>(codesDest - codesBuffer);
if (currentDepth < MAX_DIGRAPH_SEARCH_DEPTH) { if (currentDepth < MAX_DIGRAPH_SEARCH_DEPTH) {
@ -123,9 +125,8 @@ void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximit
// Make i the index of the second char of the digraph for simplicity. Forgetting // Make i the index of the second char of the digraph for simplicity. Forgetting
// to do that results in an infinite recursion so take care! // to do that results in an infinite recursion so take care!
++i; ++i;
memcpy(codesDest, codesSrc, i * BYTES_IN_ONE_CHAR); memcpy(codesDest, codesSrc, i * sizeof(codesDest[0]));
codesDest[(i - 1) * (BYTES_IN_ONE_CHAR / sizeof(codesDest[0]))] = codesDest[i - 1] = replacementCodePoint;
replacementCodePoint;
getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates,
codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize, codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize,
bigramMap, bigramFilter, useFullEditDistance, codesSrc + i + 1, bigramMap, bigramFilter, useFullEditDistance, codesSrc + i + 1,
@ -135,7 +136,7 @@ void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximit
// Copy the second char of the digraph in place, then continue processing on // Copy the second char of the digraph in place, then continue processing on
// the remaining part of the word. // the remaining part of the word.
// In our example, after "pru" in the buffer copy the "e", and continue on "fen" // In our example, after "pru" in the buffer copy the "e", and continue on "fen"
memcpy(codesDest + i, codesSrc + i, BYTES_IN_ONE_CHAR); memcpy(codesDest + i, codesSrc + i, sizeof(codesDest[0]));
getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates,
codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize, codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize,
bigramMap, bigramFilter, useFullEditDistance, codesSrc + i, codesRemain - i, bigramMap, bigramFilter, useFullEditDistance, codesSrc + i, codesRemain - i,
@ -151,13 +152,13 @@ void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximit
// If the word contains several digraphs, we'll come it for the product of them. // If the word contains several digraphs, we'll come it for the product of them.
// eg. if the word is "ueberpruefen" we'll test, in order, against // eg. if the word is "ueberpruefen" we'll test, in order, against
// "uberprufen", "uberpruefen", "ueberprufen", "ueberpruefen". // "uberprufen", "uberpruefen", "ueberprufen", "ueberpruefen".
const unsigned int remainingBytes = BYTES_IN_ONE_CHAR * codesRemain; const unsigned int remainingBytes = sizeof(codesDest[0]) * codesRemain;
if (0 != remainingBytes) { if (0 != remainingBytes) {
memcpy(codesDest, codesSrc, remainingBytes); memcpy(codesDest, codesSrc, remainingBytes);
memcpy(&xCoordinatesBuffer[startIndex], &xcoordinates[codesBufferSize - codesRemain], memcpy(&xCoordinatesBuffer[startIndex], &xcoordinates[codesBufferSize - codesRemain],
sizeof(int) * codesRemain); sizeof(xCoordinatesBuffer[0]) * codesRemain);
memcpy(&yCoordinatesBuffer[startIndex], &ycoordinates[codesBufferSize - codesRemain], memcpy(&yCoordinatesBuffer[startIndex], &ycoordinates[codesBufferSize - codesRemain],
sizeof(int) * codesRemain); sizeof(yCoordinatesBuffer[0]) * codesRemain);
} }
getWordSuggestions(proximityInfo, xCoordinatesBuffer, yCoordinatesBuffer, codesBuffer, getWordSuggestions(proximityInfo, xCoordinatesBuffer, yCoordinatesBuffer, codesBuffer,

View File

@ -116,7 +116,6 @@ class UnigramDictionary {
const int MAX_WORDS; const int MAX_WORDS;
const int FULL_WORD_MULTIPLIER; const int FULL_WORD_MULTIPLIER;
const int ROOT_POS; const int ROOT_POS;
const unsigned int BYTES_IN_ONE_CHAR;
const int MAX_DIGRAPH_SEARCH_DEPTH; const int MAX_DIGRAPH_SEARCH_DEPTH;
const int FLAGS; const int FLAGS;

View File

@ -38,7 +38,7 @@ class WordsPriorityQueue {
void setParams(int score, int *word, int wordLength, int type) { void setParams(int score, int *word, int wordLength, int type) {
mScore = score; mScore = score;
mWordLength = wordLength; mWordLength = wordLength;
memcpy(mWord, word, sizeof(int) * wordLength); memcpy(mWord, word, sizeof(mWord[0]) * wordLength);
mUsed = true; mUsed = true;
mType = type; mType = type;
} }
@ -127,7 +127,7 @@ class WordsPriorityQueue {
} }
} }
if (maxIndex > 0 && nsMaxSw) { if (maxIndex > 0 && nsMaxSw) {
memmove(&swBuffer[1], &swBuffer[0], maxIndex * sizeof(SuggestedWord *)); memmove(&swBuffer[1], &swBuffer[0], maxIndex * sizeof(swBuffer[0]));
swBuffer[0] = nsMaxSw; swBuffer[0] = nsMaxSw;
} }
} }
@ -141,7 +141,7 @@ class WordsPriorityQueue {
int *targetAddress = outputCodePoints + i * MAX_WORD_LENGTH; int *targetAddress = outputCodePoints + i * MAX_WORD_LENGTH;
frequencies[i] = sw->mScore; frequencies[i] = sw->mScore;
outputTypes[i] = sw->mType; outputTypes[i] = sw->mType;
memcpy(targetAddress, sw->mWord, wordLength * sizeof(int)); memcpy(targetAddress, sw->mWord, wordLength * sizeof(targetAddress[0]));
if (wordLength < MAX_WORD_LENGTH) { if (wordLength < MAX_WORD_LENGTH) {
targetAddress[wordLength] = 0; targetAddress[wordLength] = 0;
} }