spreadsheet #2: fix new or hidden memory leaks and remove useless tests 04/12804/2
Antoine ELIAS [Thu, 3 Oct 2013 10:55:35 +0000 (12:55 +0200)]
Coverity ID: 1097918, 1098245, 1098314, 1098316, 1098334, 1098926, 1098927, 1099115, 1099116
             1099159, 1099176, 1099237, 1099245, 1099389, 1099551, 1099552, 1100835, 1100836

Change-Id: Icb2c565de8b58a00a07173b5852c5fb90a69be7a

scilab/modules/spreadsheet/sci_gateway/c/sci_csvDefault.c
scilab/modules/spreadsheet/sci_gateway/c/sci_csvStringToDouble.c
scilab/modules/spreadsheet/sci_gateway/c/sci_csvTextScan.c
scilab/modules/spreadsheet/sci_gateway/c/sci_xls_open.c
scilab/modules/spreadsheet/src/c/csvRead.c
scilab/modules/spreadsheet/src/c/csvWrite.c
scilab/modules/spreadsheet/src/c/splitLine.c

index 3802df2..a03383f 100644 (file)
@@ -49,6 +49,7 @@
 // =============================================================================
 #define NUMBER_FIELD 8
 // =============================================================================
+static void freeVar(char** fieldname, char** fieldvalue);
 static int sci_csvDefault_no_rhs(char *fname);
 static int sci_csvDefault_one_rhs(char *fname);
 static int sci_csvDefault_two_rhs(char *fname);
@@ -155,10 +156,7 @@ static int sci_csvDefault_one_rhs(char *fname)
     fieldname = csv_getArgumentAsString(pvApiCtx, 1, fname, &iErr);
     if (iErr)
     {
-        if (fieldname)
-        {
-            FREE(fieldname);
-        }
+        freeVar(&fieldname, &fieldvalue);
         return 0;
     }
 
@@ -219,16 +217,7 @@ static int sci_csvDefault_one_rhs(char *fname)
     }
     else if (strcmp(fieldname, RESET_PARAMATERS) == 0)
     {
-        if (fieldname)
-        {
-            FREE(fieldname);
-            fieldname = NULL;
-        }
-        if (fieldvalue)
-        {
-            FREE(fieldvalue);
-            fieldvalue = NULL;
-        }
+        freeVar(&fieldname, &fieldvalue);
 
         setCsvDefaultReset();
 
@@ -241,27 +230,13 @@ static int sci_csvDefault_one_rhs(char *fname)
     else
     {
         Scierror(999, _("%s: Wrong value for input argument #%d: '%s', '%s', '%s', '%s', '%s' or '%s' expected.\n"), fname, 1, SEPARATOR_FIELDNAME, DECIMAL_FIELDNAME, CONVERSION_FIELDNAME, COMMENTSREGEXP_FIELDNAME, EOL_FIELDNAME, BLANK_FIELDNAME);
-        if (fieldname)
-        {
-            FREE(fieldname);
-            fieldname = NULL;
-        }
+        freeVar(&fieldname, &fieldvalue);
         return 0;
     }
 
-    if (fieldname)
-    {
-        FREE(fieldname);
-        fieldname = NULL;
-    }
-
     createSingleString(pvApiCtx, Rhs + 1, fieldvalue);
 
-    if (fieldvalue)
-    {
-        FREE(fieldvalue);
-        fieldvalue = NULL;
-    }
+    freeVar(&fieldname, &fieldvalue);
 
     LhsVar(1) = Rhs + 1;
     PutLhsVar();
@@ -281,10 +256,7 @@ static int sci_csvDefault_two_rhs(char *fname)
     fieldname = csv_getArgumentAsString(pvApiCtx, 1, fname, &iErr);
     if (iErr)
     {
-        if (fieldvalue)
-        {
-            FREE(fieldvalue);
-        }
+        freeVar(&fieldname, &fieldvalue);
         return 0;
     }
 
@@ -292,11 +264,7 @@ static int sci_csvDefault_two_rhs(char *fname)
     {
         if (csv_isEmpty(pvApiCtx, 2))
         {
-            if (fieldname)
-            {
-                FREE(fieldname);
-                fieldname = NULL;
-            }
+            freeVar(&fieldname, &fieldvalue);
             Scierror(999, _("%s: Wrong type for input argument #%d: A double expected.\n"), fname, 2);
             return 0;
         }
@@ -307,21 +275,13 @@ static int sci_csvDefault_two_rhs(char *fname)
             ifieldvalue = (int) csv_getArgumentAsScalarDouble(pvApiCtx, 2, fname, &iErr);
             if (iErr)
             {
-                if (fieldname)
-                {
-                    FREE(fieldname);
-                    fieldname = NULL;
-                }
+                freeVar(&fieldname, &fieldvalue);
                 return 0;
             }
 
             if ((ifieldvalue < 1) || (ifieldvalue > 17))
             {
-                if (fieldname)
-                {
-                    FREE(fieldname);
-                    fieldname = NULL;
-                }
+                freeVar(&fieldname, &fieldvalue);
                 Scierror(999, _("%s: Wrong value for input argument #%d: A double (value %d to %d) expected.\n"), fname, 2, 1, 17);
                 return 0;
             }
@@ -329,11 +289,7 @@ static int sci_csvDefault_two_rhs(char *fname)
             fieldvalue = (char*)MALLOC(sizeof(char) * ((int)strlen(FORMAT_FIELDVALUESTR) + 1));
             if (fieldvalue == NULL)
             {
-                if (fieldname)
-                {
-                    FREE(fieldname);
-                    fieldname = NULL;
-                }
+                freeVar(&fieldname, &fieldvalue);
                 Scierror(999, _("%s: Memory allocation error.\n"), fname);
                 return 0;
             }
@@ -344,11 +300,7 @@ static int sci_csvDefault_two_rhs(char *fname)
             fieldvalue = csv_getArgumentAsString(pvApiCtx, 2, fname, &iErr);
             if (iErr)
             {
-                if (fieldname)
-                {
-                    FREE(fieldname);
-                    fieldname = NULL;
-                }
+                freeVar(&fieldname, &fieldvalue);
                 return 0;
             }
         }
@@ -358,17 +310,7 @@ static int sci_csvDefault_two_rhs(char *fname)
         fieldvalue = csv_getArgumentAsString(pvApiCtx, 2, fname, &iErr);
         if (iErr)
         {
-            if (fieldname)
-            {
-                FREE(fieldname);
-                fieldname = NULL;
-            }
-
-            if (fieldvalue)
-            {
-                FREE(fieldvalue);
-                fieldvalue = NULL;
-            }
+            freeVar(&fieldname, &fieldvalue);
             return 0;
         }
     }
