spreadsheet: fix memory leaks ( thank coverity ) 89/12789/2
Antoine ELIAS [Wed, 2 Oct 2013 12:30:03 +0000 (14:30 +0200)]
Coverity ID: 1098417, 1098418, 1098423, 1098488, 1098489, 1098706, 1098768, 1098926, 1098927, 1098928, 1099019, 1099020
             1099021, 1099023, 1099029, 1099389, 1099532, 1099533, 1099534, 1099536, 1099537, 1099538, 1099539

Change-Id: I1b23e19730048d7e937100b3eb6b1c5adfc43841

scilab/modules/spreadsheet/sci_gateway/c/gw_csv_helpers.c
scilab/modules/spreadsheet/sci_gateway/c/sci_csvDefault.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/c/sci_xls_open.c
scilab/modules/spreadsheet/src/c/checkCsvWriteFormat.c
scilab/modules/spreadsheet/src/c/csvRead.c
scilab/modules/spreadsheet/src/c/splitLine.c

index d771367..8141c80 100644 (file)
@@ -87,6 +87,11 @@ char *csv_getArgumentAsStringWithEmptyManagement(void* _pvCtx, int _iVar, const
         *iErr = getAllocatedSingleString(pvApiCtx, piAddressVar, &returnedValue);
         if (*iErr)
         {
+            if (returnedValue)
+            {
+                FREE(returnedValue);
+            }
+
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return NULL;
         }
@@ -141,6 +146,11 @@ char *csv_getArgumentAsString(void* _pvCtx, int _iVar,
     *iErr = getAllocatedSingleString(pvApiCtx, piAddressVar, &returnedValue);
     if (*iErr)
     {
+        if (returnedValue)
+        {
+            FREE(returnedValue);
+        }
+
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
         return NULL;
     }
@@ -278,6 +288,10 @@ char **csv_getArgumentAsMatrixOfString(void* _pvCtx, int _iVar,
     *iErr = getAllocatedMatrixOfString(pvApiCtx, piAddressVar, &m_, &n_, &pStringValues);
     if (*iErr != 0)
     {
+        if (pStringValues)
+        {
+            FREE(pStringValues);
+        }
         return NULL;
     }
 
index 425cf58..3802df2 100644 (file)
@@ -155,6 +155,10 @@ static int sci_csvDefault_one_rhs(char *fname)
     fieldname = csv_getArgumentAsString(pvApiCtx, 1, fname, &iErr);
     if (iErr)
     {
+        if (fieldname)
+        {
+            FREE(fieldname);
+        }
         return 0;
     }
 
@@ -277,6 +281,10 @@ static int sci_csvDefault_two_rhs(char *fname)
     fieldname = csv_getArgumentAsString(pvApiCtx, 1, fname, &iErr);
     if (iErr)
     {
+        if (fieldvalue)
+        {
+            FREE(fieldvalue);
+        }
         return 0;
     }
 
@@ -355,6 +363,12 @@ static int sci_csvDefault_two_rhs(char *fname)
                 FREE(fieldname);
                 fieldname = NULL;
             }
+
+            if (fieldvalue)
+            {
+                FREE(fieldvalue);
+                fieldvalue = NULL;
+            }
             return 0;
         }
     }
index b33b9e9..86b5f05 100644 (file)
@@ -31,8 +31,7 @@
 #include "gw_csv_helpers.h"
 #include "getRange.h"
 
-static void releaseObjects(csvResult *result, char *filename, char *conversion);
-
+static void freeVar(char** filename, char** separator, char** decimal, char** conversion, int** iRange, char*** toreplace, int sizeReplace, char** regexp);
 /* ==================================================================== */
 #define CONVTOSTR "string"
 #define CONVTODOUBLE "double"
@@ -72,27 +71,20 @@ int sci_csvRead(char *fname, unsigned long fname_len)
         iRange = csv_getArgumentAsMatrixofIntFromDouble(pvApiCtx, 7, fname, &m7, &n7, &iErr);
         if (iErr)
         {
+            freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, 0, &regexp);
             return 0;
         }
 
         if ((m7 * n7 != SIZE_RANGE_SUPPORTED) )
         {
-            if (iRange)
-            {
-                FREE(iRange);
-                iRange = NULL;
-            }
+            freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, 0, &regexp);
             Scierror(999, _("%s: Wrong size for input argument #%d: Four entries expected.\n"), fname, 7);
             return 0;
         }
 
         if ((m7 != 1) && (n7 != 1))
         {
-            if (iRange)
-            {
-                FREE(iRange);
-                iRange = NULL;
-            }
+            freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, 0, &regexp);
             Scierror(999, _("%s: Wrong size for input argument #%d: A column or row vector expected.\n"), fname, 7);
             return 0;
         }
