changeset 23562:596efd9dfcaf

8035340: (prefs) Check jdk/src/windows/native/java/util/WindowsPreference.c for JNI pending exceptions Summary: Check for JNI pending exceptions. Add null checks after malloc calls Reviewed-by: alanb, chegar
author msheppar
date Thu, 20 Mar 2014 23:34:38 +0000
parents babc1414a7e4
children 71ed058849d4
files jdk/make/lib/CoreLibraries.gmk jdk/src/windows/native/java/util/prefs/WindowsPreferences.c
diffstat 2 files changed, 54 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/make/lib/CoreLibraries.gmk	Thu Mar 20 23:12:55 2014 +0000
+++ b/jdk/make/lib/CoreLibraries.gmk	Thu Mar 20 23:34:38 2014 +0000
@@ -305,7 +305,7 @@
     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) \
--- a/jdk/src/windows/native/java/util/prefs/WindowsPreferences.c	Thu Mar 20 23:12:55 2014 +0000
+++ b/jdk/src/windows/native/java/util/prefs/WindowsPreferences.c	Thu Mar 20 23:34:38 2014 +0000
@@ -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 @@
         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 @@
         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 @@
         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 @@
         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 @@
         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 @@
         }
 
         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 @@
         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 @@
         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 @@
 
     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 @@
         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 @@
         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 @@
           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 @@
             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;
      }