[Libreoffice-commits] core.git: jurt/source

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Sat Nov 3 14:21:04 UTC 2018


 jurt/source/pipe/com_sun_star_lib_connections_pipe_PipeConnection.c |   87 ++++------
 1 file changed, 43 insertions(+), 44 deletions(-)

New commits:
commit 882c7f4a1feb81ed74b4243d884ecc3a954e3bed
Author:     Mike Kaganski <mike.kaganski at collabora.com>
AuthorDate: Sat Nov 3 14:57:29 2018 +0300
Commit:     Mike Kaganski <mike.kaganski at collabora.com>
CommitDate: Sat Nov 3 15:20:43 2018 +0100

    tdf#120703 PVS: V547 Expression is always true
    
    This fixes possible leaks from missed pipe releases
    
    Change-Id: Ic3cdbd46e0eadc503e5711fe447d14bd5f0f7273
    Reviewed-on: https://gerrit.libreoffice.org/62804
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kaganski at collabora.com>

diff --git a/jurt/source/pipe/com_sun_star_lib_connections_pipe_PipeConnection.c b/jurt/source/pipe/com_sun_star_lib_connections_pipe_PipeConnection.c
index 5259bbcd5474..9b7806b8a07a 100644
--- a/jurt/source/pipe/com_sun_star_lib_connections_pipe_PipeConnection.c
+++ b/jurt/source/pipe/com_sun_star_lib_connections_pipe_PipeConnection.c
@@ -258,34 +258,36 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_closeJNI
 {
     enum {
         START   = 0,
-        INMONITOR
+        INMONITOR,
+        GOTPIPE,
     };
 
-    short       state   = START;
-    oslPipe     npipe;      /* native pipe */
-    jclass      tclass;     /* this class */
-    jfieldID    fid;        /* a field identifier */
+    short     state = START;
+    oslPipe   npipe = NULL;  /* native pipe */
+    jclass    tclass;        /* this class */
+    jfieldID  fid;           /* a field identifier */
 
     if ((*env)->MonitorEnter(env, obj_this) != 0)
     {
         ThrowException(env,
                        "java/lang/RuntimeException",
                        "native pipe cannot synchronize on the object");
-        goto error;
+        goto cleanup;
     }
     state   = INMONITOR;
 
     /* check connection state */
     npipe   = getPipe(env, obj_this);
     if ((*env)->ExceptionOccurred(env) != NULL)
-        goto error;
+        goto cleanup;
     if (npipe == NULL)
     {
         ThrowException(env,
                        "com/sun/star/io/IOException",
                        "native pipe is not connected");
-        goto error;
+        goto cleanup;
     }
+    state = GOTPIPE;
 
     /* remove the reference to the pipe */
     tclass  = (*env)->GetObjectClass(env, obj_this);
@@ -294,7 +296,7 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_closeJNI
         ThrowException(env,
                        "java/lang/RuntimeException",
                        "native pipe cannot find class");
-        goto error;
+        goto cleanup;
     }
 
     fid     = (*env)->GetFieldID(env, tclass, "_nPipeHandle", "J");
@@ -303,24 +305,24 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_closeJNI
         ThrowException(env,
                        "java/lang/RuntimeException",
                        "native pipe cannot find field");
-        goto error;
+        goto cleanup;
     }
 
     (*env)->SetLongField(env, obj_this, fid, (jlong)0);
 
-    /* release the pipe */
-    osl_closePipe(npipe);
-    osl_releasePipe(npipe);
-
     /* done */
-    (*env)->MonitorExit(env, obj_this);
-    return;
 
- error:
+ cleanup:
     switch (state)
     {
+        case GOTPIPE:
+            /* release the pipe */
+            osl_closePipe(npipe);
+            osl_releasePipe(npipe);
+            /* fall-through */
         case INMONITOR:
             (*env)->MonitorExit(env, obj_this);
+            /* fall-through */
         case START:
         default:
             break;
@@ -350,10 +352,11 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_readJNI
     };
 
     short       state   = START;
-    oslPipe     npipe;          /* native pipe */
+    oslPipe     npipe = NULL;   /* native pipe */
     void *      nbuff = NULL;   /* native read buffer */
     jbyteArray  bytes;          /* java read buffer */
     jint        nread;          /* number of bytes has been read */