@@ -103,11 +95,7 @@ int sci_csvRead(char *fname, unsigned long fname_len)
         }
         else
         {
-            if (iRange)
-            {
-                FREE(iRange);
-                iRange = NULL;
-            }
+            freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, 0, &regexp);
             Scierror(999, _("%s: Wrong value for input argument #%d: Inconsistent range.\n"), fname, 7);
             return 0;
         }
@@ -125,8 +113,10 @@ int sci_csvRead(char *fname, unsigned long fname_len)
                 regexp = NULL;
             }
         }
+
         if (iErr)
         {
+            freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, 0, &regexp);
             return 0;
         }
     }
@@ -156,23 +146,13 @@ int sci_csvRead(char *fname, unsigned long fname_len)
             toreplace = csv_getArgumentAsMatrixOfString(pvApiCtx, 5, fname, &m5, &n5, &iErr);
             if (iErr)
             {
-                if (regexp)
-                {
-                    FREE(regexp);
-                    regexp = NULL;
-                }
+                freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, m5 * n5, &regexp);
                 return 0;
             }
 
             if (n5 != 2)
             {
-                if (regexp)
-                {
-                    FREE(regexp);
-                    regexp = NULL;
-                }
-                freeArrayOfString(toreplace, m5 * n5);
-                toreplace = NULL;
+                freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, m5 * n5, &regexp);
                 Scierror(999, _("%s: Wrong size for input argument #%d.\n"), fname, 5);
                 return 0;
             }
@@ -191,26 +171,13 @@ int sci_csvRead(char *fname, unsigned long fname_len)
         conversion = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 4, fname, getCsvDefaultConversion(), &iErr);
         if (iErr)
         {
-            FREE(regexp);
+            freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
             return 0;
         }
+
         if (!((strcmp(conversion, CONVTOSTR) == 0) || (strcmp(conversion, CONVTODOUBLE) == 0)))
         {
-            if (regexp)
-            {
-                FREE(regexp);
-                regexp = NULL;
-            }
-            if (toreplace)
-            {
-                freeArrayOfString(toreplace, nbElementsToReplace * 2);
-                toreplace = NULL;
-            }
-            if (conversion)
-            {
-                FREE(conversion);
-                conversion = NULL;
-            }
+            freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
             Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' string expected.\n"), fname, 4, "double", "string");
             return 0;
         }
@@ -236,26 +203,7 @@ int sci_csvRead(char *fname, unsigned long fname_len)
         decimal = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 3, fname, getCsvDefaultDecimal(), &iErr);
         if (iErr)
         {
-            if (regexp)
-            {
-                FREE(regexp);
-                regexp = NULL;
-            }
-            if (conversion)
-            {
-                FREE(conversion);
-                conversion = NULL;
-            }
-            if (toreplace)
-            {
-                freeArrayOfString(toreplace, nbElementsToReplace * 2);
-                toreplace = NULL;
-            }
-            if (decimal)
-            {
-                FREE(decimal);
-                decimal = NULL;
-            }
+            freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
             return 0;
         }
     }
@@ -270,31 +218,7 @@ int sci_csvRead(char *fname, unsigned long fname_len)
         separator = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 2, fname, getCsvDefaultSeparator(), &iErr);
         if (iErr)
         {
-            if (regexp)
-            {
-                FREE(regexp);
-                regexp = NULL;
-            }
-            if (toreplace)
-            {
-                freeArrayOfString(toreplace, nbElementsToReplace * 2);
-                toreplace = NULL;
-            }
-            if (conversion)
-            {
-                FREE(conversion);
-                conversion = NULL;
-            }
-            if (decimal)
-            {
-                FREE(decimal);
-                decimal = NULL;
-            }
-            if (separator)
-            {
-                FREE(separator);
-                separator = NULL;
-            }
+            freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
             return 0;
         }
     }
