hdf5 #2: fix memory leaks 62/12862/2
Antoine ELIAS [Wed, 9 Oct 2013 10:17:20 +0000 (12:17 +0200)]
Coverity ID: 1098267, 1098268, 1098916, 1099033, 1102829, 1102830, 1102837, 1102838

Change-Id: Ibdab4aed4dacc021742415f4dd061cb5e77f1ef9

scilab/modules/hdf5/sci_gateway/cpp/sci_export_to_hdf5.cpp
scilab/modules/hdf5/sci_gateway/cpp/sci_import_from_hdf5.cpp
scilab/modules/hdf5/sci_gateway/cpp/sci_listvar_in_hdf5.cpp
scilab/modules/hdf5/src/cpp/import_from_hdf5_v1.cpp

index 3fe54eb..1e863bf 100644 (file)
@@ -873,6 +873,7 @@ static bool isVarExist(int _iFile, char* _pstVarName)
         {
             if (strcmp(pstVarNameList[i], _pstVarName) == 0)
             {
+                freeArrayOfString(pstVarNameList, iNbItem);
                 return true;
             }
         }
index d392b2a..fa83fa6 100644 (file)
@@ -138,11 +138,13 @@ int sci_import_from_hdf5(char *fname, unsigned long fname_len)
 
             if (import_variable(iFile, pstVarName) == false)
             {
+                FREE(pstVarName);
                 bImport = false;
                 break;
             }
 
             FREE(pstVarName);
+            pstVarName = NULL;
         }
     }
     else
@@ -342,39 +344,44 @@ static bool import_double(int _iDatasetId, int _iItemPos, int *_piAddress, char
         return false;
     }
 
-    if (iDims != 0)
+    if (iDims)
     {
+        if (iDims > 2)
+        {
+            //hypermatrix
+            FREE(piDims);
+            return false;
+        }
+
         piDims = (int*)MALLOC(sizeof(int) * iDims);
         iSize = getDatasetInfo(_iDatasetId, &iComplex, &iDims, piDims);
 
-        if (iDims == 2 && piDims[0] * piDims[1] != 0)
+        if (iSize > 0)
         {
+            pdblReal = (double *)MALLOC(iSize * sizeof(double));
+
             if (iComplex)
             {
-                pdblReal = (double *)MALLOC(iSize * sizeof(double));
                 pdblImg = (double *)MALLOC(iSize * sizeof(double));
                 iRet = readDoubleComplexMatrix(_iDatasetId, pdblReal, pdblImg);
             }
             else
             {
-                pdblReal = (double *)MALLOC(iSize * sizeof(double));
                 iRet = readDoubleMatrix(_iDatasetId, pdblReal);
             }
 
-            if (iRet)
+            //to be sure ti have 2 dims
+            if (iDims == 1)
             {
                 FREE(piDims);
-                return false;
+                piDims = (int*)MALLOC(sizeof(int) * 2);
+                piDims[0] = 1;
+                piDims[1] = iSize;
             }
         }
-        else if (iDims > 2)
-        {
-            //hypermatrix
-            FREE(piDims);
-            return false;
-        }
     }
-    else
+
+    if (iDims == 0 || iSize == 0) //empty matrix
     {
         /*bug 7224 : to close dataset */
         iRet = readEmptyMatrix(_iDatasetId);
@@ -666,6 +673,7 @@ static bool import_integer(int _iDatasetId, int _iItemPos, int *_piAddress, char
                 sciErr = createMatrixOfInteger64InNamedList(_pstVarname, _piAddress, _iItemPos, piDims[0], piDims[1], pllData);
             }
 #else
+            FREE(piDims);
             return false;
 #endif
         }
@@ -692,6 +700,7 @@ static bool import_integer(int _iDatasetId, int _iItemPos, int *_piAddress, char
                 sciErr = createMatrixOfUnsignedInteger64InNamedList(_pstVarname, _piAddress, _iItemPos, piDims[0], piDims[1], pullData);
             }
 #else
+            FREE(piDims);
             return false;
 #endif
         }
index 8cb9bed..dc46998 100644 (file)
@@ -149,7 +149,7 @@ int sci_listvar_in_hdf5(char *fname, unsigned long fname_len)
                 break;
             }
 
-            strncpy(pInfo[i].varName, pstVarNameList[i], sizeof(pInfo[i].varName));
+            strncpy(pInfo[i].varName, pstVarNameList[i], sizeof(pInfo[i].varName) - 1);
             pInfo[i].iSize = 0;
             b = read_data(iDataSetId, 0, NULL, &pInfo[i]) == false;
             if (b)
index 630e388..294377a 100644 (file)
@@ -124,11 +124,13 @@ int sci_import_from_hdf5_v1(char *fname, unsigned long fname_len)
 
             if (import_variable_v1(iFile, pstVarName) == false)
             {
+                FREE(pstVarName);
                 bImport = false;
                 break;
             }
 
             FREE(pstVarName);
+            pstVarName = NULL;
         }
     }
     else