8035340: (prefs) Check jdk/src/windows/native/java/util/WindowsPreference.c for JNI pending exceptions

Check for JNI pending exceptions. Add null checks after malloc calls

Reviewed-by: alanb, chegar
This commit is contained in:
Mark Sheppard 2014-03-20 23:34:38 +00:00
parent e506d671fa
commit d278ecae87
2 changed files with 54 additions and 19 deletions

View File

@ -305,7 +305,7 @@ $(eval $(call SetupNativeCompilation,BUILD_LIBPREFS, \
LDFLAGS_SUFFIX_linux := -ljvm -ljava, \
LDFLAGS_SUFFIX_solaris := -ljvm -ljava -lc, \
LDFLAGS_SUFFIX_aix := -ljvm -ljava, \
LDFLAGS_SUFFIX_windows := advapi32.lib, \
LDFLAGS_SUFFIX_windows := advapi32.lib jvm.lib $(WIN_JAVA_LIB), \
LDFLAGS_SUFFIX_macosx := -ljvm -framework CoreFoundation, \
VERSIONINFO_RESOURCE := $(JDK_TOPDIR)/src/windows/resource/version.rc, \
RC_FLAGS := $(RC_FLAGS) \

View File

@ -24,8 +24,10 @@
*/
#include <stdlib.h>
#include <jni.h>
#include <windows.h>
#include "jni.h"
#include "jni_util.h"
#include "jvm.h"
#ifdef __cplusplus
extern "C" {
#endif
@ -37,12 +39,15 @@ extern "C" {
int errorCode=-1;
jintArray result;
str = (*env)->GetByteArrayElements(env, lpSubKey, NULL);
CHECK_NULL_RETURN(str, NULL);
errorCode = RegOpenKeyEx((HKEY)hKey, str, 0, securityMask, &handle);
(*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0);
tmp[0]= (int) handle;
tmp[1]= errorCode;
result = (*env)->NewIntArray(env,2);
(*env)->SetIntArrayRegion(env, result, 0, 2, tmp);
if (result != NULL) {
(*env)->SetIntArrayRegion(env, result, 0, 2, tmp);
}
return result;
}
@ -58,8 +63,9 @@ extern "C" {
int tmp[3];
DWORD lpdwDisposition;
int errorCode;
jintArray result;
jintArray result = NULL;
str = (*env)->GetByteArrayElements(env, lpSubKey, NULL);
CHECK_NULL_RETURN(str, NULL);
errorCode = RegCreateKeyEx((HKEY)hKey, str, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_READ,
NULL, &handle, &lpdwDisposition);
@ -68,7 +74,9 @@ extern "C" {
tmp[1]= errorCode;
tmp[2]= lpdwDisposition;
result = (*env)->NewIntArray(env,3);
(*env)->SetIntArrayRegion(env, result, 0, 3, tmp);
if (result != NULL) {
(*env)->SetIntArrayRegion(env, result, 0, 3, tmp);
}
return result;
}
@ -77,6 +85,7 @@ extern "C" {
char* str;
int result;
str = (*env)->GetByteArrayElements(env, lpSubKey, NULL);
CHECK_NULL_RETURN(str, -1);
result = RegDeleteKey((HKEY)hKey, str);
(*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0);
return result;
@ -96,6 +105,7 @@ extern "C" {
DWORD valueType;
DWORD valueSize;
valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL);
CHECK_NULL_RETURN(valueNameStr, NULL);
if (RegQueryValueEx((HKEY)hKey, valueNameStr, NULL, &valueType, NULL,
&valueSize) != ERROR_SUCCESS) {
(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);
@ -103,19 +113,26 @@ extern "C" {
}
buffer = (char*)malloc(valueSize);
if (RegQueryValueEx((HKEY)hKey, valueNameStr, NULL, &valueType, buffer,
&valueSize) != ERROR_SUCCESS) {
free(buffer);
if (buffer != NULL) {
if (RegQueryValueEx((HKEY)hKey, valueNameStr, NULL, &valueType, buffer,
&valueSize) != ERROR_SUCCESS) {
free(buffer);
(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);
return NULL;
}
} else {
JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);
return NULL;
return NULL;
}
if (valueType == REG_SZ) {
result = (*env)->NewByteArray(env, valueSize);
(*env)->SetByteArrayRegion(env, result, 0, valueSize, buffer);
result = (*env)->NewByteArray(env, valueSize);
if (result != NULL) {
(*env)->SetByteArrayRegion(env, result, 0, valueSize, buffer);
}
} else {
result = NULL;
result = NULL;
}
free(buffer);
(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);
@ -135,11 +152,14 @@ extern "C" {
if ((valueName == NULL)||(data == NULL)) {return -1;}
size = (*env)->GetArrayLength(env, data);
dataStr = (*env)->GetByteArrayElements(env, data, NULL);
CHECK_NULL_RETURN(dataStr, -1);
valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL);
error_code = RegSetValueEx((HKEY)hKey, valueNameStr, 0,
if (valueNameStr != NULL) {
error_code = RegSetValueEx((HKEY)hKey, valueNameStr, 0,
REG_SZ, dataStr, size);
(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);
}
(*env)->ReleaseByteArrayElements(env, data, dataStr, 0);
(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);
return error_code;
}
@ -149,6 +169,7 @@ extern "C" {
int error_code = -1;
if (valueName == NULL) {return -1;}
valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL);
CHECK_NULL_RETURN(valueNameStr, -1);
error_code = RegDeleteValue((HKEY)hKey, valueNameStr);
(*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);
return error_code;
@ -156,7 +177,7 @@ extern "C" {
JNIEXPORT jintArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegQueryInfoKey
(JNIEnv* env, jclass this_class, jint hKey) {
jintArray result;
jintArray result = NULL;
int tmp[5];
int valuesNumber = -1;
int maxValueNameLength = -1;
@ -173,7 +194,9 @@ extern "C" {
tmp[3]= maxSubKeyLength;
tmp[4]= maxValueNameLength;
result = (*env)->NewIntArray(env,5);
(*env)->SetIntArrayRegion(env, result, 0, 5, tmp);
if (result != NULL) {
(*env)->SetIntArrayRegion(env, result, 0, 5, tmp);
}
return result;
}
@ -183,13 +206,19 @@ extern "C" {
jbyteArray result;
char* buffer = NULL;
buffer = (char*)malloc(maxKeyLength);
if (buffer == NULL) {
JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
return NULL;
}
if (RegEnumKeyEx((HKEY) hKey, subKeyIndex, buffer, &size, NULL, NULL,
NULL, NULL) != ERROR_SUCCESS){
free(buffer);
return NULL;
}
result = (*env)->NewByteArray(env, size + 1);
(*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer);
if (result != NULL) {
(*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer);
}
free(buffer);
return result;
}
@ -201,6 +230,10 @@ extern "C" {
char* buffer = NULL;
int error_code;
buffer = (char*)malloc(maxValueNameLength);
if (buffer == NULL) {
JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
return NULL;
}
error_code = RegEnumValue((HKEY) hKey, valueIndex, buffer,
&size, NULL, NULL, NULL, NULL);
if (error_code!= ERROR_SUCCESS){
@ -208,7 +241,9 @@ extern "C" {
return NULL;
}
result = (*env)->NewByteArray(env, size + 1);
(*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer);
if (result != NULL) {
(*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer);
}
free(buffer);
return result;
}