* Bug #12383 fixed - csvRead memory leak 03/10803/3
Simon Marchetto [Wed, 13 Mar 2013 13:08:24 +0000 (14:08 +0100)]
Fix memory leaks in:
- csvRead.c: getStringsFromLines(), stripCharacters()
- stringToComplex.c: stringToComplex()

Change-Id: Ic56fb52e2a800c7101670b6295be31f02ac66fc5

scilab/CHANGES_5.4.X
scilab/modules/spreadsheet/src/c/csvRead.c
scilab/modules/spreadsheet/src/c/stringToComplex.c

index 61c44bd..0c371c5 100644 (file)
@@ -533,6 +533,8 @@ Bug fixes
 
 * Bug #12378 fixed - sdf() crashed Scilab.
 
+* Bug #12383 fixed - csvRead memory leak.
+
 * Bug #12386 fixed - sgrid example improved in the list of graphics.
 
 
index 8efc545..9854290 100644 (file)
@@ -456,6 +456,7 @@ static char **getStringsFromLines(const char **lines, int sizelines,
                     lineStrings[j] = NULL;
                 }
             }
+            FREE(lineStrings);
         }
     }
     return results;
@@ -570,6 +571,7 @@ static char *stripCharacters(const char *line)
                 if (tmpLineWithoutCR)
                 {
                     returnedLine = csv_strsubst(tmpLineWithoutCR, " ", "");
+                    FREE(tmpLineWithoutCR);
                 }
                 else
                 {
index 1864e90..7af1fdd 100644 (file)
@@ -108,54 +108,56 @@ doublecomplex stringToComplex(const char *pSTR, const char *decimal, BOOL bConve
     {
         double real = 0.;
         double imag = 0.;
-        char * pStrWithOutBlanks = csv_strsubst(pSTR, " ", "");
+        char *pStrTemp = csv_strsubst(pSTR, " ", "");
 
-        pStrWithOutBlanks = csv_strsubst(pStrWithOutBlanks, decimal, ".");
-
-        if (pStrWithOutBlanks)
+        if (pStrTemp)
         {
-            int lenStrWithOutBlanks = (int) strlen(pStrWithOutBlanks);
+            char *pStrFormatted = csv_strsubst(pStrTemp, decimal, ".");
+            FREE(pStrTemp);
 
-            /* case .4 replaced by 0.4 */
-            if (pStrWithOutBlanks[0] == '.')
+            if (pStrFormatted)
             {
+                int lenStrFormatted = (int) strlen(pStrFormatted);
+
                 /* case .4 replaced by 0.4 */
-                char *pstStrTemp = (char*)MALLOC(sizeof(char) * (lenStrWithOutBlanks + strlen("0") + 1));
-                strcpy(pstStrTemp, "0");
-                strcat(pstStrTemp, pStrWithOutBlanks);
-                FREE(pStrWithOutBlanks);
-                pStrWithOutBlanks = pstStrTemp;
-            }
+                if (pStrFormatted[0] == '.')
+                {
+                    /* case .4 replaced by 0.4 */
+                    char *pstStrTemp = (char*)MALLOC(sizeof(char) * (lenStrFormatted + strlen("0") + 1));
+                    strcpy(pstStrTemp, "0");
+                    strcat(pstStrTemp, pStrFormatted);
+                    FREE(pStrFormatted);
+                    pStrFormatted = pstStrTemp;
+                }
 
-            if (lenStrWithOutBlanks > 1)
-            {
-                if (((pStrWithOutBlanks[0] == '+') || (pStrWithOutBlanks[0] == '-')) &&
-                        (pStrWithOutBlanks[1] == '.'))
+                if (lenStrFormatted > 1)
                 {
-                    /* case +.4 replaced by +0.4 */
-                    char *pstStrTemp = csv_strsubst(pStrWithOutBlanks, "+.", "+0.");
-                    FREE(pStrWithOutBlanks);
-
-                    /* case -.4 replaced by -0.4 */
-                    pStrWithOutBlanks = csv_strsubst(pstStrTemp, "-.", "-0.");
-                    FREE(pstStrTemp);
-                    pstStrTemp = NULL;
+                    if (((pStrFormatted[0] == '+') || (pStrFormatted[0] == '-')) &&
+                            (pStrFormatted[1] == '.'))
+                    {
+                        /* case +.4 replaced by +0.4 */
+                        char *pstStrTemp = csv_strsubst(pStrFormatted, "+.", "+0.");
+                        FREE(pStrFormatted);
+
+                        /* case -.4 replaced by -0.4 */
+                        pStrFormatted = csv_strsubst(pstStrTemp, "-.", "-0.");
+                        FREE(pstStrTemp);
+                    }
                 }
-            }
 
-            /* Case: "i", "+i", "-i", and with "j"  */
-            if (is_unit_imaginary (pStrWithOutBlanks, &imag))
-            {
-                *ierr = STRINGTOCOMPLEX_NO_ERROR;
-                dComplexValue.r = 0.;
-                dComplexValue.i = imag;
-            }
-            else
-            {
-                *ierr = ParseComplexValue(pStrWithOutBlanks, bConvertByNAN, &real, &imag);
+                /* Case: "i", "+i", "-i", and with "j"  */
+                if (is_unit_imaginary (pStrFormatted, &imag))
+                {
+                    *ierr = STRINGTOCOMPLEX_NO_ERROR;
+                    dComplexValue.r = 0.;
+                    dComplexValue.i = imag;
+                }
+                else
+                {
+                    *ierr = ParseComplexValue(pStrFormatted, bConvertByNAN, &real, &imag);
+                }
+                FREE(pStrFormatted);
             }
-            FREE(pStrWithOutBlanks);
-            pStrWithOutBlanks = NULL;
         }
         dComplexValue.r = real;
         dComplexValue.i = imag;