Coverity: fileio module memory errors fixed. 57/18357/4
Dhruv Khattar [Fri, 8 Jul 2016 20:00:24 +0000 (01:00 +0530)]
Change-Id: Id94b314d16b036274b6a7769a3546cb70ea4d8d3

scilab/modules/fileio/sci_gateway/c/sci_fscanfMat.c
scilab/modules/fileio/sci_gateway/c/sci_pwd.c
scilab/modules/fileio/sci_gateway/cpp/sci_get_absolute_file_path.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_mfprintf.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_mscanf.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_msscanf.cpp
scilab/modules/fileio/src/c/copyfile_others.c
scilab/modules/fileio/src/c/dlManager.c
scilab/modules/fileio/src/c/readline.c
scilab/modules/fileio/src/cpp/mgetl.cpp
scilab/modules/fileio/src/cpp/scilab_sscanf.cpp

index 9135e34..12ec074 100644 (file)
@@ -154,13 +154,15 @@ int sci_fscanfMat(char *fname, void* pvApiCtx)
 
         for (i = 0; i < NB_DEFAULT_SUPPORTED_SEPARATORS; i++)
         {
+            if (results)
+            {
+                freeFscanfMatResult(results);
+            }
             results = fscanfMat(expandedFilename, Format, supportedSeparators[i]);
             if (results && results->err == FSCANFMAT_NO_ERROR)
             {
                 break;
             }
-
-            freeFscanfMatResult(results);
         }
     }
     else
@@ -169,6 +171,7 @@ int sci_fscanfMat(char *fname, void* pvApiCtx)
         if (results && results->err != FSCANFMAT_NO_ERROR)
         {
             freeFscanfMatResult(results);
+            results = NULL;
         }
     }
 
index e14c2cd..df9df0f 100644 (file)
@@ -53,15 +53,14 @@ int sci_pwd(char *fname, void* pvApiCtx)
         if (sciErr.iErr)
         {
             printError(&sciErr, 0);
+            FREE(path);
+            path = NULL;
             return 0;
         }
 
         LhsVar(1) = Rhs + 1;
-        if (path)
-        {
-            FREE(path);
-            path = NULL;
-        }
+        FREE(path);
+        path = NULL;
         PutLhsVar();
     }
 
index b35fc21..1999111 100644 (file)
@@ -79,14 +79,9 @@ types::Function::ReturnValue sci_get_absolute_file_path(types::typed_list &in, i
 
     freeArrayOfWideString(wcsFilesOpened, FileManager::getOpenedCount());
 
-    if (wcsTemp == NULL)
-    {
-        char* pstFile = wide_string_to_UTF8(wcsFileName);
-        Scierror(999, _("%s: The file %s is not opened in scilab.\n"), "get_absolute_file_path", pstFile);
-        FREE(pstFile);
-        return types::Function::Error;
-    }
-
-    return types::Function::OK;
+    char* pstFile = wide_string_to_UTF8(wcsFileName);
+    Scierror(999, _("%s: The file %s is not opened in scilab.\n"), "get_absolute_file_path", pstFile);
+    FREE(pstFile);
+    return types::Function::Error;
 }
 /*--------------------------------------------------------------------------*/
index 298a995..f461ab9 100644 (file)
@@ -114,6 +114,7 @@ types::Function::ReturnValue sci_mfprintf(types::typed_list &in, int _iRetCount,
             }
 #endif
             isSTDErr = TRUE;
+            break;
         case 6:
             isSTD = TRUE;
             break;
@@ -124,7 +125,7 @@ types::Function::ReturnValue sci_mfprintf(types::typed_list &in, int _iRetCount,
             isSTD = FALSE;
             types::File* pFile = FileManager::getFile(iFile);
             // file opened with fortran open function
-            if (pFile->getFileType() == 1)
+            if (!pFile || pFile->getFileType() == 1)
             {
                 Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mfprintf", iFile);
                 return types::Function::Error;
index 3af586f..d719ddc 100644 (file)
@@ -124,23 +124,15 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
             switch (err)
             {
                 case DO_XXPRINTF_MISMATCH:
-                    if (iNiter >= 0)
-                    {
-                        Free_Scan(rowcount, ncol, type_s, &data);
-                        Scierror(999, _("%s: Data mismatch.\n"), "mscanf");
-                        return types::Function::Error;
-                    }
-                    break;
+                    Free_Scan(rowcount, ncol, type_s, &data);
+                    Scierror(999, _("%s: Data mismatch.\n"), "mscanf");
+                    return types::Function::Error;
 
                 case DO_XXPRINTF_MEM_LACK:
                     Free_Scan(rowcount, ncol, type_s, &data);
                     Scierror(999, _("%s: No more memory.\n"), "mscanf");
                     return types::Function::Error;
             }
-            if (err == DO_XXPRINTF_MISMATCH)
-            {
-                break;
-            }
         }
     }
 