@@ -423,31 +365,13 @@ static int sci_csvDefault_two_rhs(char *fname)
     else
     {
         Scierror(999, _("%s: Wrong value for input argument #%d: '%s', '%s', '%s' , '%s', '%s', '%s', '%s' or '%s' expected.\n"), fname, 1, SEPARATOR_FIELDNAME, DECIMAL_FIELDNAME, CONVERSION_FIELDNAME, PRECISION_FIELDNAME, COMMENTSREGEXP_FIELDNAME, EOL_FIELDNAME, ENCODING_FIELDNAME, BLANK_FIELDNAME);
-        if (fieldname)
-        {
-            FREE(fieldname);
-            fieldname = NULL;
-        }
-        if (fieldvalue)
-        {
-            FREE(fieldvalue);
-            fieldvalue = NULL;
-        }
+        freeVar(&fieldname, &fieldvalue);
         return 0;
     }
 
     createScalarBoolean(pvApiCtx, Rhs + 1, (resultSet == 0));
 
-    if (fieldname)
-    {
-        FREE(fieldname);
-        fieldname = NULL;
-    }
-    if (fieldvalue)
-    {
-        FREE(fieldvalue);
-        fieldvalue = NULL;
-    }
+    freeVar(&fieldname, &fieldvalue);
 
     LhsVar(1) = Rhs + 1;
     PutLhsVar();
@@ -455,3 +379,17 @@ static int sci_csvDefault_two_rhs(char *fname)
     return 0;
 }
 // =============================================================================
+static void freeVar(char** fieldname, char** fieldvalue)
+{
+    if (fieldname && *fieldname)
+    {
+        FREE(*fieldname);
+        *fieldname = NULL;
+    }
+
+    if (fieldvalue && *fieldvalue)
+    {
+        FREE(*fieldvalue);
+        *fieldvalue = NULL;
+    }
+}
\ No newline at end of file
index 344ec05..953013e 100644 (file)
@@ -104,6 +104,7 @@ int sci_csvStringToDouble(char *fname, unsigned long fname_len)
         {
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
         }
+        break;
 
         default:
         case STRINGTOCOMPLEX_ERROR:
index 4a9f836..1c313b0 100644 (file)
@@ -277,18 +277,22 @@ int sci_csvTextScan(char *fname, unsigned long fname_len)
                         {
                             Scierror(999, _("%s: Memory allocation error.\n"), fname);
                         }
+                        break;
+
                         default:
                         case STRINGTOCOMPLEX_ERROR:
                         {
                             Scierror(999, _("%s: can not convert data.\n"), fname);
                         }
+                        break;
                     }
                 }
 
