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 keyXCoordinateArray, jintArray keyYCoordinateArray, jintArray keyWidthArray,
jintArray keyHeightArray, jintArray keyCharCodeArray) { jintArray keyHeightArray, jintArray keyCharCodeArray) {
jint *proximityChars = env->GetIntArrayElements(proximityCharsArray, NULL); jint *proximityChars = env->GetIntArrayElements(proximityCharsArray, NULL);
jint* keyXCoordinates = env->GetIntArrayElements(keyXCoordinateArray, NULL); jint *keyXCoordinates = safeGetIntArrayElements(env, keyXCoordinateArray);
jint* keyYCoordinates = env->GetIntArrayElements(keyYCoordinateArray, NULL); jint *keyYCoordinates = safeGetIntArrayElements(env, keyYCoordinateArray);
jint* keyWidths = env->GetIntArrayElements(keyWidthArray, NULL); jint *keyWidths = safeGetIntArrayElements(env, keyWidthArray);
jint* keyHeights = env->GetIntArrayElements(keyHeightArray, NULL); jint *keyHeights = safeGetIntArrayElements(env, keyHeightArray);
jint* keyCharCodes = env->GetIntArrayElements(keyCharCodeArray, NULL); jint *keyCharCodes = safeGetIntArrayElements(env, keyCharCodeArray);
ProximityInfo *proximityInfo = new ProximityInfo(maxProximityCharsSize, displayWidth, ProximityInfo *proximityInfo = new ProximityInfo(maxProximityCharsSize, displayWidth,
displayHeight, gridWidth, gridHeight, (const uint32_t*)proximityChars, displayHeight, gridWidth, gridHeight, (const uint32_t*)proximityChars,
keyCount, (const int32_t*)keyXCoordinates, (const int32_t*)keyYCoordinates, keyCount, (const int32_t*)keyXCoordinates, (const int32_t*)keyYCoordinates,
(const int32_t*)keyWidths, (const int32_t*)keyHeights, (const int32_t*)keyCharCodes); (const int32_t*)keyWidths, (const int32_t*)keyHeights, (const int32_t*)keyCharCodes);
env->ReleaseIntArrayElements(keyCharCodeArray, keyCharCodes, 0); safeReleaseIntArrayElements(env, keyCharCodeArray, keyCharCodes);
env->ReleaseIntArrayElements(keyHeightArray, keyHeights, 0); safeReleaseIntArrayElements(env, keyHeightArray, keyHeights);
env->ReleaseIntArrayElements(keyWidthArray, keyWidths, 0); safeReleaseIntArrayElements(env, keyWidthArray, keyWidths);
env->ReleaseIntArrayElements(keyYCoordinateArray, keyYCoordinates, 0); safeReleaseIntArrayElements(env, keyYCoordinateArray, keyYCoordinates);
env->ReleaseIntArrayElements(keyXCoordinateArray, keyXCoordinates, 0); safeReleaseIntArrayElements(env, keyXCoordinateArray, keyXCoordinates);
env->ReleaseIntArrayElements(proximityCharsArray, proximityChars, 0); env->ReleaseIntArrayElements(proximityCharsArray, proximityChars, 0);
return (jint)proximityInfo; return (jint)proximityInfo;
} }

View file

@ -18,6 +18,8 @@
#ifndef LATINIME_JNI_COMMON_H #ifndef LATINIME_JNI_COMMON_H
#define LATINIME_JNI_COMMON_H #define LATINIME_JNI_COMMON_H
#include <stdlib.h>
#include "jni.h" #include "jni.h"
namespace latinime { namespace latinime {
@ -25,6 +27,20 @@ namespace latinime {
int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods, int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods,
int numMethods); 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 } // namespace latinime
#endif // LATINIME_JNI_COMMON_H #endif // LATINIME_JNI_COMMON_H

View file

@ -26,6 +26,14 @@
namespace latinime { 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, ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth,
const int keyboardHeight, const int gridWidth, const int gridHeight, const int keyboardHeight, const int gridWidth, const int gridHeight,
const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates, 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])); memcpy(mProximityCharsArray, proximityCharsArray, len * sizeof(mProximityCharsArray[0]));
if (KEY_COUNT > 0) { copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
if (DEBUG_PROXIMITY_INFO) { copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
LOGI("Create key coordinate array %d", keyCount); copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
} copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
memcpy(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0])); copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[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]));
}
initializeCodeToGroup(); initializeCodeToGroup();
initializeCodeToKeyIndex(); initializeCodeToKeyIndex();