Coverity: tclsci module resource leaks fixed 62/18162/6
Dhruv Khattar [Fri, 13 May 2016 07:28:37 +0000 (12:28 +0530)]
Change-Id: I13ca6609b0beb1195038cc11598c15fbe3aaa188

scilab/modules/tclsci/sci_gateway/c/sci_TCL_EvalStr.c
scilab/modules/tclsci/sci_gateway/c/sci_TCL_ExistArray.c
scilab/modules/tclsci/sci_gateway/c/sci_TCL_ExistInterp.c
scilab/modules/tclsci/sci_gateway/c/sci_TCL_GetVar.c
scilab/modules/tclsci/sci_gateway/c/sci_TCL_GetVersion.c
scilab/modules/tclsci/sci_gateway/c/sci_TCL_SetVar.c
scilab/modules/tclsci/sci_gateway/c/sci_TCL_UpVar.c

index bcd6980..ea04e96 100644 (file)
@@ -25,6 +25,7 @@
 #include "GlobalTclInterp.h"
 #include "os_string.h"
 #include "api_scilab.h"
+#include "sci_malloc.h"
 
 /*--------------------------------------------------------------------------*/
 int sci_TCL_EvalStr(char *fname, void* pvApiCtx)
@@ -41,7 +42,7 @@ int sci_TCL_EvalStr(char *fname, void* pvApiCtx)
     {
         char *tclSlave = NULL;
         char **Str = NULL;
-        int m1, n1, i;
+        int m1, n1, i, j;
         int m2, n2;
         char **ReturnArrayString = NULL;
         int k = 0;
@@ -63,8 +64,8 @@ int sci_TCL_EvalStr(char *fname, void* pvApiCtx)
 
         if (!existsGlobalInterp())
         {
-            freeAllocatedMatrixOfString(m1, n1, Str);
             Scierror(999, _("%s: Error main TCL interpreter not initialized.\n"), fname);
+            freeAllocatedMatrixOfString(m1, n1, Str);
             return 0;
         }
 
@@ -77,6 +78,7 @@ int sci_TCL_EvalStr(char *fname, void* pvApiCtx)
                 if (sciErr.iErr)
                 {
                     printError(&sciErr, 0);
+                    freeAllocatedMatrixOfString(m1, n1, Str);
                     return 1;
                 }
 
@@ -84,13 +86,14 @@ int sci_TCL_EvalStr(char *fname, void* pvApiCtx)
                 if (getAllocatedSingleString(pvApiCtx, piAddrl2, &l2))
                 {
                     Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 2);
+                    freeAllocatedMatrixOfString(m1, n1, Str);
                     return 1;
                 }
 
                 if (!existsSlaveInterp((l2)))
                 {
-                    freeAllocatedMatrixOfString(m1, n1, Str);
                     Scierror(999, _("%s: No such slave interpreter.\n"), fname);
+                    freeAllocatedMatrixOfString(m1, n1, Str);
                     return 0;
                 }
                 tclSlave =  os_strdup((l2));
@@ -98,13 +101,13 @@ int sci_TCL_EvalStr(char *fname, void* pvApiCtx)
             }
             else
             {
-                freeAllocatedMatrixOfString(m1, n1, Str);
                 Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, 2);
+                freeAllocatedMatrixOfString(m1, n1, Str);
                 return 0;
             }
         }
 
-        ReturnArrayString = (char **) MALLOC(m1 * n1 * sizeof(char **));
+        ReturnArrayString = (char **) MALLOC(m1 * n1 * sizeof(char *));
 
         for (i = 0; i < m1 * n1 ; i++)
         {
@@ -132,6 +135,11 @@ int sci_TCL_EvalStr(char *fname, void* pvApiCtx)
                     trace = Tcl_GetVar(getTclInterp(), "errorInfo", TCL_GLOBAL_ONLY);
                 }
                 releaseTclInterp();
+                for (j = 0; j < k; ++j)
+                {
+                    FREE(ReturnArrayString[j]);
+                }
+                FREE(ReturnArrayString);
                 freeAllocatedMatrixOfString(m1, n1, Str);
 
                 {
@@ -148,6 +156,7 @@ int sci_TCL_EvalStr(char *fname, void* pvApiCtx)
                     Scierror(999, "%s, %s at line %i\n %s\n", fname, (char *)result, i + 1, (char *)trace);
                     releaseTclInterp();
                 }
+                FREE(tclSlave);
                 return 0;
             }
             else
@@ -163,6 +172,9 @@ int sci_TCL_EvalStr(char *fname, void* pvApiCtx)
         {
             printError(&sciErr, 0);
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
+            freeAllocatedMatrixOfString(m1, n1, ReturnArrayString);
+            freeAllocatedMatrixOfString(m1, n1, Str);
+            FREE(tclSlave);
             return 1;
         }
 