index 3eead4d..11e65c0 100644 (file)
@@ -106,23 +106,15 @@ types::Function::ReturnValue sci_msscanf(types::typed_list &in, int _iRetCount,
             switch (err)
             {
                 case DO_XXPRINTF_MISMATCH:
-                    if (iNiter >= 0)
-                    {
-                        Free_Scan(rowcount, ncol, type_s, &data);
-                        Scierror(999, _("%s: Data mismatch.\n"), "msscanf");
-                        return types::Function::Error;
-                    }
-                    break;
+                    Free_Scan(rowcount, ncol, type_s, &data);
+                    Scierror(999, _("%s: Data mismatch.\n"), "msscanf");
+                    return types::Function::Error;
 
                 case DO_XXPRINTF_MEM_LACK:
                     Free_Scan(rowcount, ncol, type_s, &data);
                     Scierror(999, _("%s: No more memory.\n"), "msscanf");
                     return types::Function::Error;
             }
-            if (err == DO_XXPRINTF_MISMATCH)
-            {
-                break;
-            }
         }
     }
 
index 19b47cf..04ac939 100644 (file)
@@ -142,16 +142,10 @@ static int CopyFileFunction_others(wchar_t *DestinationFilename, wchar_t *Source
 
     fchmod (dfd, st.st_mode & 0777);
     close (sfd);
-    if (pStrDest)
-    {
-        FREE(pStrDest);
-        pStrDest = NULL;
-    }
-    if (pStrSrc)
-    {
-        FREE(pStrSrc);
-        pStrSrc = NULL;
-    }
+    FREE(pStrDest);
+    pStrDest = NULL;
+    FREE(pStrSrc);
+    pStrSrc = NULL;
     return 0;
 
 err:
@@ -165,16 +159,10 @@ err:
         unlink (pStrDest);
     }
 
-    if (pStrDest)
-    {
-        FREE(pStrDest);
-        pStrDest = NULL;
-    }
-    if (pStrSrc)
-    {
-        FREE(pStrSrc);
-        pStrSrc = NULL;
-    }
+    FREE(pStrDest);
+    pStrDest = NULL;
+    FREE(pStrSrc);
+    pStrSrc = NULL;
     return status;
 }
 /*--------------------------------------------------------------------------*/
index 4ef8ee8..6929d09 100644 (file)
@@ -140,7 +140,7 @@ int getProxyValues(char **proxyHost, long *proxyPort, char **proxyUserPwd)
         }
         else
         {
-            *proxyUserPwd = (char *)MALLOC((ulen + 1 + plen + 1) * sizeof(char *));
+            *proxyUserPwd = (char *)MALLOC((ulen + 1 + plen + 1) * sizeof(char));
             sprintf(*proxyUserPwd, "%s:%s", values[3]/*user*/, values[4]/*password*/);
             (*proxyUserPwd)[ulen + 1 + plen] = '\0';
             FREE(values[3]);
index f840011..7a346e6 100644 (file)
@@ -47,7 +47,7 @@ int LineRead(int fd, char buf[], int n, int *cnt, int *nr)
             if (line && nbLinesReaded == 1)
             {
                 /* current limitation (bsiz) of line readed by scilab */
-                if ((int)wcslen(lines[0]) < bsiz)
+                if ((int)strlen(line) < bsiz)
                 {
                     strcpy(buf, line);
                     returnedInfo = READNEXTLINE_ERROR_EOL;
@@ -67,7 +67,7 @@ int LineRead(int fd, char buf[], int n, int *cnt, int *nr)
 
         case MGETL_EOF:
         {
-            if (lines)
+            if (line)
             {
                 if (nbLinesReaded == 0)
                 {
index d2317a0..32ca9f3 100644 (file)
@@ -116,7 +116,7 @@ wchar_t **mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr)
             }
             while ( getLine ( Line, LINE_MAX * iLineSizeMult, pFile ) != NULL )
             {
-                if (((int) wcslen(Line)) >= (LINE_MAX * iLineSizeMult) - 1)
+                if (((int) wcslen(Line)) >= (LINE_MAX * iLineSizeMult) - 1 && iPos >= 0)
                 {
                     FREE(Line);
                     iLineSizeMult++;
index ff73394..ff21d4c 100644 (file)
@@ -438,15 +438,15 @@ int scilab_sscanf(wchar_t* _wcsFormat, wchar_t* _wcsData, int _iIterrator, int _
                                 iSingleData = wcstoul(wcsData, &wcsData, base);
                                 if ((iSingleData == 0) && (temp == wcsData[0]))
                                 {
+                                    FREE(wcsData);
+                                    wcsData = NULL;
                                     if (_iIterrator == 0 && !bStar && _iNiter == 1)
                                     {
-                                        wcsData = NULL;
                                         _pITOut->push_back(types::Double::Empty());
                                         bStar = TRUE;
                                     }
                                     else
                                     {
-                                        FREE(wcsData);
                                         return -10;
                                     }
                                 }