Coverity: Fileio Module Memory Bugs Fixed 62/18062/8
Aashish Misraa [Fri, 8 Apr 2016 14:09:28 +0000 (19:09 +0530)]
Change-Id: I6678c01a284ad2ca9726efe0e2f2f04d197ad3b9

17 files changed:
scilab/modules/fileio/sci_gateway/c/sci_fscanfMat.c
scilab/modules/fileio/sci_gateway/cpp/sci_findfiles.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_getrelativefilename.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_mfscanf.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_mget.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_mgetl.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_mopen.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_mput.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_mscanf.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_msscanf.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_pathsep.cpp
scilab/modules/fileio/src/c/createtempfilename.c
scilab/modules/fileio/src/c/do_xxscanf.c
scilab/modules/fileio/src/c/fullpath.c
scilab/modules/fileio/src/c/getrelativefilename.c
scilab/modules/fileio/src/cpp/mgetl.cpp
scilab/modules/fileio/src/cpp/scilab_sscanf.cpp

index d476540..9135e34 100644 (file)
@@ -166,6 +166,10 @@ int sci_fscanfMat(char *fname, void* pvApiCtx)
     else
     {
         results = fscanfMat(expandedFilename, Format, separator);
+        if (results && results->err != FSCANFMAT_NO_ERROR)
+        {
+            freeFscanfMatResult(results);
+        }
     }
 
     if (results == NULL)
index 1807abe..d7e3c5e 100644 (file)
@@ -77,6 +77,7 @@ types::Function::ReturnValue sci_findfiles(types::typed_list &in, int _iRetCount
             if (in[1]->isString() == false || in[1]->getAs<types::String>()->getSize() != 1)
             {
                 Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "findfiles", 2);
+                FREE(pwstPath);
                 return types::Function::Error;
             }
 