+                freeCsvResult(result);
+                freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
+
                 if (sciErr.iErr)
                 {
-                    freeCsvResult(result);
-                    freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
                     printError(&sciErr, 0);
                     Scierror(17, _("%s: Memory allocation error.\n"), fname);
                     return 0;
@@ -325,8 +329,6 @@ int sci_csvTextScan(char *fname, unsigned long fname_len)
     {
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
     }
-    freeCsvResult(result);
-    freeVar(&text, nbLines, &lengthText, &separator, &decimal, &conversion, &iRange);
 
     return 0;
 }
index 6b0838b..8a9fe89 100644 (file)
@@ -48,6 +48,7 @@ int sci_xls_open(char *fname, unsigned long fname_len)
     int nsheets = 0;
     char *filename_IN = NULL;
     char TMP[max_char_xls_open];
+    char* base_name = NULL;
 
     char sep[2];
     char *TMPDIR = NULL;
@@ -72,7 +73,7 @@ int sci_xls_open(char *fname, unsigned long fname_len)
     GetRhsVar(1, STRING_DATATYPE, &m1, &n1, &l1);
 
     filename_IN = expandPathVariable(cstk(l1));
-    if (filename_IN)
+
     {
         /* bug 5615 */
         /* remove blank characters @ the end */
@@ -103,15 +104,16 @@ int sci_xls_open(char *fname, unsigned long fname_len)
     }
 
     TMPDIR = getTMPDIR();
-    strcpy(TMP, TMPDIR);
-    if (TMPDIR)
-    {
-        FREE(TMPDIR);
-        TMPDIR = NULL;
-    }
+    strncpy(TMP, TMPDIR, sizeof(TMP));
+    FREE(TMPDIR);
+
+    strncat(TMP, sep, sizeof(TMP) - strlen(TMP) - 1);
+    TMP[sizeof(TMP) - 1] = '\0';
+
+    base_name = xls_basename(filename_IN);
+    strncat(TMP, base_name, sizeof(TMP) - strlen(TMP) - 1);
+    TMP[sizeof(TMP) - 1] = '\0';
 
-    strcat(TMP, sep);
-    strcat(TMP, xls_basename(filename_IN));
     result = ripole(filename_IN, TMP, 0, 0);
     if (result != OLE_OK)
     {
@@ -124,20 +126,12 @@ int sci_xls_open(char *fname, unsigned long fname_len)
                  result == OLEER_LOADFAT_BAD_BOUNDARY || result == OLEER_MINIFAT_READ_FAIL || result == OLEER_PROPERTIES_READ_FAIL)
         {
             Scierror(999, _("%s: File %s is not an ole2 file.\n"), fname, filename_IN);
-            if (filename_IN)
-            {
-                FREE(filename_IN);
-                filename_IN = NULL;
-            }
+            FREE(filename_IN);
         }
         else if (result == -1)
         {
             Scierror(999, _("%s: Cannot open file %s.\n"), fname, filename_IN);
-            if (filename_IN)
-            {
-                FREE(filename_IN);
-                filename_IN = NULL;
-            }
+            FREE(filename_IN);
         }
         return 0;
     }
@@ -147,19 +141,11 @@ int sci_xls_open(char *fname, unsigned long fname_len)
     if (ierr != 0)
     {
         Scierror(999, _("%s: There is no xls stream in the ole2 file %s.\n"), fname, filename_IN);
-        if (filename_IN)
-        {
-            FREE(filename_IN);
-            filename_IN = NULL;
-        }
+        FREE(filename_IN);
         return 0;
     }
 
-    if (filename_IN)
-    {
-        FREE(filename_IN);
-        filename_IN = NULL;
-    }
+    FREE(filename_IN);
 
     CreateVar(Rhs + 1, MATRIX_OF_INTEGER_DATATYPE, &one, &one, &l2);
     *istk(l2) = fd;             /* logical unit */
index 8e9a203..6456d5c 100644 (file)
@@ -83,20 +83,14 @@ csvResult* csvRead(const char *filename, const char *separator, const char *deci
             result->pstrComments = NULL;
             result->nbComments = 0;
         }
-        if (expandedFilename)
-        {
-            FREE(expandedFilename);
-            expandedFilename = NULL;
-        }
+
+        FREE(expandedFilename);
         return result;
     }
 
     C2F(mopen)(&fd, expandedFilename, (char*)READ_ONLY_TEXT_MODE, &f_swap, &res, &errMOPEN);