@@ -170,6 +182,7 @@ int sci_TCL_EvalStr(char *fname, void* pvApiCtx)
 
         freeAllocatedMatrixOfString(m1, n1, ReturnArrayString);
         freeAllocatedMatrixOfString(m1, n1, Str);
+        FREE(tclSlave);
 
         ReturnArguments(pvApiCtx);
     }
index 83cb883..ffeadbb 100644 (file)
@@ -60,8 +60,8 @@ int sci_TCL_ExistArray(char *fname, void* pvApiCtx)
 
         if (!existsGlobalInterp())
         {
-            freeAllocatedSingleString(VarName);
             Scierror(999, _("%s: Error main TCL interpreter not initialized.\n"), fname);
+            freeAllocatedSingleString(VarName);
             return 0;
         }
 
@@ -74,14 +74,15 @@ int sci_TCL_ExistArray(char *fname, void* pvApiCtx)
                 if (sciErr.iErr)
                 {
                     printError(&sciErr, 0);
+                    freeAllocatedSingleString(VarName);
                     return 1;
                 }
 
                 // Retrieve a matrix of double at position 2.
                 if (getAllocatedSingleString(pvApiCtx, piAddrl2, &l2))
                 {
-                    freeAllocatedSingleString(VarName);
                     Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 2);
+                    freeAllocatedSingleString(VarName);
                     return 1;
                 }
 
@@ -89,15 +90,15 @@ int sci_TCL_ExistArray(char *fname, void* pvApiCtx)
                 freeAllocatedSingleString(l2);
                 if (TCLinterpreter == NULL)
                 {
-                    freeAllocatedSingleString(VarName);
                     Scierror(999, _("%s: No such slave interpreter.\n"), fname);
+                    freeAllocatedSingleString(VarName);
                     return 0;
                 }
             }
             else
             {
-                freeAllocatedSingleString(VarName);
                 Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, 2);
+                freeAllocatedSingleString(VarName);
                 return 0;
             }
         }
index 5f7a0ac..8bfb107 100644 (file)
@@ -50,6 +50,7 @@ int sci_TCL_ExistInterp(char *fname, void* pvApiCtx)
         if (!existsGlobalInterp())
         {
             Scierror(999, _("%s: Error main TCL interpreter not initialized.\n"), fname);
+            freeAllocatedSingleString(InterpName);
             return 1;
         }
 
@@ -58,6 +59,7 @@ int sci_TCL_ExistInterp(char *fname, void* pvApiCtx)
         {
             printError(&sciErr, 0);
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
+            freeAllocatedSingleString(InterpName);
             return 1;
         }
 
index 9742514..8a87bae 100644 (file)
@@ -62,6 +62,7 @@ int sci_TCL_GetVar(char *fname, void* pvApiCtx)
         if (!existsGlobalInterp())
         {
             Scierror(999, _("%s: Error main TCL interpreter not initialized.\n"), fname);
+            freeAllocatedSingleString(VarName);
             return 0;
         }
 
@@ -74,6 +75,7 @@ int sci_TCL_GetVar(char *fname, void* pvApiCtx)
                 if (sciErr.iErr)
                 {
                     printError(&sciErr, 0);
+                    freeAllocatedSingleString(VarName);
                     return 1;
                 }
 
@@ -81,6 +83,7 @@ int sci_TCL_GetVar(char *fname, void* pvApiCtx)
                 if (getAllocatedSingleString(pvApiCtx, piAddrl2, &l2))
                 {
                     Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 2);
+                    freeAllocatedSingleString(VarName);
                     return 1;
                 }
 
@@ -89,12 +92,14 @@ int sci_TCL_GetVar(char *fname, void* pvApiCtx)
                 if (TCLinterpreter == NULL)
                 {
                     Scierror(999, _("%s: No such slave interpreter.\n"), fname);
+                    freeAllocatedSingleString(VarName);
                     return 0;
                 }
             }
             else
             {
                 Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, 2);
+                freeAllocatedSingleString(VarName);
                 return 0;
             }
         }
@@ -125,6 +130,9 @@ int sci_TCL_GetVar(char *fname, void* pvApiCtx)
                     {
                         printError(&sciErr, 0);
                         Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                        freeArrayOfString(ReturnArrayString, nb_lines * nb_columns);
+                        freeArrayOfString(index_list, nb_lines * nb_columns);
+                        freeAllocatedSingleString(VarName);
                         return 1;
                     }
 
@@ -136,14 +144,16 @@ int sci_TCL_GetVar(char *fname, void* pvApiCtx)
                 }
                 else
                 {
-                    freeArrayOfString(index_list, nb_lines * nb_columns);
                     Scierror(999, _("%s: No more memory.\n"), fname);
+                    freeArrayOfString(index_list, nb_lines * nb_columns);
+                    freeAllocatedSingleString(VarName);
                     return 0;
                 }
             }
             else
             {
                 Scierror(999, _("%s: No more memory.\n"), fname);
+                freeAllocatedSingleString(VarName);
                 return 0;
             }
         }