index cec5db5..3e70d7d 100644 (file)
@@ -81,6 +81,8 @@ types::Function::ReturnValue sci_getrelativefilename(types::typed_list &in, int
         if (wcslen(wcsAbsDir) > PATH_MAX)
         {
             Scierror(999, _("%s: Wrong size for input argument #%d: Must be less than %d characters.\n"), "getrelativefilename", 1, PATH_MAX);
+            FREE(wcsAbsDir);
+            delete pStrOut;
             return types::Function::Error;
         }
 
@@ -88,6 +90,9 @@ types::Function::ReturnValue sci_getrelativefilename(types::typed_list &in, int
         if (wcslen(wcsAbsFile) > PATH_MAX)
         {
             Scierror(999, _("%s: Wrong size for input argument #%d: Must be less than %d characters.\n"), "getrelativefilename", 2, PATH_MAX);
+            FREE(wcsAbsFile);
+            FREE(wcsAbsDir);
+            delete pStrOut;
             return types::Function::Error;
         }
 
index f50ac99..24f3cc6 100644 (file)
@@ -52,7 +52,7 @@ types::Function::ReturnValue sci_mfscanf(types::typed_list &in, int _iRetCount,
     int retval_s    = 0;
     int rowcount    = 0;
     rec_entry buf[MAXSCAN] = {0};
-    entry *data;
+    entry *data = NULL;
     sfdir type[MAXSCAN] = {NONE};
     sfdir type_s[MAXSCAN] = {NONE};
 
index ea50c51..cbb4166 100644 (file)
@@ -42,6 +42,7 @@ types::Function::ReturnValue sci_mget(types::typed_list &in, int _iRetCount, typ
     if (in.size() < 1 || in.size() > 3)
     {
         Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "mget", 1, 3);
+        FREE(pstType);
         return types::Function::Error;
     }
 
@@ -49,6 +50,7 @@ types::Function::ReturnValue sci_mget(types::typed_list &in, int _iRetCount, typ
     if (in[0]->isDouble() == false || in[0]->getAs<types::Double>()->getSize() != 1)
     {
         Scierror(999, _("%s: Wrong type for input argument #%d: A positive integer value expected.\n"), "mget", 1);
+        FREE(pstType);
         return types::Function::Error;
     }
 
@@ -56,6 +58,7 @@ types::Function::ReturnValue sci_mget(types::typed_list &in, int _iRetCount, typ
     if ((pDoubleTest->get(0) != (int)pDoubleTest->get(0)) || (pDoubleTest->get(0) < 0))
     {
         Scierror(999, _("%s: Wrong value for input argument #%d: A positive integer value expected.\n"), "mget", 1);
+        FREE(pstType);
         return types::Function::Error;
     }
 
@@ -67,6 +70,7 @@ types::Function::ReturnValue sci_mget(types::typed_list &in, int _iRetCount, typ
         if (in[1]->isString() == false || in[1]->getAs<types::String>()->getSize() != 1)
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "mget", 2);
+            FREE(pstType);
             return types::Function::Error;
         }
 
@@ -79,6 +83,7 @@ types::Function::ReturnValue sci_mget(types::typed_list &in, int _iRetCount, typ
         if (in[2]->isDouble() == false || in[2]->getAs<types::Double>()->getSize() != 1)
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A integer expected.\n"), "mget", 3);
+            FREE(pstType);
             return types::Function::Error;
         }
 
index 425bc00..46c719d 100644 (file)
@@ -105,6 +105,7 @@ types::Function::ReturnValue sci_mgetl(types::typed_list &in, int _iRetCount, ty
             }
 
             FREE(pst);
+            FREE(expandedFileName);
             return types::Function::Error;
         }
         FREE(expandedFileName);
index 12b22ce..59e7de6 100644 (file)
@@ -71,6 +71,7 @@ types::Function::ReturnValue sci_mopen(types::typed_list &in, int _iRetCount, ty
             if (in[1]->isString() == false)
             {
                 Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "mopen", 2);
+                FREE(pstFilename);
                 return types::Function::Error;
             }
 
@@ -78,6 +79,7 @@ types::Function::ReturnValue sci_mopen(types::typed_list &in, int _iRetCount, ty
             if (pS2->getSize() != 1)
             {
                 Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), "mopen" , 2);
+                FREE(pstFilename);
                 return types::Function::Error;
             }
 
@@ -89,6 +91,7 @@ types::Function::ReturnValue sci_mopen(types::typed_list &in, int _iRetCount, ty
                 if (in[2]->isDouble() == false)
                 {
                     Scierror(999, _("%s: Wrong type for input argument #%d: An integer expected.\n"), "mopen" , 3);
+                    FREE(pstFilename);
                     return types::Function::Error;
                 }
 
@@ -96,6 +99,7 @@ types::Function::ReturnValue sci_mopen(types::typed_list &in, int _iRetCount, ty
                 if (pD3->getSize() != 1 || pD3->isComplex())
                 {
                     Scierror(999, _("%s: Wrong size for input argument #%d: An integer expected.\n"), "mopen", 3);
+                    FREE(pstFilename);
                     return types::Function::Error;
                 }
 
@@ -108,6 +112,7 @@ types::Function::ReturnValue sci_mopen(types::typed_list &in, int _iRetCount, ty
                 if (in.size() >= 4)
                 {
                     Scierror(999, _("%s: Wrong number of input arguments: %d to %d expected.\n"), "mopen" , 1, 3);
+                    FREE(pstFilename);
                     return types::Function::Error;
                 }
 
index 8cd568e..21c318e 100644 (file)
@@ -44,6 +44,7 @@ types::Function::ReturnValue sci_mput(types::typed_list &in, int _iRetCount, typ
     if (in.size() < 1 || in.size() > 3)
     {
         Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "mput", 1, 3);
+        FREE(pstType);
         return types::Function::Error;
     }
 
@@ -51,6 +52,7 @@ types::Function::ReturnValue sci_mput(types::typed_list &in, int _iRetCount, typ
     if ((in[0]->isDouble() == false) && (in[0]->isInt() == false))
     {
         Scierror(999, _("%s: Wrong type for input argument #%d: A integer expected.\n"), "mput", 1);
+        FREE(pstType);
         return types::Function::Error;
     }
 
@@ -62,6 +64,7 @@ types::Function::ReturnValue sci_mput(types::typed_list &in, int _iRetCount, typ
         if (in[1]->isString() == false || in[1]->getAs<types::String>()->getSize() != 1)
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "mput", 2);
+            FREE(pstType);
             return types::Function::Error;
         }
 
@@ -75,6 +78,7 @@ types::Function::ReturnValue sci_mput(types::typed_list &in, int _iRetCount, typ
         if (in[2]->isDouble() == false || in[2]->getAs<types::Double>()->getSize() != 1)
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A integer expected.\n"), "mput", 3);
+            FREE(pstType);
             return types::Function::Error;
         }
 
@@ -85,6 +89,7 @@ types::Function::ReturnValue sci_mput(types::typed_list &in, int _iRetCount, typ
     {
         case 5: // stdin
             Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mput", iFile);
+            FREE(pstType);
             return types::Function::Error;
     }
 
index 54a13ec..3af586f 100644 (file)
@@ -51,12 +51,14 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
     int retval_s    = 0;
     int rowcount    = -1;
     rec_entry buf[MAXSCAN];
-    entry *data;
-    sfdir type[MAXSCAN], type_s[MAXSCAN];
+    entry *data = NULL;
+    sfdir type[MAXSCAN] = {NONE};
+    sfdir type_s[MAXSCAN] = {NONE};
 
     if (size < 1 || size > 2)
     {
         Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "mscanf", 1, 2);
+        delete pIT;
         return types::Function::Error;
     }
 
@@ -65,6 +67,7 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
         if (in[0]->isDouble() == false || in[0]->getAs<types::Double>()->isScalar() == false || in[0]->getAs<types::Double>()->isComplex())
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A Real expected.\n"), "mscanf", 1);
+            delete pIT;
             return types::Function::Error;
         }
         iNiter = static_cast<int>(in[0]->getAs<types::Double>()->get(0));
@@ -77,6 +80,7 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
     if (in[size - 1]->isString() == false || in[size - 1]->getAs<types::String>()->isScalar() == false)
     {
         Scierror(999, _("%s: Wrong type for input argument #%d: A String expected.\n"), "mscanf", size);
+        delete pIT;
         return types::Function::Error;
     }
 
@@ -111,6 +115,7 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
         FREE(wcsRead);
         if (err < 0)
         {
+            delete pIT;
             return types::Function::Error;
         }
         err = Store_Scan(&nrow, &ncol, type_s, type, &retval, &retval_s, buf, &data, rowcount, args);
@@ -131,7 +136,6 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
                     Free_Scan(rowcount, ncol, type_s, &data);
                     Scierror(999, _("%s: No more memory.\n"), "mscanf");
                     return types::Function::Error;
-                    break;
             }
             if (err == DO_XXPRINTF_MISMATCH)
             {
@@ -201,6 +205,7 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
         if (sizeOfVector == 0)
         {
             out.push_back(new types::String(L""));
+            delete pIT;
             return types::Function::OK;
         }
 
@@ -284,6 +289,7 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
                             }
                             break;
                             default :
+                                delete pITTemp;
                                 return types::Function::Error;
                         }
                     }
@@ -304,6 +310,7 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
         }
     }
     Free_Scan(rowcount, ncol, type_s, &data);
