Coverity: jvm module memory errors fixed 46/18346/2
Dhruv Khattar [Sat, 2 Jul 2016 20:57:48 +0000 (01:57 +0530)]
Change-Id: I4254771b0ff29c64206038f0c01941ed39f96b33

scilab/modules/jvm/src/c/InitializeJVM.c
scilab/modules/jvm/src/c/JVM.c
scilab/modules/jvm/src/c/JVM_Unix.c
scilab/modules/jvm/src/c/getJvmOptions.c
scilab/modules/jvm/src/c/loadClasspath.c
scilab/modules/jvm/src/c/loadLibrarypath.c
scilab/modules/jvm/src/c/loadOnUseClassPath.c

index 6124d68..63af0c6 100644 (file)
@@ -91,11 +91,8 @@ BOOL InitializeJVM(void)
         }
     }
 
-    if (sciPath)
-    {
-        FREE(sciPath);
-        sciPath = NULL;
-    }
+    FREE(sciPath);
+    sciPath = NULL;
 
     if (!bOK)
     {
@@ -110,11 +107,8 @@ static void DoLoadClasspathInEtc(char *sciPath)
     char *classpathfile = (char*)MALLOC(sizeof(char) * (strlen(sciPath) + strlen(XMLCLASSPATH) + 1));
     sprintf(classpathfile, XMLCLASSPATH, sciPath);
     LoadClasspath(classpathfile);
-    if (classpathfile)
-    {
-        FREE(classpathfile);
-        classpathfile = NULL;
-    }
+    FREE(classpathfile);
+    classpathfile = NULL;
 }
 /*--------------------------------------------------------------------------*/
 static void DoLoadLibrarypathInEtc(char *sciPath)
@@ -122,11 +116,8 @@ static void DoLoadLibrarypathInEtc(char *sciPath)
     char *librarypathfile = (char*)MALLOC(sizeof(char) * (strlen(sciPath) + strlen(XMLLIBRARYPATH) + 1));
     sprintf(librarypathfile, XMLLIBRARYPATH, sciPath);
     LoadLibrarypath(librarypathfile);
-    if (librarypathfile)
-    {
-        FREE(librarypathfile);
-        librarypathfile = NULL;
-    }
+    FREE(librarypathfile);
+    librarypathfile = NULL;
 }
 /*--------------------------------------------------------------------------*/
 BOOL ExecuteInitialHooks(void)
index cebf3b8..a2d34e6 100644 (file)
@@ -200,20 +200,14 @@ BOOL startJVM(char *SCI_PATH)
 #else
                 fprintf(stderr, _("\nImpossible to read %s.\n"), jvm_options_filename);
 #endif
-                if (jvm_options_filename)
-                {
-                    FREE(jvm_options_filename);
-                    jvm_options_filename = NULL;
-                }
+                FREE(jvm_options_filename);
+                jvm_options_filename = NULL;
                 exit(1);
             }
             else
             {
-                if (jvm_options_filename)
-                {
-                    FREE(jvm_options_filename);
-                    jvm_options_filename = NULL;
-                }
+                FREE(jvm_options_filename);
+                jvm_options_filename = NULL;
 
                 HadAlreadyJavaVm = FALSE;
 
index da8bd21..66ea638 100644 (file)
@@ -65,11 +65,8 @@ BOOL LoadDynLibJVM(char *SCILAB_PATH)
     if (LoadFunctionsJVM(JVMLibFullName) == NULL)
     {
         /* 2. search in LD_LIBRARY_PATH */
-        if (JVMLibFullName)
-        {
-            FREE(JVMLibFullName);
-            JVMLibFullName = NULL;
-        };
+        FREE(JVMLibFullName);
+        JVMLibFullName = NULL;
 
         JVMLibFullName = (char *)MALLOC((strlen(LIBJAVANAME) + strlen(SHARED_LIB_EXT) + 1) * sizeof(char));
         sprintf(JVMLibFullName, "%s%s", LIBJAVANAME, SHARED_LIB_EXT);
@@ -84,11 +81,8 @@ BOOL LoadDynLibJVM(char *SCILAB_PATH)
         bOK = TRUE;
     }
 
-    if (JVMLibFullName)
-    {
-        FREE(JVMLibFullName);
-        JVMLibFullName = NULL;
-    };
+    FREE(JVMLibFullName);
+    JVMLibFullName = NULL;
 
     return bOK;
 
@@ -122,16 +116,14 @@ static JavaVM *SearchCreatedJavaVMEmbedded(char *SCILAB_PATH)
         if (res != JNI_OK)
         {
             fprintf(stderr, "\nJNI_GetCreatedJavaVMs failed to detect any started Java VM.\n");
+            FREE(JVMLibFullName);
             return NULL;
         }
 
         if (jvm_count == 1)
         {
-            if (JVMLibFullName)
-            {
-                FREE(JVMLibFullName);
-                JVMLibFullName = NULL;
-            }
+            FREE(JVMLibFullName);
+            JVMLibFullName = NULL;
             return jvm;
         }
         else
@@ -139,11 +131,8 @@ static JavaVM *SearchCreatedJavaVMEmbedded(char *SCILAB_PATH)
             jvm = NULL;
         }
     }