@@ -315,60 +239,12 @@ int sci_csvRead(char *fname, unsigned long fname_len)
     filename = csv_getArgumentAsString(pvApiCtx, 1, fname, &iErr);
     if (iErr)
     {
-        if (regexp)
-        {
-            FREE(regexp);
-            regexp = NULL;
-        }
-        if (toreplace)
-        {
-            freeArrayOfString(toreplace, nbElementsToReplace * 2);
-            toreplace = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        if (conversion)
-        {
-            FREE(conversion);
-            conversion = NULL;
-        }
-        if (decimal)
-        {
-            FREE(decimal);
-            decimal = NULL;
-        }
-        if (filename)
-        {
-            FREE(filename);
-            filename = NULL;
-        }
+        freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
         return 0;
     }
 
     result = csvRead(filename, separator, decimal, (const char**)toreplace, nbElementsToReplace * 2, regexp);
-    if (regexp)
-    {
-        FREE(regexp);
-        regexp = NULL;
-    }
-    if (toreplace)
-    {
-        freeArrayOfString(toreplace, nbElementsToReplace * 2);
-        toreplace = NULL;
-    }
-    if (separator)
-    {
-        FREE(separator);
-        separator = NULL;
-    }
-    if (decimal)
-    {
-        FREE(decimal);
-        decimal = NULL;
-    }
+    freeVar(NULL, &separator, &decimal, NULL, NULL, &toreplace, nbElementsToReplace, &regexp);
 
     if (result)
     {
@@ -411,7 +287,9 @@ int sci_csvRead(char *fname, unsigned long fname_len)
                             {
                                 Scierror(999, _("%s: Memory allocation error.\n"), fname);
                             }
-                            releaseObjects(result, filename, conversion);
+
+                            freeCsvResult(result);
+                            freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
                             return 0;
                         }
                     }
@@ -427,7 +305,7 @@ int sci_csvRead(char *fname, unsigned long fname_len)
 
                     if (ptrComplexArray == NULL)
                     {
-                        releaseObjects(result, filename, conversion);
+                        freeCsvResult(result);
                         if (ierr == STRINGTOCOMPLEX_ERROR)
                         {
                             Scierror(999, _("%s: can not convert data.\n"), fname);
@@ -436,6 +314,8 @@ int sci_csvRead(char *fname, unsigned long fname_len)
                         {
                             Scierror(999, _("%s: Memory allocation error.\n"), fname);
                         }
+
+                        freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
                         return 0;
                     }
 
@@ -472,7 +352,9 @@ int sci_csvRead(char *fname, unsigned long fname_len)
                                     {
                                         Scierror(999, _("%s: Memory allocation error.\n"), fname);
                                     }
-                                    releaseObjects(result, filename, conversion);
+
+                                    freeCsvResult(result);
+                                    freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
                                     return 0;
                                 }
                             }
@@ -495,14 +377,16 @@ int sci_csvRead(char *fname, unsigned long fname_len)
                         case STRINGTOCOMPLEX_MEMORY_ALLOCATION:
                         {
                             Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                            releaseObjects(result, filename, conversion);
+                            freeCsvResult(result);
+                            freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
                             return 0;
                         }
                         default:
                         case STRINGTOCOMPLEX_ERROR:
                         {
                             Scierror(999, _("%s: can not convert data.\n"), fname);
-                            releaseObjects(result, filename, conversion);
+                            freeCsvResult(result);
+                            freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
                             return 0;
                         }
                     }
@@ -511,7 +395,8 @@ int sci_csvRead(char *fname, unsigned long fname_len)
                 if (sciErr.iErr)
                 {
                     Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                    releaseObjects(result, filename, conversion);
+                    freeCsvResult(result);
+                    freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
                     return 0;
                 }
                 else
@@ -524,7 +409,8 @@ int sci_csvRead(char *fname, unsigned long fname_len)
                         if (sciErr.iErr)
                         {
                             Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                            releaseObjects(result, filename, conversion);
+                            freeCsvResult(result);
+                            freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
                             return 0;
                         }
                         LhsVar(2) = Rhs + 2;
@@ -570,23 +456,55 @@ int sci_csvRead(char *fname, unsigned long fname_len)
     {
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
     }
-    releaseObjects(result, filename, conversion);
-    if (iRange)
-    {
-        FREE(iRange);
-        iRange = NULL;
-    }
+
+    freeCsvResult(result);
+
+    freeVar(&filename, &separator, &decimal, &conversion, &iRange, &toreplace, nbElementsToReplace, &regexp);
 
     return 0;
 }
 /* ==================================================================== */