@@ -158,6 +168,7 @@ int sci_TCL_GetVar(char *fname, void* pvApiCtx)
                 {
                     printError(&sciErr, 0);
                     Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                    freeAllocatedSingleString(VarName);
                     return 1;
                 }
 
@@ -171,16 +182,17 @@ int sci_TCL_GetVar(char *fname, void* pvApiCtx)
             }
             else
             {
-                releaseTclInterp();
                 Scierror(999, _("%s: Could not read Tcl Variable.\n"), "TCL_GetVar");
+                freeAllocatedSingleString(VarName);
+                releaseTclInterp();
                 return 0;
             }
         }
     }
     else
     {
-        releaseTclInterp();
         Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, 1);
+        releaseTclInterp();
         return 0;
     }
 
index b334e6d..947a28b 100644 (file)
@@ -111,6 +111,7 @@ int sci_TCL_GetVersion(char *fname, void* pvApiCtx)
                 {
                     printError(&sciErr, 0);
                     Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                    freeAllocatedSingleString(Param);
                     return 1;
                 }
 
index 7946367..4f67111 100644 (file)
@@ -163,15 +163,16 @@ int sci_TCL_SetVar(char *fname, void* pvApiCtx)
         sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrl1);
         if (sciErr.iErr)
         {
-            freeAllocatedSingleString(VarName);
             printError(&sciErr, 0);
+            freeAllocatedSingleString(VarName);
             return 1;
         }
 
         if (isVarComplex(pvApiCtx, piAddrl1))
         {
-            releaseTclInterp();
             Scierror(999, _("This function doesn't work with Complex.\n"));
+            freeAllocatedSingleString(VarName);
+            releaseTclInterp();
             return 0;
         }
 
@@ -179,17 +180,17 @@ int sci_TCL_SetVar(char *fname, void* pvApiCtx)
         sciErr = getMatrixOfDouble(pvApiCtx, piAddrl1, &m1, &n1, &l1);
         if (sciErr.iErr)
         {
-            freeAllocatedSingleString(VarName);
             printError(&sciErr, 0);
             Scierror(202, _("%s: Wrong type for argument %d: A real expected.\n"), fname, 2);
+            freeAllocatedSingleString(VarName);
             return 1;
         }
 
         if ( (m1 == 0) && (n1 == 0) )
         {
+            Scierror(999, _("[] doesn't work with Tcl/Tk.\n"));
             freeAllocatedSingleString(VarName);
             releaseTclInterp();
-            Scierror(999, _("[] doesn't work with Tcl/Tk.\n"));
             return 0;
         }
 
index b142382..f03b192 100644 (file)
@@ -60,8 +60,8 @@ int sci_TCL_UpVar (char *fname, void* pvApiCtx)
         sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrl2);
         if (sciErr.iErr)
         {
-            freeAllocatedSingleString(sourceName);
             printError(&sciErr, 0);
+            freeAllocatedSingleString(sourceName);
             return 1;
         }
 
@@ -75,10 +75,10 @@ int sci_TCL_UpVar (char *fname, void* pvApiCtx)
 
         if (getTclInterp() == NULL)
         {
+            Scierror(999, _("%s: Error main TCL interpreter not initialized.\n"), fname);
             freeAllocatedSingleString(destName);
             freeAllocatedSingleString(sourceName);
             releaseTclInterp();
-            Scierror(999, _("%s: Error main TCL interpreter not initialized.\n"), fname);
             return 0;
         }
         releaseTclInterp();
@@ -92,17 +92,18 @@ int sci_TCL_UpVar (char *fname, void* pvApiCtx)
                 sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddrl3);
                 if (sciErr.iErr)
                 {
-                    freeAllocatedSingleString(sourceName);
                     printError(&sciErr, 0);
+                    freeAllocatedSingleString(sourceName);
+                    freeAllocatedSingleString(destName);
                     return 1;
                 }
 
                 // Retrieve a matrix of double at position 3.
                 if (getAllocatedSingleString(pvApiCtx, piAddrl3, &l3))
                 {
+                    Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 3);
                     freeAllocatedSingleString(destName);
                     freeAllocatedSingleString(sourceName);
-                    Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 3);
                     return 1;
                 }
 
@@ -111,17 +112,17 @@ int sci_TCL_UpVar (char *fname, void* pvApiCtx)
                 releaseTclInterp();
                 if (TCLinterpreter == NULL)
                 {
+                    Scierror(999, _("%s: No such slave interpreter.\n"), fname);
                     freeAllocatedSingleString(destName);
                     freeAllocatedSingleString(sourceName);
-                    Scierror(999, _("%s: No such slave interpreter.\n"), fname);
                     return 0;
                 }
             }
             else
             {
+                Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, 3);
                 freeAllocatedSingleString(destName);
                 freeAllocatedSingleString(sourceName);
-                Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, 3);
                 return 0;
             }
         }