-    if (expandedFilename)
-    {
-        FREE(expandedFilename);
-        expandedFilename = NULL;
-    }
+    FREE(expandedFilename);
+
     if (errMOPEN != MOPEN_NO_ERROR)
     {
         result = (csvResult*)(MALLOC(sizeof(csvResult)));
@@ -191,11 +185,7 @@ csvResult* csvRead(const char *filename, const char *separator, const char *deci
     }
 
     result = csvTextScan((const char**)lines, nblines, (const char*)separator, (const char*)decimal);
-    if (lines)
-    {
-        freeArrayOfString(lines, nblines);
-        lines = NULL;
-    }
+    freeArrayOfString(lines, nblines);
 
     if (result)
     {
@@ -622,7 +612,7 @@ static char **replaceStrings(const char **lines, int nbLines, const char **torep
                 replacedStrings[j] = strdup(lines[j]);
             }
             // Make replacements within the target replacedStrings.
-            for (i = 0; i < nr; i = i++)
+            for (i = 0; i < nr; i++)
             {
                 for (j = 0; j < nbLines; j++)
                 {
index d754b3b..9a48f80 100644 (file)
@@ -320,7 +320,7 @@ csvWriteError csvWrite_complex(const char *filename,
                     result = strsub(buffer, getCsvDefaultDecimal(), decimal);
                     if (result)
                     {
-                        strcpy(StringValue, result);
+                        strncpy(StringValue, result, sizeof(StringValue) - 1);
                         FREE(result);
                     }
                     else
@@ -385,11 +385,12 @@ csvWriteError csvWrite_complex(const char *filename,
                     {
                         if ((hasReal) || (imagValue < 0))
                         {
-                            strcat(StringValue, result);
+                            strncat(StringValue, result, sizeof(StringValue) - strlen(StringValue) - 1);
+                            StringValue[sizeof(StringValue) - 1] = '\0';
                         }
                         else
                         {
-                            strcpy(StringValue, result);
+                            strncpy(StringValue, result, sizeof(StringValue) - 1);
                         }
                         FREE(result);
                     }
@@ -511,59 +512,36 @@ csvWriteError csvWrite_string(const char *filename,
     {
         for ( j = 0 ; j < n ; j++)
         {
-            if (decimal == NULL)
+            char *result = NULL;
+            result = strsub((char*)(pStrValues[i + m * j]), getCsvDefaultDecimal(), decimal);
+            if (result)
             {
                 if (isIsoLatin)
                 {
-                    const char *converted = utftolatin((char*)pStrValues[i + m * j]);
+                    char *converted = utftolatin(result);
                     if (converted)
                     {
                         fprintf(fd, DEFAULT_CSV_WRITE_STRING_FORMAT, converted);
-                        FREE((char*)converted);
+                        FREE(converted);
                         converted = NULL;
                     }
                     else
                     {
-                        fprintf(fd, DEFAULT_CSV_WRITE_STRING_FORMAT, pStrValues[i + m * j]);
+                        fprintf(fd, DEFAULT_CSV_WRITE_STRING_FORMAT, result);
                     }
                 }
                 else
                 {
-                    fprintf(fd, DEFAULT_CSV_WRITE_STRING_FORMAT, pStrValues[i + m * j]);
+                    fprintf(fd, DEFAULT_CSV_WRITE_STRING_FORMAT, result);
                 }
+                FREE(result);
+                result = NULL;
             }
             else
             {
-                char *result = NULL;
-                result = strsub((char*)(pStrValues[i + m * j]), getCsvDefaultDecimal(), decimal);
-                if (result)
-                {
-                    if (isIsoLatin)
-                    {
-                        char *converted = utftolatin(result);
-                        if (converted)
-                        {
-                            fprintf(fd, DEFAULT_CSV_WRITE_STRING_FORMAT, converted);
-                            FREE(converted);
-                            converted = NULL;
-                        }
-                        else
-                        {
-                            fprintf(fd, DEFAULT_CSV_WRITE_STRING_FORMAT, result);
-                        }
-                    }
-                    else
-                    {
-                        fprintf(fd, DEFAULT_CSV_WRITE_STRING_FORMAT, result);
-                    }
-                    FREE(result);
-                    result = NULL;
-                }
-                else
-                {
-                    fprintf(fd, DEFAULT_CSV_WRITE_STRING_FORMAT, pStrValues[i + m * j]);
-                }
+                fprintf(fd, DEFAULT_CSV_WRITE_STRING_FORMAT, pStrValues[i + m * j]);
             }
+
             if (j + 1 < n)
             {
                 fprintf(fd, "%s", separator);
index b8c2ac3..dc7313e 100644 (file)
@@ -167,8 +167,8 @@ char **splitLineCSV(const char *str, const char *sep, int *toks)
                             else
                             {
                                 *toks = 0;
-                                FREE(substitutedstring);
                                 freeArrayOfString(retstr, strlen(substitutedstring));
+                                FREE(substitutedstring);
                                 return NULL;
                             }
                         }
@@ -201,7 +201,7 @@ char **splitLineCSV(const char *str, const char *sep, int *toks)
 
             while (*idxTmp == '"')
             {
-                *idxTmp++;
+                idxTmp++;
             }
             nbDoubleQuotes = idxTmp - idx;