-    if (JVMLibFullName)
-    {
-        FREE(JVMLibFullName);
-        JVMLibFullName = NULL;
-    }
+    FREE(JVMLibFullName);
+    JVMLibFullName = NULL;
     return jvm;
 }
 
@@ -166,15 +155,13 @@ static JavaVM *SearchCreatedJavaVMPath(void)
         if (res != JNI_OK)
         {
             fprintf(stderr, "\nJNI_GetCreatedJavaVMs failed to detect any started Java VM.\n");
+            FREE(JVMLibFullName);
             return NULL;
         }
         if (jvm_count == 1)     /* We could update this to behave differently when two (or more) JVMs are already started */
         {
-            if (JVMLibFullName)
-            {
-                FREE(JVMLibFullName);
-                JVMLibFullName = NULL;
-            };
+            FREE(JVMLibFullName);
+            JVMLibFullName = NULL;
             return jvm;
         }
         else
@@ -182,11 +169,8 @@ static JavaVM *SearchCreatedJavaVMPath(void)
             jvm = NULL;
         }
     }
-    if (JVMLibFullName)
-    {
-        FREE(JVMLibFullName);
-        JVMLibFullName = NULL;
-    };
+    FREE(JVMLibFullName);
+    JVMLibFullName = NULL;
     return jvm;
 }
 