+    delete pIT;
     return types::Function::OK;
 }
 /*--------------------------------------------------------------------------*/
index 487bcc8..3eead4d 100644 (file)
@@ -46,8 +46,9 @@ types::Function::ReturnValue sci_msscanf(types::typed_list &in, int _iRetCount,
     int retval_s    = 0;
     int rowcount    = -1;
     rec_entry buf[MAXSCAN];
-    entry *data;
-    sfdir type[MAXSCAN], type_s[MAXSCAN];
+    entry *data = NULL;
+    sfdir type[MAXSCAN]   = {NONE};
+    sfdir type_s[MAXSCAN] = {NONE};
 
     if (size < 2 || size > 3)
     {
@@ -117,7 +118,6 @@ types::Function::ReturnValue sci_msscanf(types::typed_list &in, int _iRetCount,
                     Free_Scan(rowcount, ncol, type_s, &data);
                     Scierror(999, _("%s: No more memory.\n"), "msscanf");
                     return types::Function::Error;
-                    break;
             }
             if (err == DO_XXPRINTF_MISMATCH)
             {
@@ -274,6 +274,7 @@ types::Function::ReturnValue sci_msscanf(types::typed_list &in, int _iRetCount,
                             }
                             break;
                             default :
+                                delete pITTemp;
                                 return types::Function::Error;
                         }
                     }
index 817b080..ae5e427 100644 (file)
@@ -38,6 +38,7 @@ types::Function::ReturnValue sci_pathsep(types::typed_list &in, int _iRetCount,
     if (in.size() > 0)
     {
         Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "pathsep", 0);
+        FREE(wcsSep);
         return types::Function::Error;
     }
 
index d10b362..5b80969 100644 (file)
@@ -64,6 +64,7 @@ char *createtempfilename(const char *prefix, BOOL bShortFormat)
         }
         tempfilename = os_strdup(TempFileName);
     }