-static void releaseObjects(csvResult *result, char* filename, char* conversion)
+static void freeVar(char** filename, char** separator, char** decimal, char** conversion, int** iRange, char*** toreplace, int sizeReplace, char** regexp)
 {
-    freeCsvResult(result);
+    if (filename && *filename)
+    {
+        FREE(*filename);
+        *filename = NULL;
+    }
+
+    if (separator && *separator)
+    {
+        FREE(*separator);
+        *separator = NULL;
+    }
 
-    FREE(filename);
-    filename = NULL;
+    if (decimal && *decimal)
+    {
+        FREE(*decimal);
+        *decimal = NULL;
+    }
 
-    FREE(conversion);
-    conversion = NULL;
+    if (conversion && *conversion)
+    {
+        FREE(*conversion);
+        *conversion = NULL;
+    }
+
+    if (iRange && *iRange)
+    {
+        FREE(*iRange);
+        *iRange = NULL;
+    }
+
+    if (toreplace && *toreplace)
+    {
+        freeArrayOfString(*toreplace, sizeReplace);
+        *toreplace = NULL;
+    }
+
+    if (regexp && *regexp)
+    {
+        FREE(*regexp);
+        *regexp = NULL;
+    }
 }
index f52e593..4a9f836 100644 (file)
@@ -29,6 +29,7 @@
 #include "getRange.h"
 #include "gw_csv_helpers.h"
 
+static void freeVar(char*** text, int sizeText, int** lengthText, char** separator, char** decimal, char** conversion, int** iRange);
 // =============================================================================
 #define CONVTOSTR "string"
 #define CONVTODOUBLE "double"
@@ -68,27 +69,20 @@ int sci_csvTextScan(char *fname, unsigned long fname_len)
         iRange = csv_getArgumentAsMatrixofIntFromDouble(pvApiCtx, 5, fname, &m5, &n5, &iErr);
         if (iErr)
         {
+            freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
             return 0;
         }
 
         if ((m5 * n5 != SIZE_RANGE_SUPPORTED) )
         {
-            if (iRange)
-            {
-                FREE(iRange);
-                iRange = NULL;
-            }
+            freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
             Scierror(999, _("%s: Wrong size for input argument #%d: Four entries expected.\n"), fname, 5);
             return 0;
         }
 
         if ((m5 != 1) && (n5 != 1))
         {
-            if (iRange)
-            {
-                FREE(iRange);
-                iRange = NULL;
-            }
+            freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
             Scierror(999, _("%s: Wrong size for input argument #%d: A column or row vector expected.\n"), fname, 5);
             return 0;
         }
@@ -99,11 +93,7 @@ int sci_csvTextScan(char *fname, unsigned long fname_len)
         }
         else
         {
-            if (iRange)
-            {
-                FREE(iRange);
-                iRange = NULL;
-            }
+            freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
             Scierror(999, _("%s: Wrong value for input argument #%d: Inconsistent range.\n"), fname, 5);
             return 0;
         }
@@ -114,27 +104,13 @@ int sci_csvTextScan(char *fname, unsigned long fname_len)
         conversion = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 4, fname, getCsvDefaultConversion(), &iErr);
         if (iErr)
         {
-            if (iRange)
-            {
-                FREE(iRange);
-                iRange = NULL;
-            }
+            freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
             return 0;
         }
 
         if (!((strcmp(conversion, CONVTOSTR) == 0) || (strcmp(conversion, CONVTODOUBLE) == 0)))
         {
-            if (iRange)
-            {
-                FREE(iRange);
-                iRange = NULL;
-            }
-            if (conversion)
-            {
-                FREE(conversion);
-                conversion = NULL;
-            }
-
+            freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
             Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' string expected.\n"), fname, 4, "double", "string");
             return 0;
         }
@@ -149,32 +125,13 @@ int sci_csvTextScan(char *fname, unsigned long fname_len)
         decimal = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 3, fname, getCsvDefaultDecimal(), &iErr);
         if (iErr)
         {
-            if (iRange)
-            {
-                FREE(iRange);
-                iRange = NULL;
-            }
-            if (conversion)
-            {
-                FREE(conversion);
-                conversion = NULL;
-            }
+            freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
             return 0;
         }
 
         if (decimal[0] != '.' && decimal[0] != ',')
         {
-            if (iRange)
-            {
-                FREE(iRange);
-                iRange = NULL;
-            }
-            if (conversion)
-            {
-                FREE(conversion);
-                conversion = NULL;
-            }
-
+            freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
             Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' string expected.\n"), fname, 3, ",", ".");
             return 0;
         }
@@ -189,21 +146,7 @@ int sci_csvTextScan(char *fname, unsigned long fname_len)
         separator = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 2, fname, getCsvDefaultSeparator(), &iErr);
         if (iErr)
         {
-            if (iRange)
-            {
-                FREE(iRange);
-                iRange = NULL;
-            }
-            if (decimal)
-            {
-                FREE(decimal);
-                decimal = NULL;
-            }
-            if (conversion)
-            {
-                FREE(conversion);
-                conversion = NULL;
-            }
+            freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
             return 0;
         }
     }
