@@ -276,11 +276,16 @@ static void createDumpFile(const Settings& settings,
276276 dumpFile = getDumpFileName (settings, filename);
277277
278278 fdump.open (dumpFile);
279+
280+ // TODO: Technically, exceptions are allowed on the file so this is always false,
281+ // TODO: but the function is not aware of that as this function is not the owner of the file
279282 if (!fdump.is_open ())
280283 return ;
281284
282285 {
283- std::ofstream fout (getCtuInfoFileName (dumpFile));
286+ std::ofstream fout;
287+ fout.exceptions (std::ios_base::failbit | std::ios_base::badbit);
288+ fout.open (getCtuInfoFileName (dumpFile));
284289 }
285290
286291 std::string language;
@@ -396,7 +401,9 @@ CppCheck::CppCheck(ErrorLogger &errorLogger,
396401 , mTooManyConfigs(false )
397402 , mSimplify(true )
398403 , mExecuteCommand(std::move(executeCommand))
399- {}
404+ {
405+ mPlistFile .exceptions (std::ios_base::failbit | std::ios_base::badbit);
406+ }
400407
401408CppCheck::~CppCheck ()
402409{
@@ -408,7 +415,16 @@ CppCheck::~CppCheck()
408415
409416 if (mPlistFile .is_open ()) {
410417 mPlistFile << ErrorLogger::plistFooter ();
411- mPlistFile .close ();
418+
419+ try
420+ {
421+ mPlistFile .close ();
422+ }
423+ catch (const std::ios_base::failure&)
424+ {
425+ // TODO report error
426+ assert (false );
427+ }
412428 }
413429}
414430
@@ -504,7 +520,9 @@ unsigned int CppCheck::check(const std::string &path)
504520 const std::string args2 = " -fsyntax-only -Xclang -ast-dump -fno-color-diagnostics " + flags + path;
505521 const std::string redirect2 = analyzerInfo.empty () ? std::string (" 2>&1" ) : (" 2> " + clangStderr);
506522 if (!mSettings .buildDir .empty ()) {
507- std::ofstream fout (clangcmd);
523+ std::ofstream fout;
524+ fout.exceptions (std::ios_base::failbit | std::ios_base::badbit);
525+ fout.open (clangcmd);
508526 fout << exe << " " << args2 << " " << redirect2 << std::endl;
509527 } else if (mSettings .verbose && !mSettings .quiet ) {
510528 mErrorLogger .reportOut (exe + " " + args2);
@@ -535,7 +553,9 @@ unsigned int CppCheck::check(const std::string &path)
535553 }
536554
537555 if (!mSettings .buildDir .empty ()) {
538- std::ofstream fout (clangAst);
556+ std::ofstream fout;
557+ fout.exceptions (std::ios_base::failbit | std::ios_base::badbit);
558+ fout.open (clangAst);
539559 fout << output2 << std::endl;
540560 }
541561
@@ -555,6 +575,7 @@ unsigned int CppCheck::check(const std::string &path)
555575
556576 // create dumpfile
557577 std::ofstream fdump;
578+ fdump.exceptions (std::ios_base::failbit | std::ios_base::badbit);
558579 std::string dumpFile;
559580 createDumpFile (mSettings , path, fdump, dumpFile);
560581 if (fdump.is_open ()) {
@@ -773,6 +794,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
773794
774795 // write dump file xml prolog
775796 std::ofstream fdump;
797+ fdump.exceptions (std::ios_base::failbit | std::ios_base::badbit);
776798 std::string dumpFile;
777799 createDumpFile (mSettings , filename, fdump, dumpFile);
778800 if (fdump.is_open ()) {
@@ -1422,7 +1444,9 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
14221444
14231445 if (files.size () >= 2 || endsWith (files[0 ], " .ctu-info" )) {
14241446 fileList = Path::getPathFromFilename (files[0 ]) + FILELIST;
1425- std::ofstream fout (fileList);
1447+ std::ofstream fout;
1448+ fout.exceptions (std::ios_base::failbit | std::ios_base::badbit);
1449+ fout.open (fileList);
14261450 for (const std::string& f: files)
14271451 fout << f << std::endl;
14281452 }
@@ -1681,7 +1705,10 @@ void CppCheck::analyseClangTidy(const ImportProject::FileSettings &fileSettings)
16811705
16821706 if (!mSettings .buildDir .empty ()) {
16831707 const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile (mSettings .buildDir , fileSettings.filename , emptyString);
1684- std::ofstream fcmd (analyzerInfoFile + " .clang-tidy-cmd" );
1708+
1709+ std::ofstream fcmd;
1710+ fcmd.exceptions (std::ios_base::failbit | std::ios_base::badbit);
1711+ fcmd.open (analyzerInfoFile + " .clang-tidy-cmd" );
16851712 fcmd << istr.str ();
16861713 }
16871714
0 commit comments