@@ -276,14 +276,18 @@ static void createDumpFile(const Settings& settings,
276276 dumpFile = getDumpFileName (settings, filename);
277277
278278 fdump.open (dumpFile);
279+ assert (fdump.is_open ()); // If we ever disable exceptions, catch issues ASAP
280+
279281 // TODO: Technically, exceptions are allowed on the file so this is always false,
280282 // TODO: but the function is not aware of that as this function is not the owner of the file
281283 if (!fdump.is_open ())
282284 return ;
283285
284286 {
285- std::ofstream fout ( getCtuInfoFileName (dumpFile)) ;
287+ std::ofstream fout;
286288 fout.exceptions (std::ios_base::failbit | std::ios_base::badbit);
289+ fout.open (getCtuInfoFileName (dumpFile));
290+ assert (fout.is_open ()); // If we ever disable exceptions, catch issues ASAP
287291 }
288292
289293 std::string language;
@@ -399,7 +403,9 @@ CppCheck::CppCheck(ErrorLogger &errorLogger,
399403 , mTooManyConfigs(false )
400404 , mSimplify(true )
401405 , mExecuteCommand(std::move(executeCommand))
402- {}
406+ {
407+ mPlistFile .exceptions (std::ios_base::failbit | std::ios_base::badbit);
408+ }
403409
404410CppCheck::~CppCheck ()
405411{
@@ -411,7 +417,17 @@ CppCheck::~CppCheck()
411417
412418 if (mPlistFile .is_open ()) {
413419 mPlistFile << ErrorLogger::plistFooter ();
414- mPlistFile .close ();
420+
421+ try
422+ {
423+ mPlistFile .close ();
424+ }
425+ catch (const std::ios_base::failure&)
426+ {
427+ assert (false );
428+
429+ // TODO report error
430+ }
415431 }
416432}
417433
@@ -507,8 +523,10 @@ unsigned int CppCheck::check(const std::string &path)
507523 const std::string args2 = " -fsyntax-only -Xclang -ast-dump -fno-color-diagnostics " + flags + path;
508524 const std::string redirect2 = analyzerInfo.empty () ? std::string (" 2>&1" ) : (" 2> " + clangStderr);
509525 if (!mSettings .buildDir .empty ()) {
510- std::ofstream fout (clangcmd) ;
526+ std::ofstream fout;
511527 fout.exceptions (std::ios_base::failbit | std::ios_base::badbit);
528+ fout.open (clangcmd);
529+ assert (fout.is_open ()); // If we ever disable exceptions, catch issues ASAP
512530 fout << exe << " " << args2 << " " << redirect2 << std::endl;
513531 } else if (mSettings .verbose && !mSettings .quiet ) {
514532 mErrorLogger .reportOut (exe + " " + args2);
@@ -539,8 +557,10 @@ unsigned int CppCheck::check(const std::string &path)
539557 }
540558
541559 if (!mSettings .buildDir .empty ()) {
542- std::ofstream fout (clangAst) ;
560+ std::ofstream fout;
543561 fout.exceptions (std::ios_base::failbit | std::ios_base::badbit);
562+ fout.open (clangAst);
563+ assert (fout.is_open ()); // If we ever disable exceptions, catch issues ASAP
544564 fout << output2 << std::endl;
545565 }
546566
@@ -722,9 +742,12 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
722742 filename2 = filename.substr (filename.rfind (' /' ) + 1 );
723743 else
724744 filename2 = filename;
745+
725746 const std::size_t fileNameHash = std::hash<std::string> {}(filename);
726747 filename2 = mSettings .plistOutput + filename2.substr (0 , filename2.find (' .' )) + " _" + std::to_string (fileNameHash) + " .plist" ;
727748 mPlistFile .open (filename2);
749+ assert (mPlistFile .is_open ());// If we ever disable exceptions, catch issues ASAP
750+
728751 mPlistFile << ErrorLogger::plistHeader (version (), files);
729752 }
730753
@@ -1429,8 +1452,11 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
14291452
14301453 if (files.size () >= 2 || endsWith (files[0 ], " .ctu-info" )) {
14311454 fileList = Path::getPathFromFilename (files[0 ]) + FILELIST;
1432- std::ofstream fout (fileList) ;
1455+ std::ofstream fout;
14331456 fout.exceptions (std::ios_base::failbit | std::ios_base::badbit);
1457+ fout.open (fileList);
1458+ assert (fout.is_open ()); // If we ever disable exceptions, catch issues ASAP
1459+
14341460 for (const std::string& f: files)
14351461 fout << f << std::endl;
14361462 }
@@ -1689,8 +1715,12 @@ void CppCheck::analyseClangTidy(const ImportProject::FileSettings &fileSettings)
16891715
16901716 if (!mSettings .buildDir .empty ()) {
16911717 const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile (mSettings .buildDir , fileSettings.filename , emptyString);
1692- std::ofstream fcmd (analyzerInfoFile + " .clang-tidy-cmd" );
1718+
1719+ std::ofstream fcmd;
16931720 fcmd.exceptions (std::ios_base::failbit | std::ios_base::badbit);
1721+ fcmd.open (analyzerInfoFile + " .clang-tidy-cmd" );
1722+ assert (fcmd.is_open ()); // If we ever disable exceptions, catch issues ASAP
1723+
16941724 fcmd << istr.str ();
16951725 }
16961726
0 commit comments