@@ -216,26 +159,7 @@ int sci_csvTextScan(char *fname, unsigned long fname_len)
             !csv_isColumnVector(pvApiCtx, 1) &&
             !csv_isScalar(pvApiCtx, 1))
     {
-        if (iRange)
-        {
-            FREE(iRange);
-            iRange = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        if (decimal)
-        {
-            FREE(decimal);
-            decimal = NULL;
-        }
-        if (conversion)
-        {
-            FREE(conversion);
-            conversion = NULL;
-        }
+        freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
         Scierror(999, _("%s: Wrong size for input argument #%d: String vector expected.\n"), fname, 1);
         return 0;
     }
@@ -243,49 +167,13 @@ int sci_csvTextScan(char *fname, unsigned long fname_len)
     text = csv_getArgumentAsMatrixOfString(pvApiCtx, 1, fname, &m1, &n1, &iErr);
     if (iErr)
     {
-        if (iRange)
-        {
-            FREE(iRange);
-            iRange = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        if (decimal)
-        {
-            FREE(decimal);
-            decimal = NULL;
-        }
-        if (conversion)
-        {
-            FREE(conversion);
-            conversion = NULL;
-        }
+        freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
         return 0;
     }
 
     nbLines = m1 * n1;
     result = csvTextScan((const char**)text, nbLines, separator, decimal);
 
-    if (text)
-    {
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        freeArrayOfString(text, nbLines);
-        text = NULL;
-    }
-
-    if (separator)
-    {
-        FREE(separator);
-        separator = NULL;
-    }
-
     if (result)
     {
         switch (result->err)
@@ -315,7 +203,6 @@ int sci_csvTextScan(char *fname, unsigned long fname_len)
                         {
                             Scierror(999, _("%s: Memory allocation error.\n"), fname);
                         }
-
                     }
                     else
                     {
@@ -329,21 +216,8 @@ int sci_csvTextScan(char *fname, unsigned long fname_len)
                     if (ptrComplexArray == NULL)
                     {
                         freeCsvResult(result);
-                        if (decimal)
-                        {
-                            FREE(decimal);
-                            decimal = NULL;
-                        }
-                        if (conversion)
-                        {
-                            FREE(conversion);
-                            conversion = NULL;
-                        }
-                        if (iRange)
-                        {
-                            FREE(iRange);
-                            iRange = NULL;
-                        }
+                        freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
+
                         if (ierr == STRINGTOCOMPLEX_ERROR)
                         {
                             Scierror(999, _("%s: can not convert data.\n"), fname);
@@ -414,21 +288,7 @@ int sci_csvTextScan(char *fname, unsigned long fname_len)
                 if (sciErr.iErr)
                 {
                     freeCsvResult(result);
-                    if (decimal)
-                    {
-                        FREE(decimal);
-                        decimal = NULL;
-                    }
-                    if (conversion)
-                    {
-                        FREE(conversion);
-                        conversion = NULL;
-                    }
-                    if (iRange)
-                    {
-                        FREE(iRange);
-                        iRange = NULL;
-                    }
+                    freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
                     printError(&sciErr, 0);
                     Scierror(17, _("%s: Memory allocation error.\n"), fname);
                     return 0;
@@ -466,22 +326,46 @@ int sci_csvTextScan(char *fname, unsigned long fname_len)
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
     }
     freeCsvResult(result);
-    if (decimal)
+    freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
+
+    return 0;
+}
+// =============================================================================
+static void freeVar(char*** text, int sizeText, int** lengthText, char** separator, char** decimal, char** conversion, int** iRange)
+{
+    if (text && *text)
+    {
+        freeArrayOfString(*text, sizeText);
+        *text = NULL;
+    }
+
+    if (lengthText && *lengthText)
     {
-        FREE(decimal);
-        decimal = NULL;
+        FREE(*lengthText);
+        *lengthText = NULL;
     }
-    if (conversion)
+
+    if (separator && *separator)
     {
-        FREE(conversion);
-        conversion = NULL;
+        FREE(*separator);
+        *separator = NULL;
     }
-    if (iRange)
+
+    if (decimal && *decimal)
     {
-        FREE(iRange);
-        iRange = NULL;
+        FREE(*decimal);
+        *decimal = NULL;
     }
 
-    return 0;
+    if (conversion && *conversion)
+    {
+        FREE(*conversion);
+        *conversion = NULL;
+    }
+
+    if (iRange && *iRange)
+    {
+        FREE(*iRange);
+        *iRange = NULL;
+    }
 }
-// =============================================================================
index c70fb5f..fa50c15 100644 (file)
@@ -27,6 +27,8 @@
 #include "checkCsvWriteFormat.h"
 #include "gw_csv_helpers.h"
 
+
+static void freeVar(char** separator, char** decimal, char** filename, char** precisionFormat, char*** pHeadersLines, int sizeHeader);
 // =============================================================================
 // csvWrite(M, filename[, separator, decimal, precision]) */
 // with M string or double (not complex)
@@ -70,16 +72,13 @@ int sci_csvWrite(char *fname, unsigned long fname_len)
         pHeadersLines = csv_getArgumentAsMatrixOfString(pvApiCtx, 6, fname, &m6, &n6, &iErr);
         if (iErr)
         {
+            freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
             return 0;
         }
         isOnlyRowOrCol = ((m6 > 1) && (n6 == 1)) || ((m6 == 1) && (n6 > 1)) || ((m6 == 1) && (n6 == 1));
         if (!isOnlyRowOrCol)
         {
-            if (pHeadersLines)
-            {
-                freeArrayOfString(pHeadersLines, nbHeadersLines);
-                pHeadersLines = NULL;
-            }
+            freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
             Scierror(999, _("%s: Wrong size for input argument #%d: A 1-by-n or m-by-1 array of strings expected.\n"), fname, 6);
             return 0;
         }
@@ -94,22 +93,14 @@ int sci_csvWrite(char *fname, unsigned long fname_len)
             int iFormatValue = (int) csv_getArgumentAsScalarDouble(pvApiCtx, 5, fname, &iErr);
             if (iErr)
             {
-                if (pHeadersLines)
-                {
-                    freeArrayOfString(pHeadersLines, nbHeadersLines);
-                    pHeadersLines = NULL;
-                }
+                freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
                 return 0;
             }
 
             if ((iFormatValue < 1) || (iFormatValue > 17))
             {
                 Scierror(999, _("%s: Wrong value for input argument #%d: A double (value 1 to 17) expected.\n"), fname, 5);
-                if (pHeadersLines)
-                {
-                    freeArrayOfString(pHeadersLines, nbHeadersLines);
-                    pHeadersLines = NULL;
-                }
+                freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
                 return 0;
             }
 
@@ -117,11 +108,7 @@ int sci_csvWrite(char *fname, unsigned long fname_len)
             if (precisionFormat == NULL)
             {
                 Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                if (pHeadersLines)
-                {
-                    freeArrayOfString(pHeadersLines, nbHeadersLines);
-                    pHeadersLines = NULL;
-                }
+                freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
                 return 0;
             }
             sprintf(precisionFormat, FORMAT_FIELDVALUESTR, iFormatValue);
@@ -131,21 +118,14 @@ int sci_csvWrite(char *fname, unsigned long fname_len)
             precisionFormat = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 5, fname, getCsvDefaultPrecision(), &iErr);
             if (iErr)
             {
+                freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
                 return 0;
             }
+
             if (checkCsvWriteFormat(precisionFormat))
             {
                 Scierror(999, _("%s: Not supported format %s.\n"), fname, precisionFormat);
-                if (precisionFormat)
-                {
-                    FREE(precisionFormat);
-                    precisionFormat = NULL;
-                }
-                if (pHeadersLines)
-                {
-                    freeArrayOfString(pHeadersLines, nbHeadersLines);
-                    pHeadersLines = NULL;
-                }
+                freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
                 return 0;
             }
         }
@@ -160,16 +140,7 @@ int sci_csvWrite(char *fname, unsigned long fname_len)
         decimal = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 4, fname, getCsvDefaultDecimal(), &iErr);
         if (iErr)
         {
-            if (precisionFormat)
-            {
-                FREE(precisionFormat);
-                precisionFormat = NULL;
-            }
-            if (pHeadersLines)
-            {
-                freeArrayOfString(pHeadersLines, nbHeadersLines);
-                pHeadersLines = NULL;
-            }
+            freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
             return 0;
         }
 
@@ -177,7 +148,7 @@ int sci_csvWrite(char *fname, unsigned long fname_len)
         {
             //invalid value
             Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), "write_csv", 4, ".", ",");
