char** ScilabJavaObject::getInfos (JavaVM * jvm_, int *lenRow){
JNIEnv * curEnv = NULL;
jvm_->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL);
jclass cls = initClass(curEnv);
if ( cls == NULL) {
throw GiwsException::JniCallMethodException(curEnv);
}
static jmethodID jobjectArray_getInfosID = curEnv->GetStaticMethodID(cls, "getInfos", "()[Ljava/lang/String;" ) ;
if (jobjectArray_getInfosID == NULL) {
throw GiwsException::JniMethodNotFoundException(curEnv, "getInfos");
}
jobjectArray res = static_cast<jobjectArray>( curEnv->CallStaticObjectMethod(cls, jobjectArray_getInfosID ));
if (curEnv->ExceptionCheck()) {
throw GiwsException::JniCallMethodException(curEnv);
}if (res != NULL) {
* lenRow = curEnv->GetArrayLength(res);
char **arrayOfString;
arrayOfString = new char *[*lenRow];
for (jsize i = 0; i < *lenRow; i++){
jstring resString = reinterpret_cast<jstring>(curEnv->GetObjectArrayElement(res, i));
const char *tempString = curEnv->GetStringUTFChars(resString, 0);
arrayOfString[i] = new char[strlen(tempString) + 1];
strcpy(arrayOfString[i], tempString);
curEnv->ReleaseStringUTFChars(resString, tempString);
curEnv->DeleteLocalRef(resString);
}
if (curEnv->ExceptionCheck()) {
delete[] arrayOfString;
throw GiwsException::JniCallMethodException(curEnv);
}
curEnv->DeleteLocalRef(res);
return arrayOfString;
} else {
curEnv->DeleteLocalRef(res);
return NULL;
}
}
If
jobjectArray res = static_cast<jobjectArray>( curEnv->CallStaticObjectMethod(cls, jobjectArray_getInfosID ));returnsNULLthenlenRowis not initialized and can make the caller crash.