Use sizeof() more safely.

Change-Id: I7cffb4d8da847b483cf4bb482508e3c88c4903be
main
Ken Wakasa 2012-11-01 17:05:08 +09:00
parent 2db1ea79e6
commit 44d9c1ebfc
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;
int adjDictOffset = static_cast<int>(dictOffset) - 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) {
AKLOGE("DICT: Can't mmap dictionary. errno=%d", errno);
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);
return 0;
}
dictBuf = malloc(sizeof(char) * dictSize);
dictBuf = malloc(dictSize);
if (!dictBuf) {
AKLOGE("DICT: Can't allocate memory region for dictionary. errno=%d", errno);
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);
return 0;
}
ret = fread(dictBuf, sizeof(char) * dictSize, 1, file);
ret = fread(dictBuf, dictSize, 1, file);
if (ret != 1) {
AKLOGE("DICT: Failure in fread. ret=%d errno=%d", ret, errno);
return 0;

View File

@ -36,7 +36,7 @@ static inline void safeGetOrFillZeroIntArrayRegion(JNIEnv *env, jintArray jArray
if (jArray && buffer) {
env->GetIntArrayRegion(jArray, 0, len, 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) {
env->GetFloatArrayRegion(jArray, 0, len, 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)
: DICT_ROOT(streamStart), MAX_WORD_LENGTH(maxWordLength), MAX_WORDS(maxWords),
FULL_WORD_MULTIPLIER(fullWordMultiplier), // TODO : remove this variable.
ROOT_POS(0), BYTES_IN_ONE_CHAR(sizeof(int)),
MAX_DIGRAPH_SEARCH_DEPTH(DEFAULT_MAX_DIGRAPH_SEARCH_DEPTH), FLAGS(flags) {
ROOT_POS(0), MAX_DIGRAPH_SEARCH_DEPTH(DEFAULT_MAX_DIGRAPH_SEARCH_DEPTH), FLAGS(flags) {
if (DEBUG_DICT) {
AKLOGI("UnigramDictionary - constructor");
}
@ -103,6 +102,9 @@ void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximit
const int codesRemain, const int currentDepth, int *codesDest, Correction *correction,
WordsPriorityQueuePool *queuePool,
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);
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
// to do that results in an infinite recursion so take care!
++i;
memcpy(codesDest, codesSrc, i * BYTES_IN_ONE_CHAR);
codesDest[(i - 1) * (BYTES_IN_ONE_CHAR / sizeof(codesDest[0]))] =
replacementCodePoint;
memcpy(codesDest, codesSrc, i * sizeof(codesDest[0]));
codesDest[i - 1] = replacementCodePoint;
getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates,
codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize,
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
// the remaining part of the word.
// 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,
codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize,
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.
// eg. if the word is "ueberpruefen" we'll test, in order, against
// "uberprufen", "uberpruefen", "ueberprufen", "ueberpruefen".
const unsigned int remainingBytes = BYTES_IN_ONE_CHAR * codesRemain;
const unsigned int remainingBytes = sizeof(codesDest[0]) * codesRemain;
if (0 != remainingBytes) {
memcpy(codesDest, codesSrc, remainingBytes);
memcpy(&xCoordinatesBuffer[startIndex], &xcoordinates[codesBufferSize - codesRemain],
sizeof(int) * codesRemain);
sizeof(xCoordinatesBuffer[0]) * codesRemain);
memcpy(&yCoordinatesBuffer[startIndex], &ycoordinates[codesBufferSize - codesRemain],
sizeof(int) * codesRemain);
sizeof(yCoordinatesBuffer[0]) * codesRemain);
}
getWordSuggestions(proximityInfo, xCoordinatesBuffer, yCoordinatesBuffer, codesBuffer,

View File

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

View File

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