-            FREE(precisionFormat);
+            freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
             return 1;
         }
     }
@@ -191,21 +162,7 @@ int sci_csvWrite(char *fname, unsigned long fname_len)
         separator = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 3, fname, getCsvDefaultSeparator(), &iErr);
         if (iErr)
         {
-            if (pHeadersLines)
-            {
-                freeArrayOfString(pHeadersLines, nbHeadersLines);
-                pHeadersLines = NULL;
-            }
-            if (precisionFormat)
-            {
-                FREE(precisionFormat);
-                precisionFormat = NULL;
-            }
-            if (decimal)
-            {
-                FREE(decimal);
-                decimal = NULL;
-            }
+            freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
             return 0;
         }
     }
@@ -217,57 +174,14 @@ int sci_csvWrite(char *fname, unsigned long fname_len)
     filename = csv_getArgumentAsString(pvApiCtx, 2, fname, &iErr);
     if (iErr)
     {
-        if (pHeadersLines)
-        {
-            freeArrayOfString(pHeadersLines, nbHeadersLines);
-            pHeadersLines = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        if (precisionFormat)
-        {
-            FREE(precisionFormat);
-            precisionFormat = NULL;
-        }
-        if (decimal)
-        {
-            FREE(decimal);
-            decimal = NULL;
-        }
+        freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
         return 0;
     }
 
     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
     if (sciErr.iErr)
     {
-        if (pHeadersLines)
-        {
-            freeArrayOfString(pHeadersLines, nbHeadersLines);
-            pHeadersLines = NULL;
-        }
-        if (filename)
-        {
-            FREE(filename);
-            filename = NULL;
-        }
-        if (precisionFormat)
-        {
-            FREE(precisionFormat);
-            precisionFormat = NULL;
-        }
-        if (decimal)
-        {
-            FREE(decimal);
-            decimal = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
+        freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
         printError(&sciErr, 0);
         return 0;
     }
@@ -275,31 +189,7 @@ int sci_csvWrite(char *fname, unsigned long fname_len)
     sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1);
     if (sciErr.iErr)
     {
-        if (pHeadersLines)
-        {
-            freeArrayOfString(pHeadersLines, nbHeadersLines);
-            pHeadersLines = NULL;
-        }
-        if (filename)
-        {
-            FREE(filename);
-            filename = NULL;
-        }
-        if (precisionFormat)
-        {
-            FREE(precisionFormat);
-            precisionFormat = NULL;
-        }
-        if (decimal)
-        {
-            FREE(decimal);
-            decimal = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
+        freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
         printError(&sciErr, 0);
         return 0;
     }
@@ -309,31 +199,7 @@ int sci_csvWrite(char *fname, unsigned long fname_len)
         pStringValues = csv_getArgumentAsMatrixOfString(pvApiCtx, 1, fname, &m1, &n1, &iErr);
         if (iErr)
         {
-            if (pHeadersLines)
-            {
-                freeArrayOfString(pHeadersLines, nbHeadersLines);
-                pHeadersLines = NULL;
-            }
-            if (filename)
-            {
-                FREE(filename);
-                filename = NULL;
-            }
-            if (precisionFormat)
-            {
-                FREE(precisionFormat);
-                precisionFormat = NULL;
-            }
-            if (decimal)
-            {
-                FREE(decimal);
-                decimal = NULL;
-            }
-            if (separator)
-            {
-                FREE(separator);
-                separator = NULL;
-            }
+            freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
             return 0;
         }
     }
@@ -351,63 +217,14 @@ int sci_csvWrite(char *fname, unsigned long fname_len)
 
         if (sciErr.iErr)
         {
-            if (pHeadersLines)
-            {
-                freeArrayOfString(pHeadersLines, nbHeadersLines);
-                pHeadersLines = NULL;
-            }
-            if (precisionFormat)
-            {
-                FREE(precisionFormat);
-                precisionFormat = NULL;
-            }
-            if (filename)
-            {
-                FREE(filename);
-                filename = NULL;
-            }
-            if (decimal)
-            {
-                FREE(decimal);
-                decimal = NULL;
-            }
-            if (separator)
-            {
-                FREE(separator);
-                separator = NULL;
-            }
+            freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
             printError(&sciErr, 0);
             return 0;
         }
     }
     else
     {
-        if (pHeadersLines)
-        {
-            freeArrayOfString(pHeadersLines, nbHeadersLines);
-            pHeadersLines = NULL;
-        }
-        if (precisionFormat)
-        {
-            FREE(precisionFormat);
-            precisionFormat = NULL;
-        }
-        if (filename)
-        {
-            FREE(filename);
-            filename = NULL;
-        }
-        if (decimal)
-        {
-            FREE(decimal);
-            decimal = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-
+        freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
         Scierror(999, _("%s: Wrong type for input argument #%d: A matrix of string or a matrix of real expected.\n"), fname, 1);
         return 0;
     }
@@ -444,32 +261,6 @@ int sci_csvWrite(char *fname, unsigned long fname_len)
         }
     }
 
