am e76dbffe: Merge "Add explicit NULL check to latinime_Keyboard_setProximityInfo() and ProximityInfo::ProximityInfo()."

* commit 'e76dbffe002e37dee4d5cac25af06c63257632a2':
  Add explicit NULL check to latinime_Keyboard_setProximityInfo() and ProximityInfo::ProximityInfo().
This commit is contained in:
Yusuke Nojima 2011-09-26 23:07:45 -07:00 committed by Android Git Automerger
commit c9db25b0a5
3 changed files with 45 additions and 26 deletions

View file

@ -34,20 +34,20 @@ static jint latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object,
jintArray keyXCoordinateArray, jintArray keyYCoordinateArray, jintArray keyWidthArray,
jintArray keyHeightArray, jintArray keyCharCodeArray) {
jint *proximityChars = env->GetIntArrayElements(proximityCharsArray, NULL);
jint* keyXCoordinates = env->GetIntArrayElements(keyXCoordinateArray, NULL);
jint* keyYCoordinates = env->GetIntArrayElements(keyYCoordinateArray, NULL);
jint* keyWidths = env->GetIntArrayElements(keyWidthArray, NULL);
jint* keyHeights = env->GetIntArrayElements(keyHeightArray, NULL);
jint* keyCharCodes = env->GetIntArrayElements(keyCharCodeArray, NULL);
jint *keyXCoordinates = safeGetIntArrayElements(env, keyXCoordinateArray);
jint *keyYCoordinates = safeGetIntArrayElements(env, keyYCoordinateArray);
jint *keyWidths = safeGetIntArrayElements(env, keyWidthArray);
jint *keyHeights = safeGetIntArrayElements(env, keyHeightArray);
jint *keyCharCodes = safeGetIntArrayElements(env, keyCharCodeArray);
ProximityInfo *proximityInfo = new ProximityInfo(maxProximityCharsSize, displayWidth,
displayHeight, gridWidth, gridHeight, (const uint32_t*)proximityChars,
keyCount, (const int32_t*)keyXCoordinates, (const int32_t*)keyYCoordinates,
(const int32_t*)keyWidths, (const int32_t*)keyHeights, (const int32_t*)keyCharCodes);
env->ReleaseIntArrayElements(keyCharCodeArray, keyCharCodes, 0);
env->ReleaseIntArrayElements(keyHeightArray, keyHeights, 0);
env->ReleaseIntArrayElements(keyWidthArray, keyWidths, 0);
env->ReleaseIntArrayElements(keyYCoordinateArray, keyYCoordinates, 0);
env->ReleaseIntArrayElements(keyXCoordinateArray, keyXCoordinates, 0);
safeReleaseIntArrayElements(env, keyCharCodeArray, keyCharCodes);
safeReleaseIntArrayElements(env, keyHeightArray, keyHeights);
safeReleaseIntArrayElements(env, keyWidthArray, keyWidths);
safeReleaseIntArrayElements(env, keyYCoordinateArray, keyYCoordinates);
safeReleaseIntArrayElements(env, keyXCoordinateArray, keyXCoordinates);
env->ReleaseIntArrayElements(proximityCharsArray, proximityChars, 0);
return (jint)proximityInfo;
}

View file

@ -18,6 +18,8 @@
#ifndef LATINIME_JNI_COMMON_H
#define LATINIME_JNI_COMMON_H
#include <stdlib.h>
#include "jni.h"
namespace latinime {
@ -25,6 +27,20 @@ namespace latinime {
int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods,
int numMethods);
inline jint *safeGetIntArrayElements(JNIEnv *env, jintArray jArray) {
if (jArray) {
return env->GetIntArrayElements(jArray, NULL);
} else {
return NULL;
}
}
inline void safeReleaseIntArrayElements(JNIEnv *env, jintArray jArray, jint *cArray) {
if (jArray) {
env->ReleaseIntArrayElements(jArray, cArray, 0);
}
}
} // namespace latinime
#endif // LATINIME_JNI_COMMON_H

View file

@ -26,6 +26,14 @@
namespace latinime {
inline void copyOrFillZero(void *to, const void *from, size_t size) {
if (from) {
memcpy(to, from, size);
} else {
memset(to, 0, size);
}
}
ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth,
const int keyboardHeight, const int gridWidth, const int gridHeight,
const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
@ -43,16 +51,11 @@ ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboard
}
memcpy(mProximityCharsArray, proximityCharsArray, len * sizeof(mProximityCharsArray[0]));
if (KEY_COUNT > 0) {
if (DEBUG_PROXIMITY_INFO) {
LOGI("Create key coordinate array %d", keyCount);
}
memcpy(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
memcpy(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
memcpy(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
memcpy(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
memcpy(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
}
copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
initializeCodeToGroup();
initializeCodeToKeyIndex();