failed on exec correctly close file 96/17296/3
Antoine ELIAS [Thu, 8 Oct 2015 09:22:41 +0000 (11:22 +0200)]
Change-Id: If985cb040cadfe1a762b071f4aba35d0c32e791d

scilab/modules/functions/sci_gateway/cpp/sci_exec.cpp

index a204ade..89dd6de 100644 (file)
@@ -49,6 +49,25 @@ void printLine(const std::string& _stPrompt, const std::string& _stLine, bool _b
 std::string printExp(std::ifstream& _File, ast::Exp* _pExp, const std::string& _stPrompt, int* _piLine /* in/out */, int* _piCol /* in/out */, std::string& _stPreviousBuffer);
 std::string getExpression(const std::string& _stFile, ast::Exp* _pExp);
 
+void closeFile(std::ifstream* file, int fileId, const std::wstring& wstFile, ast::Exp* pExp)
+{
+    if (file)
+    {
+        file->close();
+        delete file;
+
+        if (pExp)
+        {
+            delete pExp;
+        }
+
+        // Check if file has not already been closed (for ex mclose('all') in function)
+        if (FileManager::isOpened(wstFile))
+        {
+            mclose(fileId);
+        }
+    }
+}
 /*--------------------------------------------------------------------------*/
 types::Function::ReturnValue sci_exec(types::typed_list &in, int _iRetCount, types::typed_list &out)
 {
@@ -158,8 +177,7 @@ types::Function::ReturnValue sci_exec(types::typed_list &in, int _iRetCount, typ
         /*fake call to mopen to show file within file()*/
         if (mopen(pwstTemp, L"r", 0, &iID) != MOPEN_NO_ERROR)
         {
-            file->close();
-            delete file;
+            closeFile(file, iID, wstFile, pExp);
             FREE(pwstTemp);
             Scierror(999, _("%s: Cannot open file %s.\n"), "exec", stFile.data());
             return types::Function::Error;
@@ -170,8 +188,7 @@ types::Function::ReturnValue sci_exec(types::typed_list &in, int _iRetCount, typ
         FREE(pwstTemp);
         if (parser.getExitStatus() !=  Parser::Succeded)
         {
-            file->close();
-            delete file;
+            closeFile(file, iID, wstFile, pExp);
             if (bErrCatch)
             {
                 out.push_back(new types::Double(999));
@@ -180,11 +197,6 @@ types::Function::ReturnValue sci_exec(types::typed_list &in, int _iRetCount, typ
                 ConfigVariable::setLastErrorMessage(parser.getErrorMessage());
                 ConfigVariable::setLastErrorNumber(999);
                 delete parser.getTree();
-                // Check if file has not already been closed (for ex mclose('all') in function)
-                if (FileManager::isOpened(wstFile.data()) == true)
-                {
-                    mclose(iID);
-                }
                 ThreadManagement::UnlockParser();
                 return types::Function::OK;
             }
@@ -194,11 +206,6 @@ types::Function::ReturnValue sci_exec(types::typed_list &in, int _iRetCount, typ
             FREE(pst);
 
             delete parser.getTree();
-            // Check if file has not already been closed (for ex mclose('all') in function)
-            if (FileManager::isOpened(wstFile.data()) == true)
-            {
-                mclose(iID);
-            }
             ThreadManagement::UnlockParser();
             return types::Function::Error;
         }
@@ -320,26 +327,14 @@ types::Function::ReturnValue sci_exec(types::typed_list &in, int _iRetCount, typ
         }
         catch (const ast::InternalAbort& ia)
         {
-            if (file)
-            {
-                file->close();
-                delete file;
-                delete pExp;
-            }
-
+            closeFile(file, iID, wstFile, pExp);
             throw ia;
         }
         catch (const ast::InternalError& ie)
         {
             if (bErrCatch == false)
             {
-                if (file)
-                {
-                    file->close();
-                    delete file;
-                    delete pExp;
-                }
-
+                closeFile(file, iID, wstFile, pExp);
                 ConfigVariable::setPromptMode(oldVal);
                 ConfigVariable::setExecutedFileID(0);
                 throw ie;
@@ -432,17 +427,7 @@ types::Function::ReturnValue sci_exec(types::typed_list &in, int _iRetCount, typ
             }
             catch (const ast::InternalAbort& ia)
             {
-                if (file)
-                {
-                    delete pExp;
-                    // Check if file has not already been closed (for ex mclose('all') in function)
-                    if (FileManager::isOpened(wstFile.data()) == true)
-                    {
-                        mclose(iID);
-                    }
-                    file->close();
-                    delete file;
-                }
+                closeFile(file, iID, wstFile, pExp);
 
                 //restore previous prompt mode
                 ConfigVariable::setPromptMode(oldVal);
@@ -469,17 +454,7 @@ types::Function::ReturnValue sci_exec(types::typed_list &in, int _iRetCount, typ
                 iErr = ConfigVariable::getLastErrorNumber();
                 if (bErrCatch == false)
                 {
-                    if (file)
-                    {
-                        delete pExp;
-                        // Check if file has not already been closed (for ex mclose('all') in function)
-                        if (FileManager::isOpened(wstFile.data()) == true)
-                        {
-                            mclose(iID);
-                        }
-                        file->close();
-                        delete file;
-                    }
+                    closeFile(file, iID, wstFile, pExp);
 
                     //restore previous prompt mode
                     ConfigVariable::setPromptMode(oldVal);
@@ -524,19 +499,7 @@ types::Function::ReturnValue sci_exec(types::typed_list &in, int _iRetCount, typ
         ConfigVariable::setLastErrorCall();
     }
 
-    if (file)
-    {
-        delete pExp;
-        // Check if file has not already been closed (for ex mclose('all') in function)
-        if (FileManager::isOpened(wstFile.data()) == true)
-        {
-            mclose(iID);
-        }
-
-        file->close();
-        delete file;
-    }
-
+    closeFile(file, iID, wstFile, pExp);
     return types::Function::OK;
 }