+    FREE(TmpDir);
 #endif
     return tempfilename;
 }
@@ -111,11 +112,6 @@ wchar_t *createtempfilenameW(const wchar_t *wcprefix, BOOL bShortFormat)
         FREE(prefix);
         prefix = NULL;
     }
-    if (result)
-    {
-        FREE(result);
-        result = NULL;
-    }
 #endif
     return wcReturnedTempFilename;
 }
index 34709de..0db7952 100644 (file)
@@ -66,7 +66,7 @@ int do_xxscanf (const wchar_t *fname, FILE *fp, const wchar_t *format, int *narg
     int ignore_flag = 0;
     int str_flag = 0;
     int num_conversion = -1;
-    void *ptrtab[MAXSCAN];
+    void *ptrtab[MAXSCAN] = {NULL};
     wchar_t sformat[MAX_STR];
     wchar_t backupcurrrentchar;
     wchar_t directive;
index 41f86a6..751e0ea 100644 (file)
@@ -109,12 +109,6 @@ char *get_full_path(char *_FullPath, const char *_Path, size_t _SizeInBytes)
         {
             strcpy(_FullPath, pstWorkingPath);
             normalizePath(_FullPath);
-            FREE(_Path_start);
-            _Path_start = NULL;
-            FREE(_FullPath_start);
-            _FullPath_start = NULL;
-            FREE(_Path_tmp);
-            _Path_tmp = NULL;
         }
         else if (strcmp(_Path, _FullPath) != 0) // For case: fullpath("a/b/c") (2) or fullpath("../a/b/c") (3)
         {
@@ -122,15 +116,13 @@ char *get_full_path(char *_FullPath, const char *_Path, size_t _SizeInBytes)
             strtok(_Path_tmp, "./"); // _Path_tmp becomes a (2) or ../a (3)
             toadd = strsub(pstWorkingPath, _Path_tmp, ""); // to add = "/b/c"
             strcat(_FullPath, toadd); //_FullPath=_Fullpath+toadd
-            FREE(_Path_tmp);
-            _Path_tmp = NULL;
-            FREE(_Path_start);
-            _Path_start = NULL;
-            FREE(_FullPath_start);
-            _FullPath_start = NULL;
+            FREE(toadd);
         }
-
+        
         FREE(pstWorkingPath);
+        FREE(_FullPath_start);
+        FREE(_Path_start);
+        FREE(_Path_tmp);
     }
 
     lenFullPath = (int)strlen(_FullPath);
index 4790036..317d71f 100644 (file)
@@ -249,6 +249,7 @@ wchar_t* getrelativefilenameW(wchar_t *currentDirectory, wchar_t *absoluteFilena
     // check that the result will not be too long
     if (levels * 3 + afLen - afMarker > PATH_MAX)
     {
+        FREE(relativeFilename);
         return NULL;
     }
 
index 32acc1a..d2317a0 100644 (file)
@@ -29,6 +29,7 @@ extern "C"
 #include "mtell.h"
 #include "mseek.h"
 #include "sciprint.h"
+#include "freeArrayOfString.h"
 }
 
 #include <iostream>
