Skip to content

Commit 5bd7ac5

Browse files
committed
Make ofstreams thow exception on write issues
1 parent a6b0129 commit 5bd7ac5

File tree

7 files changed

+15
-8
lines changed

7 files changed

+15
-8
lines changed

cli/cppcheckexecutor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck)
267267

268268
if (!settings.outputFile.empty()) {
269269
mErrorOutput = new std::ofstream(settings.outputFile);
270+
mErrorOutput->exceptions(std::ios_base::failbit | std::ios_base::badbit);
270271
}
271272

272273
if (settings.xml) {

gui/checkthread.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ static bool executeCommand(std::string exe, std::vector<std::string> args, std::
7777

7878
if (redirect.compare(0,3,"2> ") == 0) {
7979
std::ofstream fout(redirect.substr(3));
80+
fout->exceptions(std::ios_base::failbit | std::ios_base::badbit);
8081
fout << process.readAllStandardError().toStdString();
8182
}
8283
return process.exitCode() == 0;

lib/analyzerinfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void AnalyzerInformation::writeFilesTxt(const std::string &buildDir, const std::
5050

5151
const std::string filesTxt(buildDir + "/files.txt");
5252
std::ofstream fout(filesTxt);
53+
fout.exceptions(std::ios_base::failbit | std::ios_base::badbit);
5354
for (const std::string &f : sourcefiles) {
5455
const std::string afile = getFilename(f);
5556
fout << afile << ".a" << (++fileCount[afile]) << "::" << Path::simplifyPath(Path::fromNativeSeparators(f)) << '\n';

lib/cppcheck.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ static void createDumpFile(const Settings& settings,
281281

282282
{
283283
std::ofstream fout(getCtuInfoFileName(dumpFile));
284+
fout.exceptions(std::ios_base::failbit | std::ios_base::badbit);
284285
}
285286

286287
std::string language;
@@ -505,6 +506,7 @@ unsigned int CppCheck::check(const std::string &path)
505506
const std::string redirect2 = analyzerInfo.empty() ? std::string("2>&1") : ("2> " + clangStderr);
506507
if (!mSettings.buildDir.empty()) {
507508
std::ofstream fout(clangcmd);
509+
fout.exceptions(std::ios_base::failbit | std::ios_base::badbit);
508510
fout << exe << " " << args2 << " " << redirect2 << std::endl;
509511
} else if (mSettings.verbose && !mSettings.quiet) {
510512
mErrorLogger.reportOut(exe + " " + args2);
@@ -536,6 +538,7 @@ unsigned int CppCheck::check(const std::string &path)
536538

537539
if (!mSettings.buildDir.empty()) {
538540
std::ofstream fout(clangAst);
541+
fout.exceptions(std::ios_base::failbit | std::ios_base::badbit);
539542
fout << output2 << std::endl;
540543
}
541544

@@ -555,6 +558,7 @@ unsigned int CppCheck::check(const std::string &path)
555558

556559
// create dumpfile
557560
std::ofstream fdump;
561+
fdump.exceptions(std::ios_base::failbit | std::ios_base::badbit);
558562
std::string dumpFile;
559563
createDumpFile(mSettings, path, fdump, dumpFile);
560564
if (fdump.is_open()) {
@@ -773,6 +777,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
773777

774778
// write dump file xml prolog
775779
std::ofstream fdump;
780+
fdump.exceptions(std::ios_base::failbit | std::ios_base::badbit);
776781
std::string dumpFile;
777782
createDumpFile(mSettings, filename, fdump, dumpFile);
778783
if (fdump.is_open()) {
@@ -1423,6 +1428,7 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
14231428
if (files.size() >= 2 || endsWith(files[0], ".ctu-info")) {
14241429
fileList = Path::getPathFromFilename(files[0]) + FILELIST;
14251430
std::ofstream fout(fileList);
1431+
fout.exceptions(std::ios_base::failbit | std::ios_base::badbit);
14261432
for (const std::string& f: files)
14271433
fout << f << std::endl;
14281434
}
@@ -1682,6 +1688,7 @@ void CppCheck::analyseClangTidy(const ImportProject::FileSettings &fileSettings)
16821688
if (!mSettings.buildDir.empty()) {
16831689
const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, fileSettings.filename, emptyString);
16841690
std::ofstream fcmd(analyzerInfoFile + ".clang-tidy-cmd");
1691+
fcmd.exceptions(std::ios_base::failbit | std::ios_base::badbit);
16851692
fcmd << istr.str();
16861693
}
16871694

lib/summaries.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ std::string Summaries::create(const Tokenizer *tokenizer, const std::string &cfg
8787
if (pos != std::string::npos) {
8888
filename[pos+1] = 's';
8989
std::ofstream fout(filename);
90+
fout.exceptions(std::ios_base::failbit | std::ios_base::badbit);
9091
fout << ostr.str();
9192
}
9293
}

test/helpers.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ ScopedFile::ScopedFile(std::string name, const std::string &content, std::string
5555
}
5656

5757
std::ofstream of(mFullPath);
58-
if (!of.is_open())
59-
throw std::runtime_error("ScopedFile(" + mFullPath + ") - could not open file");
58+
of.exceptions(std::ios_base::failbit | std::ios_base::badbit);
6059
of << content;
6160
}
6261

tools/dmake.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ static int write_vcxproj(const std::string &proj_name, const std::function<void(
216216

217217
// treat as binary to prevent implicit line ending conversions
218218
std::ofstream out(proj_name, std::ios::binary|std::ios::trunc);
219+
out.exceptions(std::ios_base::failbit | std::ios_base::badbit);
219220
out << outstr;
220221

221222
return EXIT_SUCCESS;
@@ -395,6 +396,7 @@ int main(int argc, char **argv)
395396
// QMAKE - lib/lib.pri
396397
{
397398
std::ofstream fout1("lib/lib.pri");
399+
fout1.exceptions(std::ios_base::failbit | std::ios_base::badbit);
398400
if (fout1.is_open()) {
399401
fout1 << "# no manual edits - this file is autogenerated by dmake\n\n";
400402
fout1 << "include($$PWD/pcrerules.pri)\n";
@@ -418,12 +420,7 @@ int main(int argc, char **argv)
418420

419421
static const char makefile[] = "Makefile";
420422
std::ofstream fout(makefile, std::ios_base::trunc);
421-
if (!fout.is_open()) {
422-
std::cerr << "An error occurred while trying to open "
423-
<< makefile
424-
<< ".\n";
425-
return EXIT_FAILURE;
426-
}
423+
fout.exceptions(std::ios_base::failbit | std::ios_base::badbit);
427424

428425
fout << "# This file is generated by tools/dmake, do not edit.\n\n";
429426

0 commit comments

Comments
 (0)