changeset 7401:fd940a08ec2d

8055421: (fs) bad error handling in java.base/unix/native/libnio/fs/UnixNativeDispatcher.c Reviewed-by: martin, alanb
author igerasim
date Tue, 26 Aug 2014 15:55:35 +0400
parents 525b2ad2fc7c
children 70b7e2766d4a f40d035e338d
files src/solaris/native/sun/nio/fs/UnixNativeDispatcher.c
diffstat 1 files changed, 8 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/solaris/native/sun/nio/fs/UnixNativeDispatcher.c	Tue Aug 26 15:53:05 2014 +0100
+++ b/src/solaris/native/sun/nio/fs/UnixNativeDispatcher.c	Tue Aug 26 15:55:35 2014 +0400
@@ -278,7 +278,7 @@
     int res = -1;
 
     RESTARTABLE(dup((int)fd), res);
-    if (fd == -1) {
+    if (res == -1) {
         throwUnixException(env, errno);
     }
     return (jint)res;
@@ -306,13 +306,14 @@
 JNIEXPORT void JNICALL
 Java_sun_nio_fs_UnixNativeDispatcher_fclose(JNIEnv* env, jclass this, jlong stream)
 {
-    int res;
     FILE* fp = jlong_to_ptr(stream);
 
-    do {
-        res = fclose(fp);
-    } while (res == EOF && errno == EINTR);
-    if (res == EOF) {
+    /* NOTE: fclose() wrapper is only used with read-only streams.
+     * If it ever is used with write streams, it might be better to add
+     * RESTARTABLE(fflush(fp)) before closing, to make sure the stream
+     * is completely written even if fclose() failed.
+     */
+    if (fclose(fp) == EOF && errno != EINTR) {
         throwUnixException(env, errno);
     }
 }
@@ -610,11 +611,9 @@
 
 JNIEXPORT void JNICALL
 Java_sun_nio_fs_UnixNativeDispatcher_closedir(JNIEnv* env, jclass this, jlong dir) {
-    int err;
     DIR* dirp = jlong_to_ptr(dir);
 
-    RESTARTABLE(closedir(dirp), err);
-    if (errno == -1) {
+    if (closedir(dirp) == -1 && errno != EINTR) {
         throwUnixException(env, errno);
     }
 }