diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 461ed56955bc..81c271b07633 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,14 @@ +2003-06-11 Andrew Haley + + * jni.cc (_Jv_JNI_check_types): New. + (_Jv_JNI_SetPrimgitiveArrayRegion): Check array type. + (_Jv_JNI_GetPrimitiveArrayRegion): Ditto. + (_Jv_JNI_GetPrimitiveArrayElements): Ditto. + (_Jv_JNI_ReleasePrimitiveArrayElements): Ditto. + + * java/lang/natVMSecurityManager.cc (getClassContext): Fix + infinite loop. + 2003-06-11 Tom Tromey * java/lang/ClassLoader.java (loadClass): Not deprecated. diff --git a/libjava/java/lang/natVMSecurityManager.cc b/libjava/java/lang/natVMSecurityManager.cc index 7b88e8a4a8c4..33aa8873f1d5 100644 --- a/libjava/java/lang/natVMSecurityManager.cc +++ b/libjava/java/lang/natVMSecurityManager.cc @@ -28,9 +28,9 @@ java::lang::VMSecurityManager::getClassContext () int maxlen = t->length(); int len = 0; - while (len < maxlen) + for (int i=0; iclassAt(len); + jclass klass = t->classAt(i); if (klass != NULL && klass != &java::lang::VMSecurityManager::class$ && klass != &java::lang::SecurityManager::class$) ++len; @@ -41,9 +41,9 @@ java::lang::VMSecurityManager::getClassContext () NULL); len = 0; - while (len < maxlen) + for (int i=0; iclassAt(len); + jclass klass = t->classAt(i); if (klass != NULL && klass != &java::lang::VMSecurityManager::class$ && klass != &java::lang::SecurityManager::class$) elements(result)[len++] = klass; diff --git a/libjava/jni.cc b/libjava/jni.cc index 7dd983641187..68aeb5d6bc19 100644 --- a/libjava/jni.cc +++ b/libjava/jni.cc @@ -39,6 +39,7 @@ details. */ #include #include #include +#include #include #include @@ -384,6 +385,22 @@ static jobject return _Jv_JNI_PopLocalFrame (env, result, MARK_USER); } +// Make sure an array's type is compatible with the type of the +// destination. +template +static bool +_Jv_JNI_check_types (JNIEnv *env, JArray *array, jclass K) +{ + jclass klass = array->getClass()->getComponentType(); + if (__builtin_expect (klass != K, false)) + { + env->ex = new java::lang::IllegalAccessError (); + return false; + } + else + return true; +} + // Pop a `system' frame from the stack. This is `extern "C"' as it is // used by the compiler. extern "C" void @@ -1446,12 +1463,14 @@ static JArray * } } -template +template static T * -(JNICALL _Jv_JNI_GetPrimitiveArrayElements) (JNIEnv *, JArray *array, +(JNICALL _Jv_JNI_GetPrimitiveArrayElements) (JNIEnv *env, JArray *array, jboolean *isCopy) { array = unwrap (array); + if (! _Jv_JNI_check_types (env, array, K)) + return NULL; T *elts = elements (array); if (isCopy) { @@ -1462,25 +1481,28 @@ static T * return elts; } -template +template static void -(JNICALL _Jv_JNI_ReleasePrimitiveArrayElements) (JNIEnv *, JArray *array, +(JNICALL _Jv_JNI_ReleasePrimitiveArrayElements) (JNIEnv *env, JArray *array, T *, jint /* mode */) { array = unwrap (array); + _Jv_JNI_check_types (env, array, K); // Note that we ignore MODE. We can do this because we never copy // the array elements. My reading of the JNI documentation is that // this is an option for the implementor. unmark_for_gc (array, global_ref_table); } -template +template static void (JNICALL _Jv_JNI_GetPrimitiveArrayRegion) (JNIEnv *env, JArray *array, jsize start, jsize len, T *buf) { array = unwrap (array); + if (! _Jv_JNI_check_types (env, array, K)) + return; // The cast to unsigned lets us save a comparison. if (start < 0 || len < 0 @@ -1504,12 +1526,14 @@ static void } } -template +template static void (JNICALL _Jv_JNI_SetPrimitiveArrayRegion) (JNIEnv *env, JArray *array, jsize start, jsize len, T *buf) { array = unwrap (array); + if (! _Jv_JNI_check_types (env, array, K)) + return; // The cast to unsigned lets us save a comparison. if (start < 0 || len < 0 @@ -2688,38 +2712,70 @@ struct JNINativeInterface _Jv_JNIFunctions = _Jv_JNI_NewPrimitiveArray, // NewLongArray _Jv_JNI_NewPrimitiveArray, // NewFloatArray _Jv_JNI_NewPrimitiveArray, // NewDoubleArray - _Jv_JNI_GetPrimitiveArrayElements, // GetBooleanArrayElements - _Jv_JNI_GetPrimitiveArrayElements, // GetByteArrayElements - _Jv_JNI_GetPrimitiveArrayElements, // GetCharArrayElements - _Jv_JNI_GetPrimitiveArrayElements, // GetShortArrayElements - _Jv_JNI_GetPrimitiveArrayElements, // GetIntArrayElements - _Jv_JNI_GetPrimitiveArrayElements, // GetLongArrayElements - _Jv_JNI_GetPrimitiveArrayElements, // GetFloatArrayElements - _Jv_JNI_GetPrimitiveArrayElements, // GetDoubleArrayElements - _Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseBooleanArrayElements - _Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseByteArrayElements - _Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseCharArrayElements - _Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseShortArrayElements - _Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseIntArrayElements - _Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseLongArrayElements - _Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseFloatArrayElements - _Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseDoubleArrayElements - _Jv_JNI_GetPrimitiveArrayRegion, // GetBooleanArrayRegion - _Jv_JNI_GetPrimitiveArrayRegion, // GetByteArrayRegion - _Jv_JNI_GetPrimitiveArrayRegion, // GetCharArrayRegion - _Jv_JNI_GetPrimitiveArrayRegion, // GetShortArrayRegion - _Jv_JNI_GetPrimitiveArrayRegion, // GetIntArrayRegion - _Jv_JNI_GetPrimitiveArrayRegion, // GetLongArrayRegion - _Jv_JNI_GetPrimitiveArrayRegion, // GetFloatArrayRegion - _Jv_JNI_GetPrimitiveArrayRegion, // GetDoubleArrayRegion - _Jv_JNI_SetPrimitiveArrayRegion, // SetBooleanArrayRegion - _Jv_JNI_SetPrimitiveArrayRegion, // SetByteArrayRegion - _Jv_JNI_SetPrimitiveArrayRegion, // SetCharArrayRegion - _Jv_JNI_SetPrimitiveArrayRegion, // SetShortArrayRegion - _Jv_JNI_SetPrimitiveArrayRegion, // SetIntArrayRegion - _Jv_JNI_SetPrimitiveArrayRegion, // SetLongArrayRegion - _Jv_JNI_SetPrimitiveArrayRegion, // SetFloatArrayRegion - _Jv_JNI_SetPrimitiveArrayRegion, // SetDoubleArrayRegion + _Jv_JNI_GetPrimitiveArrayElements, + // GetBooleanArrayElements + _Jv_JNI_GetPrimitiveArrayElements, + // GetByteArrayElements + _Jv_JNI_GetPrimitiveArrayElements, + // GetCharArrayElements + _Jv_JNI_GetPrimitiveArrayElements, + // GetShortArrayElements + _Jv_JNI_GetPrimitiveArrayElements, + // GetIntArrayElements + _Jv_JNI_GetPrimitiveArrayElements, + // GetLongArrayElements + _Jv_JNI_GetPrimitiveArrayElements, + // GetFloatArrayElements + _Jv_JNI_GetPrimitiveArrayElements, + // GetDoubleArrayElements + _Jv_JNI_ReleasePrimitiveArrayElements, + // ReleaseBooleanArrayElements + _Jv_JNI_ReleasePrimitiveArrayElements, + // ReleaseByteArrayElements + _Jv_JNI_ReleasePrimitiveArrayElements, + // ReleaseCharArrayElements + _Jv_JNI_ReleasePrimitiveArrayElements, + // ReleaseShortArrayElements + _Jv_JNI_ReleasePrimitiveArrayElements, + // ReleaseIntArrayElements + _Jv_JNI_ReleasePrimitiveArrayElements, + // ReleaseLongArrayElements + _Jv_JNI_ReleasePrimitiveArrayElements, + // ReleaseFloatArrayElements + _Jv_JNI_ReleasePrimitiveArrayElements, + // ReleaseDoubleArrayElements + _Jv_JNI_GetPrimitiveArrayRegion, + // GetBooleanArrayRegion + _Jv_JNI_GetPrimitiveArrayRegion, + // GetByteArrayRegion + _Jv_JNI_GetPrimitiveArrayRegion, + // GetCharArrayRegion + _Jv_JNI_GetPrimitiveArrayRegion, + // GetShortArrayRegion + _Jv_JNI_GetPrimitiveArrayRegion, + // GetIntArrayRegion + _Jv_JNI_GetPrimitiveArrayRegion, + // GetLongArrayRegion + _Jv_JNI_GetPrimitiveArrayRegion, + // GetFloatArrayRegion + _Jv_JNI_GetPrimitiveArrayRegion, + // GetDoubleArrayRegion + _Jv_JNI_SetPrimitiveArrayRegion, + // SetBooleanArrayRegion + _Jv_JNI_SetPrimitiveArrayRegion, + // SetByteArrayRegion + _Jv_JNI_SetPrimitiveArrayRegion, + // SetCharArrayRegion + _Jv_JNI_SetPrimitiveArrayRegion, + // SetShortArrayRegion + _Jv_JNI_SetPrimitiveArrayRegion, + // SetIntArrayRegion + _Jv_JNI_SetPrimitiveArrayRegion, + // SetLongArrayRegion + _Jv_JNI_SetPrimitiveArrayRegion, + // SetFloatArrayRegion + _Jv_JNI_SetPrimitiveArrayRegion, + // SetDoubleArrayRegion _Jv_JNI_RegisterNatives, // RegisterNatives _Jv_JNI_UnregisterNatives, // UnregisterNatives _Jv_JNI_MonitorEnter, // MonitorEnter