+    jint        nreturn = -1;   /* actual return value */
 
     /* enter monitor */
     if ((*env)->MonitorEnter(env, obj_this) != 0)
@@ -361,20 +364,20 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_readJNI
         ThrowException(env,
                        "java/lang/RuntimeException",
                        "native pipe cannot synchronize on the object");
-        goto error;
+        goto cleanup;
     }
     state = INMONITOR;
 
     /* check connection state */
     npipe   = getPipe(env, obj_this);
     if ((*env)->ExceptionOccurred(env) != NULL)
-        goto error;
+        goto cleanup;
     if (npipe == NULL)
     {
         ThrowException(env,
                        "com/sun/star/io/IOException",
                        "native pipe is not connected");
-        goto error;
+        goto cleanup;
     }
 
     /* acquire pipe */
@@ -387,7 +390,7 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_readJNI
         ThrowException(env,
                        "java/lang/RuntimeException",
                        "native pipe out of memory");
-        goto error;
+        goto cleanup;
     }
 
     state = GOTBUFFER;
@@ -404,7 +407,7 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_readJNI
         ThrowException(env,
                        "java/lang/RuntimeException",
                        "native pipe cannot synchronize on the object");
-        goto error;
+        goto cleanup;
     }
 
     /* copy buffer */
@@ -416,7 +419,7 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_readJNI
             ThrowException(env,
                            "java/lang/RuntimeException",
                            "native pipe out of memory");
-            goto error;
+            goto cleanup;
         }
 
         /* save the data */
@@ -426,28 +429,26 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_readJNI
     }
 
     /* done */
-    free(nbuff);
-    if ( state >= ACQUIRED )
-        osl_releasePipe( npipe );
-
-    /* exit monitor */
-    (*env)->MonitorExit(env, obj_this);
-    return nread;
+    nreturn = nread;
 
- error:
+ cleanup:
     switch (state)
     {
         case GOTBUFFER:
             free(nbuff);
             /* fall-through */
+        case ACQUIRED:
+            osl_releasePipe(npipe);
+            /* fall-through */
         case INMONITOR:
             (*env)->MonitorExit(env, obj_this);
+            /* fall-through */
         case START:
         default:
             break;
     }
-    return -1;
-}
+    return nreturn;
+ }
 
 /*****************************************************************************/
 /*
@@ -480,20 +481,20 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_writeJNI
         ThrowException(env,
                        "java/lang/RuntimeException",
                        "native pipe cannot synchronize on the object");
-        goto error;
+        goto cleanup;
     }
     state   = INMONITOR;
 
     /* check connection state */
     npipe   = getPipe(env, obj_this);
     if ((*env)->ExceptionOccurred(env) != NULL)
-        goto error;
+        goto cleanup;
     if (npipe == NULL)
     {
         ThrowException(env,
                        "com/sun/star/io/IOException",
                        "native pipe is not connected");
-        goto error;
+        goto cleanup;
     }
 
     nwrite  = (*env)->GetArrayLength(env, buffer);
@@ -505,7 +506,7 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_writeJNI
             ThrowException(env,
                            "java/lang/RuntimeException",
                            "native pipe out of memory");
-            goto error;
+            goto cleanup;
         }
         state   = GOTBUFFER;
 
@@ -517,22 +518,19 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_writeJNI
             ThrowException(env,
                            "java/lang/RuntimeException",
                            "native pipe cannot synchronize on the object");
-            goto error;
+            goto cleanup;
         }
         if (count != nwrite)
         {
             ThrowException(env,
                            "com/sun/star/io/IOException",
                            "native pipe: failed to write");
-            goto error;
+            goto cleanup;
         }
     }
     /* done */
-    (*env)->ReleaseByteArrayElements(env, buffer, nbuff, JNI_ABORT);
-    (*env)->MonitorExit(env, obj_this);
-    return;
 
- error:
+ cleanup:
     switch (state)
     {
         case GOTBUFFER:
@@ -540,6 +538,7 @@ JNICALL Java_com_sun_star_lib_connections_pipe_PipeConnection_writeJNI
             /* fall through */
         case INMONITOR:
             (*env)->MonitorExit(env, obj_this);
+            /* fall through */
         case START:
         default:
             break;


More information about the Libreoffice-commits mailing list