@@ -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,17 @@ 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+ assert (false );
426+
427+ // TODO report error
428+ }
412429 }
413430}
414431
@@ -504,7 +521,9 @@ unsigned int CppCheck::check(const std::string &path)
504521 const std::string args2 = " -fsyntax-only -Xclang -ast-dump -fno-color-diagnostics " + flags + path;
505522 const std::string redirect2 = analyzerInfo.empty () ? std::string (" 2>&1" ) : (" 2> " + clangStderr);
506523 if (!mSettings .buildDir .empty ()) {
507- std::ofstream fout (clangcmd);
524+ std::ofstream fout;
525+ fout.exceptions (std::ios_base::failbit | std::ios_base::badbit);
526+ fout.open (clangcmd);
508527 fout << exe << " " << args2 << " " << redirect2 << std::endl;
509528 } else if (mSettings .verbose && !mSettings .quiet ) {
510529 mErrorLogger .reportOut (exe + " " + args2);
@@ -535,7 +554,9 @@ unsigned int CppCheck::check(const std::string &path)
535554 }
536555
537556 if (!mSettings .buildDir .empty ()) {
538- std::ofstream fout (clangAst);
557+ std::ofstream fout;
558+ fout.exceptions (std::ios_base::failbit | std::ios_base::badbit);
559+ fout.open (clangAst);
539560 fout << output2 << std::endl;
540561 }
541562
@@ -555,6 +576,7 @@ unsigned int CppCheck::check(const std::string &path)
555576
556577 // create dumpfile
557578 std::ofstream fdump;
579+ fdump.exceptions (std::ios_base::failbit | std::ios_base::badbit);
558580 std::string dumpFile;
559581 createDumpFile (mSettings , path, fdump, dumpFile);
560582 if (fdump.is_open ()) {
@@ -716,9 +738,11 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
716738 filename2 = filename.substr (filename.rfind (' /' ) + 1 );
717739 else
718740 filename2 = filename;
741+
719742 const std::size_t fileNameHash = std::hash<std::string> {}(filename);
720743 filename2 = mSettings .plistOutput + filename2.substr (0 , filename2.find (' .' )) + " _" + std::to_string (fileNameHash) + " .plist" ;
721744 mPlistFile .open (filename2);
745+
722746 mPlistFile << ErrorLogger::plistHeader (version (), files);
723747 }
724748
@@ -773,6 +797,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
773797
774798 // write dump file xml prolog
775799 std::ofstream fdump;
800+ fdump.exceptions (std::ios_base::failbit | std::ios_base::badbit);
776801 std::string dumpFile;
777802 createDumpFile (mSettings , filename, fdump, dumpFile);
778803 if (fdump.is_open ()) {
@@ -1422,7 +1447,9 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
14221447
14231448 if (files.size () >= 2 || endsWith (files[0 ], " .ctu-info" )) {
14241449 fileList = Path::getPathFromFilename (files[0 ]) + FILELIST;
1425- std::ofstream fout (fileList);
1450+ std::ofstream fout;
1451+ fout.exceptions (std::ios_base::failbit | std::ios_base::badbit);
1452+ fout.open (fileList);
14261453 for (const std::string& f: files)
14271454 fout << f << std::endl;
14281455 }
@@ -1681,7 +1708,10 @@ void CppCheck::analyseClangTidy(const ImportProject::FileSettings &fileSettings)
16811708
16821709 if (!mSettings .buildDir .empty ()) {
16831710 const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile (mSettings .buildDir , fileSettings.filename , emptyString);
1684- std::ofstream fcmd (analyzerInfoFile + " .clang-tidy-cmd" );
1711+
1712+ std::ofstream fcmd;
1713+ fcmd.exceptions (std::ios_base::failbit | std::ios_base::badbit);
1714+ fcmd.open (analyzerInfoFile + " .clang-tidy-cmd" );
16851715 fcmd << istr.str ();
16861716 }
16871717
0 commit comments