@@ -110,6 +111,7 @@ wchar_t **mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr)
             {
                 *nbLinesOut = 0;
                 *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
+                FREE(Line);
                 return NULL;
             }
             while ( getLine ( Line, LINE_MAX * iLineSizeMult, pFile ) != NULL )
@@ -140,6 +142,7 @@ wchar_t **mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr)
                 {
                     *nbLinesOut = 0;
                     *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
+                    FREE(Line);
                     return NULL;
                 }
 
@@ -148,6 +151,8 @@ wchar_t **mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr)
                 {
                     *nbLinesOut = 0;
                     *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
+                    freeArrayOfWideString(strLines, nbLines);
+                    FREE(Line);
                     return NULL;
                 }
                 wcscpy(Line, EMPTYSTRW);
@@ -161,11 +166,6 @@ wchar_t **mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr)
             {
                 *ierr = MGETL_EOF;
                 *nbLinesOut = 0;
-                if (strLines)
-                {
-                    FREE(strLines);
-                }
-                strLines = NULL;
             }
             else
             {
@@ -176,6 +176,7 @@ wchar_t **mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr)
                 {
                     *nbLinesOut = 0;
                     *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
+                    FREE(Line);
                     return NULL;
                 }
 
@@ -209,6 +210,7 @@ wchar_t **mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr)
                                 wchar_t* tmpLine = os_wcsdup(Line);
                                 memset(Line, 0x00, LINE_MAX * iLineSizeMult);
                                 wcscpy(Line, &tmpLine[1]);
+                                FREE(tmpLine);
                             }
                             nbLines++;
                             strLines[nbLines - 1] = os_wcsdup(removeEOL(Line));
@@ -216,6 +218,8 @@ wchar_t **mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr)
                             {
                                 *nbLinesOut = 0;
                                 *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
+                                FREE(Line);
+                                freeArrayOfWideString(strLines, nbLines);
                                 return NULL;
                             }
                             wcscpy(Line, EMPTYSTRW);
index 9baeace..c8a0dec 100644 (file)
@@ -167,6 +167,7 @@ int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _
                         }
                         else
                         {
+                            FREE(wcsData);
                             return -10;
                         }
                     }
@@ -242,6 +243,7 @@ int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _
                         }
                         else
                         {
+                            FREE(wcsData);
                             return -10;
                         }
                     }
@@ -278,6 +280,7 @@ int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _
                         if (wscToFind == NULL)
                         {
                             // MALLOC error
+                            FREE(wcsData);
                             return -10;
                         }
 
@@ -299,6 +302,7 @@ int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _
                             }
                             else
                             {
+                                FREE(wcsData);
                                 return -10;
                             }
                         }
@@ -355,6 +359,7 @@ int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _
                         }
                         else
                         {
+                            FREE(wcsData);
                             return -10;
                         }
                     }
@@ -410,6 +415,7 @@ int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _
                                     }
                                     else
                                     {
+                                        FREE(wcsData);
                                         return -10;
                                     }
                                 }
@@ -437,6 +443,7 @@ int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _
                                     }
                                     else
                                     {
+                                        FREE(wcsData);
                                         return -10;
                                     }
                                 }
@@ -565,6 +572,7 @@ int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _
                             }
                             else
                             {
+                                FREE(wcsData);
                                 return -10;
                             }
                         }
@@ -625,6 +633,7 @@ int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _
                                 }
                                 else
                                 {
+                                    FREE(wcsData);
                                     return -10;
                                 }
                             }
@@ -646,6 +655,7 @@ int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _
                                 }
                                 else
                                 {
+                                    FREE(wcsData);
                                     return -10;
                                 }
                             }
@@ -674,6 +684,7 @@ int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _
                         }
                         else
                         {
+                            FREE(wcsData);
                             return -10;
                         }
                     }
@@ -721,10 +732,11 @@ int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _
                 break;
                 default :
                     printf("format read : %c\n", _wcsFormat[i]);
+                    FREE(wcsData);
                     return -10;
             }
     }
-
+    FREE(wcsData);
     return iCountDataRead;
 }