-    if (pHeadersLines)
-    {
-        freeArrayOfString(pHeadersLines, nbHeadersLines);
-        pHeadersLines = NULL;
-    }
-    if (pStringValues)
-    {
-        freeArrayOfString(pStringValues, m1 * n1);
-        pStringValues = NULL;
-    }
-    if (decimal)
-    {
-        FREE(decimal);
-        decimal = NULL;
-    }
-    if (separator)
-    {
-        FREE(separator);
-        separator = NULL;
-    }
-    if (precisionFormat)
-    {
-        FREE(precisionFormat);
-        precisionFormat = NULL;
-    }
-
     switch (csvError)
     {
         case CSV_WRITE_SEPARATOR_DECIMAL_EQUAL:
@@ -497,13 +288,41 @@ int sci_csvWrite(char *fname, unsigned long fname_len)
         break;
     }
 
-    if (filename)
+    freeVar(&separator, &decimal, &filename, &precisionFormat, &pHeadersLines, nbHeadersLines);
+    return 0;
+}
+// =============================================================================
+static void freeVar(char** separator, char** decimal, char** filename, char** precisionFormat, char*** pHeadersLines, int sizeHeader)
+{
+    if (separator && *separator)
     {
-        FREE(filename);
-        filename = NULL;
+        FREE(*separator);
+        *separator = NULL;
+    }
+
+    if (decimal && *decimal)
+    {
+        FREE(*decimal);
+        *decimal = NULL;
+    }
+
+    if (filename && *filename)
+    {
+        FREE(*filename);
+        *filename = NULL;
+    }
+
+    if (precisionFormat && *precisionFormat)
+    {
+        FREE(*precisionFormat);
+        *precisionFormat = NULL;
+    }
+
+    if (pHeadersLines && *pHeadersLines)
+    {
+        freeArrayOfString(*pHeadersLines, sizeHeader);
+        *pHeadersLines = NULL;
     }
 
-    return 0;
 }
