changeset 9691:7a3d9b221f8a

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
author msheppar
date Fri, 20 Jun 2014 17:43:13 +0100
parents 74014d58b791
children 7a49291d7ac7
files src/windows/native/java/io/WinNTFileSystem_md.c src/windows/native/java/io/io_util_md.c
diffstat 2 files changed, 24 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/windows/native/java/io/WinNTFileSystem_md.c	Fri Jun 20 17:33:28 2014 +0100
+++ b/src/windows/native/java/io/WinNTFileSystem_md.c	Fri Jun 20 17:43:13 2014 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -83,7 +83,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;
@@ -119,6 +119,7 @@
                 len = (*GetFinalPathNameByHandle_func)(h, result, len, 0);
             } else {
                 len = 0;
+                JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
             }
         }
 
@@ -139,6 +140,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;
@@ -162,6 +164,8 @@
             free(result);
             result = NULL;
         }
+    } else {
+        JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
     }
 
     error = GetLastError();
@@ -255,6 +259,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) {
@@ -287,6 +293,8 @@
                       rv = (*env)->NewString(env, cp, (jsize)wcslen(cp));
                     }
                     free(cp);
+                } else {
+                    JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
                 }
             } else
             if (wcanonicalizeWithPrefix(canonicalPrefix,
@@ -434,7 +442,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 {
@@ -624,6 +632,7 @@
     if (search_path == 0) {
         free (pathbuf);
         errno = ENOMEM;
+        JNU_ThrowOutOfMemoryError(env, "native memory allocation faiuled");
         return NULL;
     }
     wcscpy(search_path, pathbuf);
@@ -801,7 +810,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/src/windows/native/java/io/io_util_md.c	Fri Jun 20 17:33:28 2014 +0100
+++ b/src/windows/native/java/io/io_util_md.c	Fri Jun 20 17:43:13 2014 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -191,9 +191,9 @@
                 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;
+                    if (pathbuf == NULL) {
+                         JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
+                         return NULL;
                     }
                 } else {
                     pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
@@ -216,12 +216,17 @@
             return NULL;
         } else {
             pathbuf = (WCHAR*)malloc(sizeof(WCHAR));
-            pathbuf[0] = L'\0';
+            if (pathbuf != NULL) {
+                pathbuf[0] = L'\0';
+            } else {
+                JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
+                return NULL;
+            }
         }
     }
     if (pathbuf == 0) {
         if (!(*env)->ExceptionCheck(env)) {
-            JNU_ThrowOutOfMemoryError(env,  "native memory allocation failed");
+            JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
         }
         return NULL;
     }