8035870: Check jdk/src/windows/native/java/io/WinNTFileSystem_md.c for JNI pending exceptions
authormsheppar
Fri, 21 Mar 2014 00:57:23 +0000
changeset 23565 b28f771b8cbe
parent 23564 62146f638e22
child 23566 760a74c1b589
8035870: Check jdk/src/windows/native/java/io/WinNTFileSystem_md.c for JNI pending exceptions Summary: NI return checks, NULL return checks for malloc added Reviewed-by: alanb, chegar
jdk/src/windows/native/java/io/WinNTFileSystem_md.c
jdk/src/windows/native/java/io/io_util_md.c
--- a/jdk/src/windows/native/java/io/WinNTFileSystem_md.c	Fri Mar 21 00:13:40 2014 +0000
+++ b/jdk/src/windows/native/java/io/WinNTFileSystem_md.c	Fri Mar 21 00:57:23 2014 +0000
@@ -85,7 +85,7 @@
  * Retrieves the fully resolved (final) path for the given path or NULL
  * if the function fails.
  */
-static WCHAR* getFinalPath(const WCHAR *path)
+static WCHAR* getFinalPath(JNIEnv *env, const WCHAR *path)
 {
     HANDLE h;
     WCHAR *result;
@@ -121,6 +121,7 @@
                 len = (*GetFinalPathNameByHandle_func)(h, result, len, 0);
             } else {
                 len = 0;
+                JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
             }
         }
 
@@ -141,6 +142,7 @@
                 /* copy result without prefix into new buffer */
                 WCHAR *tmp = (WCHAR*)malloc(resultLen * sizeof(WCHAR));
                 if (tmp == NULL) {
+                    JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
                     len = 0;
                 } else {
                     WCHAR *p = result;
@@ -164,6 +166,8 @@
             free(result);
             result = NULL;
         }
+    } else {
+        JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
     }
 
     error = GetLastError();
@@ -257,6 +261,8 @@
                     rv = (*env)->NewString(env, cp, (jsize)wcslen(cp));
                 }
                 free(cp);
+            } else {
+                JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
             }
         } else if (wcanonicalize(path, canonicalPath, MAX_PATH_LENGTH) >= 0) {
             rv = (*env)->NewString(env, canonicalPath, (jsize)wcslen(canonicalPath));
@@ -288,6 +294,8 @@
                       rv = (*env)->NewString(env, cp, (jsize)wcslen(cp));
                     }
                     free(cp);
+                } else {
+                    JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
                 }
             } else if (wcanonicalizeWithPrefix(canonicalPrefix,
                                                pathWithCanonicalPrefix,
@@ -433,7 +441,7 @@
     if ((a != INVALID_FILE_ATTRIBUTES) &&
         ((a & FILE_ATTRIBUTE_REPARSE_POINT) != 0))
     {
-        WCHAR *fp = getFinalPath(pathbuf);
+        WCHAR *fp = getFinalPath(env, pathbuf);
         if (fp == NULL) {
             a = INVALID_FILE_ATTRIBUTES;
         } else {
@@ -628,6 +636,7 @@
     if (search_path == 0) {
         free (pathbuf);
         errno = ENOMEM;
+        JNU_ThrowOutOfMemoryError(env, "native memory allocation faiuled");
         return NULL;
     }
     wcscpy(search_path, pathbuf);
@@ -803,7 +812,7 @@
     if ((a != INVALID_FILE_ATTRIBUTES) &&
         ((a & FILE_ATTRIBUTE_REPARSE_POINT) != 0))
     {
-        WCHAR *fp = getFinalPath(pathbuf);
+        WCHAR *fp = getFinalPath(env, pathbuf);
         if (fp == NULL) {
             a = INVALID_FILE_ATTRIBUTES;
         } else {
--- a/jdk/src/windows/native/java/io/io_util_md.c	Fri Mar 21 00:13:40 2014 +0000
+++ b/jdk/src/windows/native/java/io/io_util_md.c	Fri Mar 21 00:57:23 2014 +0000
@@ -161,10 +161,6 @@
             {
                  if (pathlen > max_path - 1) {
                      pathbuf = prefixAbpath(ps, pathlen, pathlen);
-                     if (pathbuf == NULL) {
-                         JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
-                         return NULL;
-                     }
                  } else {
                      pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
                      if (pathbuf != 0) {
@@ -191,10 +187,6 @@
                 int dirlen = currentDirLength(ps, pathlen);
                 if (dirlen + pathlen + 1 > max_path - 1) {
                     pathbuf = prefixAbpath(ps, pathlen, dirlen + pathlen);
-                    if( pathbuf == NULL) {
-                        JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
-                        return NULL;
-                    }
                 } else {
                     pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
                     if (pathbuf != 0) {
@@ -216,13 +208,16 @@
             return NULL;
         } else {
             pathbuf = (WCHAR*)malloc(sizeof(WCHAR));
-            pathbuf[0] = L'\0';
+            if (pathbuf != NULL) {
+                pathbuf[0] = L'\0';
+            } else {
+                JNU_ThrowOutOfMemoryError(env, 0);
+                return NULL;
+            }
         }
     }
     if (pathbuf == 0) {
-        if (!(*env)->ExceptionCheck(env)) {
-            JNU_ThrowOutOfMemoryError(env,  "native memory allocation failed");
-        }
+        JNU_ThrowOutOfMemoryError(env, 0);
         return NULL;
     }
     return pathbuf;