-// =============================================================================
 
index 39213ab..6b0838b 100644 (file)
@@ -97,6 +97,7 @@ int sci_xls_open(char *fname, unsigned long fname_len)
         if (!FileExist(filename_IN))
         {
             Scierror(999, _("The file %s does not exist.\n"), filename_IN);
+            FREE(filename_IN);
             return 0;
         }
     }
index 1de53be..0059840 100644 (file)
@@ -47,22 +47,18 @@ int checkCsvWriteFormat(const char *format)
 // =============================================================================
 static char *replaceInFormat(const char *format)
 {
-    char *newFormat = NULL;
     if (format)
     {
-        int i = 0;
-        for (i = 0; i < NB_FORMAT_SUPPORTED; i++)
+        char *cleanedFormat = getCleanedFormat(format);
+        if (cleanedFormat)
         {
-            char *cleanedFormat = getCleanedFormat(format);
-            if (cleanedFormat)
-            {
-                newFormat = strdup("%s");
-                FREE(cleanedFormat);
-                cleanedFormat = NULL;
-            }
+            FREE(cleanedFormat);
+            cleanedFormat = NULL;
+            return strdup("%s");
         }
     }
-    return newFormat;
+
+    return NULL;
 }
 // =============================================================================
 static char *getCleanedFormat(const char *format)
index 0efeb81..8e9a203 100644 (file)
@@ -202,6 +202,11 @@ csvResult* csvRead(const char *filename, const char *separator, const char *deci
         result->pstrComments = pComments;
         result->nbComments = nbComments;
     }
+    else
+    {
+        freeArrayOfString(pComments, nbComments);
+    }
+
 
     return result;
 }
@@ -643,10 +648,17 @@ static char **extractComments(const char **lines, int nbLines,
 
         if ( (answer == CAN_NOT_COMPILE_PATTERN) || (answer == DELIMITER_NOT_ALPHANUMERIC))
         {
+            if (pComments)
+            {
+                freeArrayOfString(pComments, *nbcomments);
+            }
+
             *nbcomments = 0;
+
             *iErr = answer;
             return NULL;
         }
+
         if ( answer == PCRE_FINISHED_OK )
         {
             (*nbcomments)++;
index 660fb70..b8c2ac3 100644 (file)
@@ -226,8 +226,8 @@ char **splitLineCSV(const char *str, const char *sep, int *toks)
         if (!addToken(retstr, &curr_str, (char*)(idx - len), len))
         {
             *toks = 0;
-            FREE(substitutedstring);
             freeArrayOfString(retstr, strlen(substitutedstring));
+            FREE(substitutedstring);
             return NULL;
         }
     }