index 504b157..2c484fe 100644 (file)
@@ -65,11 +65,8 @@ JavaVMOption * getJvmOptions(char *SCI_PATH, char *filename_xml_conf, int *size_
             if (doc == NULL)
             {
                 fprintf(stderr, _("Error: Could not parse file %s.\n"), filename_xml_conf);
-                if (encoding)
-                {
-                    FREE(encoding);
-                    encoding = NULL;
-                }
+                FREE(encoding);
+                encoding = NULL;
                 *size_JavaVMOption = 0;
                 return NULL;
             }
@@ -87,7 +84,6 @@ JavaVMOption * getJvmOptions(char *SCI_PATH, char *filename_xml_conf, int *size_
             {
                 /* the Xpath has been understood and there are node */
                 int i;
-                char heapSizeUsed = 0;
                 char *heapSize = getJavaHeapSize();
 
                 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
@@ -104,8 +100,7 @@ JavaVMOption * getJvmOptions(char *SCI_PATH, char *filename_xml_conf, int *size_
                             const char *str = (const char*)attrib->children->content;
                             if (strstr(str, "-Xmx") == str && heapSize)
                             {
-                                jvm_option_string = heapSize;
-                                heapSizeUsed = 1;
+                                jvm_option_string = os_strdup(heapSize);
                             }
                             else
                             {
@@ -128,7 +123,7 @@ JavaVMOption * getJvmOptions(char *SCI_PATH, char *filename_xml_conf, int *size_
                         }
 
                         option_string_sci_path = strsub(option_string_path_separator, "$SCILAB", SCI_PATH);
-                        if (option_string_sci_path)
+                        if (option_string_path_separator)
                         {
                             FREE(option_string_path_separator);
                         }
@@ -140,10 +135,7 @@ JavaVMOption * getJvmOptions(char *SCI_PATH, char *filename_xml_conf, int *size_
 
                 }
 
-                if (!heapSizeUsed)
-                {
-                    FREE(heapSize);
-                }
+                FREE(heapSize);
             }
 
             if (xpathObj)
@@ -175,11 +167,8 @@ JavaVMOption * getJvmOptions(char *SCI_PATH, char *filename_xml_conf, int *size_
 #undef HEADLESS
             }
 
-            if (encoding)
-            {
-                FREE(encoding);
-                encoding = NULL;
-            }
+            FREE(encoding);
+            encoding = NULL;
 
             *size_JavaVMOption = indice;
             return jvm_options;
@@ -188,11 +177,8 @@ JavaVMOption * getJvmOptions(char *SCI_PATH, char *filename_xml_conf, int *size_
         {
             fprintf(stderr, _("Error: Not a valid configuration file %s (encoding not '%s') Encoding '%s' found.\n"), filename_xml_conf, "utf-8", encoding);
         }
-        if (encoding)
-        {
-            FREE(encoding);
-            encoding = NULL;
-        }
+        FREE(encoding);
+        encoding = NULL;
     }
     return NULL;
 }
index 2d29b53..e56d8ba 100644 (file)
@@ -87,11 +87,9 @@ BOOL LoadClasspath(char *xmlfilename)
             if (ClassPathxmlDocPtr == NULL)
             {
                 fprintf(stderr, _("Error: could not parse file %s\n"), xmlfilename);
-                if (encoding)
-                {
-                    FREE(encoding);
-                    encoding = NULL;
-                }
+                FREE(encoding);
+                encoding = NULL;
+                FREE(XPath);
                 return bOK;
             }
 
@@ -193,21 +191,15 @@ BOOL LoadClasspath(char *xmlfilename)
             {
                 xmlXPathFreeContext(xpathCtxt);
             }
-            if (XPath)
-            {
-                FREE(XPath);
-                XPath = NULL;
-            }
+            FREE(XPath);
+            XPath = NULL;
         }
         else
         {
             fprintf(stderr, _("Error : Not a valid classpath file %s (encoding not 'utf-8') Encoding '%s' found\n"), xmlfilename, encoding);
         }
-        if (encoding)
-        {
-            FREE(encoding);
-            encoding = NULL;
-        }
+        FREE(encoding);
+        encoding = NULL;
     }
     else
     {
index fe67501..0f06d2b 100644 (file)
@@ -59,11 +59,8 @@ BOOL LoadLibrarypath(char *xmlfilename)
             if (doc == NULL)
             {
                 fprintf(stderr, _("Error: could not parse file %s\n"), xmlfilename);
-                if (encoding)
-                {
-                    FREE(encoding);
-                    encoding = NULL;
-                }
+                FREE(encoding);
+                encoding = NULL;
                 return bOK;
             }
 
@@ -146,11 +143,8 @@ BOOL LoadLibrarypath(char *xmlfilename)
         {
             fprintf(stderr, _("Error : Not a valid path file %s (encoding not 'utf-8') Encoding '%s' found\n"), xmlfilename, encoding);
         }
-        if (encoding)
-        {
-            FREE(encoding);
-            encoding = NULL;
-        }
+        FREE(encoding);
+        encoding = NULL;
     }
     return bOK;
 }
index 0868165..3c4043a 100644 (file)
@@ -61,17 +61,11 @@ BOOL loadOnUseClassPath(char const* tag)
         if (doc == NULL)
         {
             fprintf(stderr, _("Error: could not parse file %s\n"), classpathfile);
-            if (XPath)
-            {
-                FREE(XPath);
-                XPath = NULL;
-            }
+            FREE(XPath);
+            XPath = NULL;
 
-            if (classpathfile)
-            {
-                FREE(classpathfile);
-                classpathfile = NULL;
-            }
+            FREE(classpathfile);
+            classpathfile = NULL;
 
             FREE(sciPath);
             return bOK;
@@ -131,11 +125,8 @@ BOOL loadOnUseClassPath(char const* tag)
         {
             xmlXPathFreeContext(xpathCtxt);
         }
-        if (XPath)
-        {
-            FREE(XPath);
-            XPath = NULL;
-        }
+        FREE(XPath);
+        XPath = NULL;
 
     }
     else
@@ -143,16 +134,10 @@ BOOL loadOnUseClassPath(char const* tag)
         fprintf(stderr, _("Warning: could not find classpath declaration file %s.\n"), classpathfile);
     }
 
-    if (classpathfile)
-    {
-        FREE(classpathfile);
-        classpathfile = NULL;
-    }
-    if (sciPath)
-    {
-        FREE(sciPath);
-        sciPath = NULL;
-    }
+    FREE(classpathfile);
+    classpathfile = NULL;
+    FREE(sciPath);
+    sciPath = NULL;
 
     return FALSE;
 }