Coverity: Spreadsheet module resource leaks fixed 32/18232/2
Dhruv Khattar [Thu, 9 Jun 2016 20:49:02 +0000 (01:49 +0530)]
Change-Id: I1d05038c8f977ae564a849e280eafe60d9c9eede

scilab/modules/spreadsheet/sci_gateway/c/gw_csv_helpers.c
scilab/modules/spreadsheet/sci_gateway/c/sci_csvRead.c
scilab/modules/spreadsheet/sci_gateway/c/sci_csvTextScan.c
scilab/modules/spreadsheet/sci_gateway/c/sci_csvWrite.c
scilab/modules/spreadsheet/sci_gateway/cpp/sci_xls_open.cpp

index f46b1f9..27c62c3 100644 (file)
@@ -100,6 +100,7 @@ char *csv_getArgumentAsStringWithEmptyManagement(void* pvApiCtx, int _iVar, cons
         }
         if (strlen(returnedValue) == 0)
         {
+            FREE(returnedValue);
             returnedValue = os_strdup(defaultValue);
         }
     }
index 186a80c..a80125e 100644 (file)
@@ -373,6 +373,8 @@ int sci_csvRead(char *fname, void* pvApiCtx)
 
                                     freeCsvResult(result);
                                     freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
+                                    freeComplexArray(ptrComplexArray);
+                                    ptrComplexArray = NULL;
                                     return 0;
                                 }
                             }
@@ -386,9 +388,9 @@ int sci_csvRead(char *fname, void* pvApiCtx)
                                 {
                                     sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, result->m, result->n, ptrComplexArray->realPart);
                                 }
-                                freeComplexArray(ptrComplexArray);
-                                ptrComplexArray = NULL;
                             }
+                            freeComplexArray(ptrComplexArray);
+                            ptrComplexArray = NULL;
                         }
                         break;
 
index b54c031..b098202 100644 (file)
@@ -216,9 +216,6 @@ int sci_csvTextScan(char *fname, void* pvApiCtx)
                     complexArray *ptrComplexArray = stringsToComplexArray((const char**)result->pstrValues, result->m * result->n, decimal, TRUE, &ierr);
                     if (ptrComplexArray == NULL)
                     {
-                        freeCsvResult(result);
-                        freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
-
                         if (ierr == STRINGTOCOMPLEX_ERROR)
                         {
                             Scierror(999, _("%s: can not convert data.\n"), fname);
@@ -227,6 +224,8 @@ int sci_csvTextScan(char *fname, void* pvApiCtx)
                         {
                             Scierror(999, _("%s: Memory allocation error.\n"), fname);
                         }
+                        freeCsvResult(result);
+                        freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
                         return 0;
                     }
 
@@ -289,13 +288,12 @@ int sci_csvTextScan(char *fname, void* pvApiCtx)
                     }
                 }
 
-                freeCsvResult(result);
-                freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
-
                 if (sciErr.iErr)
                 {
                     printError(&sciErr, 0);
                     Scierror(17, _("%s: Memory allocation error.\n"), fname);
+                    freeCsvResult(result);
+                    freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
                     return 0;
                 }
                 else
@@ -335,6 +333,7 @@ int sci_csvTextScan(char *fname, void* pvApiCtx)
     }
 
     freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
+    freeCsvResult(result);
     return 0;
 }
 // =============================================================================
index c799ae2..8e41530 100644 (file)
@@ -239,6 +239,7 @@ int sci_csvWrite(char *fname, void* pvApiCtx)
                                    separator,
                                    decimal,
                                    (const char**)pHeadersLines, nbHeadersLines);
+        freeAllocatedMatrixOfString(m1, n1, pStringValues);
     }
     else
     {
index 22df9a4..fbbcbab 100644 (file)
@@ -122,18 +122,22 @@ types::Function::ReturnValue sci_xls_open(types::typed_list &in, int _iRetCount,
             char* pstFile = wide_string_to_UTF8(filename_IN);
             Scierror(999, _("The file %s does not exist.\n"), pstFile);
             FREE(pstFile);
+            FREE(filename_IN);
+            filename_IN = NULL;
             return types::Function::Error;
         }
     }
+    else
+    {
+        Scierror(999, _("%s: Cannot read file name.\n"), "xls_open");
+        return types::Function::Error;
+    }
 
     TMPDIR = getTMPDIRW();
     wcscpy(TMP, TMPDIR);
 
-    if (TMPDIR)
-    {
-        FREE(TMPDIR);
-        TMPDIR = NULL;
-    }
+    FREE(TMPDIR);
+    TMPDIR = NULL;
 
     wcscat(TMP, sep);
     wcscat(TMP, xls_basename(filename_IN));
@@ -164,13 +168,8 @@ types::Function::ReturnValue sci_xls_open(types::typed_list &in, int _iRetCount,
             Scierror(999, _("%s: Cannot open file %s.\n"), "xls_open", pstFile);
         }
 
-        if (filename_IN)
-        {
-            FREE(filename_IN);
-            filename_IN = NULL;
-            FREE(pstFile);
-        }
-
+        FREE(filename_IN);
+        filename_IN = NULL;
         FREE(pstFile);
         return types::Function::Error;
     }
@@ -201,11 +200,8 @@ types::Function::ReturnValue sci_xls_open(types::typed_list &in, int _iRetCount,
         return types::Function::Error;
     }
 
-    if (filename_IN)
-    {
-        FREE(filename_IN);
-        filename_IN = NULL;
-    }
+    FREE(filename_IN);
+    filename_IN = NULL;
 
     xls_open(&iErr, &iId, &sst , &ns, &Sheetnames, &Abspos, &nsheets);