Skip to content

Commit ad0bc6c

Browse files
committed
aseert ofstream good() after close()
1 parent b018dc1 commit ad0bc6c

File tree

7 files changed

+41
-1
lines changed

7 files changed

+41
-1
lines changed

cli/cppcheckexecutor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ CppCheckExecutor::~CppCheckExecutor()
8080
try
8181
{
8282
mErrorOutput->close();
83+
assert(mErrorOutput->good());
8384
}
8485
catch (const std::ios_base::failure&)
8586
{

gui/checkthread.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ static bool executeCommand(std::string exe, std::vector<std::string> args, std::
7676
output = process.readAllStandardOutput().toStdString();
7777

7878
if (redirect.compare(0,3,"2> ") == 0) {
79-
std::ofstream fout();
79+
std::ofstream fout;
8080
fout.exceptions(std::ios_base::failbit | std::ios_base::badbit);
8181
fout.open(redirect.substr(3));
8282
assert(fout.is_open()); // If we ever disable exceptions, catch issues ASAP
8383
fout << process.readAllStandardError().toStdString();
84+
fout.close();
85+
assert(fout.good());
8486
}
8587
return process.exitCode() == 0;
8688
}

lib/analyzerinfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,17 @@ void AnalyzerInformation::writeFilesTxt(const std::string &buildDir, const std::
8080
const std::string afile = getFilename(fs.filename);
8181
fout << afile << ".a" << (++fileCount[afile]) << ":" << fs.cfg << ":" << Path::simplifyPath(Path::fromNativeSeparators(fs.filename)) << std::endl;
8282
}
83+
84+
assert(fout.good());
8385
}
8486

8587
void AnalyzerInformation::close()
8688
{
8789
if (mOutputStream.is_open()) {
8890
mOutputStream << "</analyzerinfo>\n";
91+
8992
mOutputStream.close();
93+
assert(mOutputStream.good());
9094
}
9195
}
9296

lib/cppcheck.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ static void createDumpFile(const Settings& settings,
288288
fout.exceptions(std::ios_base::failbit | std::ios_base::badbit);
289289
fout.open(getCtuInfoFileName(dumpFile));
290290
assert(fout.is_open()); // If we ever disable exceptions, catch issues ASAP
291+
fout.close();
292+
assert(fout.good()); // If we ever disable exceptions, catch issues ASAP
291293
}
292294

293295
std::string language;
@@ -421,6 +423,7 @@ CppCheck::~CppCheck()
421423
try
422424
{
423425
mPlistFile.close();
426+
assert(mPlistFile.good()); // If we ever disable exceptions, catch issues ASAP
424427
}
425428
catch (const std::ios_base::failure&)
426429
{
@@ -528,6 +531,8 @@ unsigned int CppCheck::check(const std::string &path)
528531
fout.open(clangcmd);
529532
assert(fout.is_open()); // If we ever disable exceptions, catch issues ASAP
530533
fout << exe << " " << args2 << " " << redirect2 << std::endl;
534+
fout.close();
535+
assert(fout.good()); // If we ever disable exceptions, catch issues ASAP
531536
} else if (mSettings.verbose && !mSettings.quiet) {
532537
mErrorLogger.reportOut(exe + " " + args2);
533538
}
@@ -562,6 +567,8 @@ unsigned int CppCheck::check(const std::string &path)
562567
fout.open(clangAst);
563568
assert(fout.is_open()); // If we ever disable exceptions, catch issues ASAP
564569
fout << output2 << std::endl;
570+
fout.close();
571+
assert(fout.good()); // If we ever disable exceptions, catch issues ASAP
565572
}
566573

567574
try {
@@ -595,6 +602,7 @@ unsigned int CppCheck::check(const std::string &path)
595602
fdump << "</dump>" << std::endl;
596603
fdump << "</dumps>" << std::endl;
597604
fdump.close();
605+
assert(fdump.good()); // If we ever disable exceptions, catch issues ASAP
598606
}
599607

600608
// run addons
@@ -690,7 +698,9 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
690698

691699
if (mPlistFile.is_open()) {
692700
mPlistFile << ErrorLogger::plistFooter();
701+
693702
mPlistFile.close();
703+
assert(mPlistFile.good()); // If we ever disable exceptions, catch issues ASAP
694704
}
695705

696706
CheckUnusedFunctions checkUnusedFunctions(nullptr, nullptr, nullptr);
@@ -1031,6 +1041,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
10311041
if (fdump.is_open()) {
10321042
fdump << "</dumps>" << std::endl;
10331043
fdump.close();
1044+
assert(fdump.good()); // If we ever disable exceptions, catch issues ASAP
10341045
}
10351046

10361047
executeAddons(dumpFile);
@@ -1459,6 +1470,9 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
14591470

14601471
for (const std::string& f: files)
14611472
fout << f << std::endl;
1473+
1474+
fout.close();
1475+
assert(fout.good()); // If we ever disable exceptions, catch issues ASAP
14621476
}
14631477

14641478
for (const std::string &addon : mSettings.addons) {
@@ -1722,6 +1736,9 @@ void CppCheck::analyseClangTidy(const ImportProject::FileSettings &fileSettings)
17221736
assert(fcmd.is_open()); // If we ever disable exceptions, catch issues ASAP
17231737

17241738
fcmd << istr.str();
1739+
1740+
fcmd.close();
1741+
assert(fcmd.good()); // If we ever disable exceptions, catch issues ASAP
17251742
}
17261743

17271744
while (std::getline(istr, line)) {

lib/summaries.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ std::string Summaries::create(const Tokenizer *tokenizer, const std::string &cfg
9292
assert(fout.is_open()); // If we ever disable exceptions, catch issues ASAP
9393

9494
fout << ostr.str();
95+
96+
fout.close();
97+
assert(fout.good()); // If we ever disable exceptions, catch issues ASAP
9598
}
9699
}
97100

test/helpers.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ ScopedFile::ScopedFile(std::string name, const std::string &content, std::string
6060
assert(of.is_open()); // If we ever disable exceptions, catch issues ASAP
6161

6262
of << content;
63+
64+
of.close();
65+
assert(of.good()); // If we ever disable exceptions, catch issues ASAP
6366
}
6467

6568
ScopedFile::~ScopedFile() {

tools/dmake.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ static int write_vcxproj(const std::string &proj_name, const std::function<void(
223223

224224
out << outstr;
225225

226+
out.close();
227+
assert(out.good()); // If we ever disable exceptions, catch issues ASAP
228+
226229
return EXIT_SUCCESS;
227230
}
228231

@@ -421,6 +424,9 @@ int main(int argc, char **argv)
421424
fout1 << " \\\n" << std::string(11, ' ');
422425
}
423426
fout1 << "\n";
427+
428+
fout1.close();
429+
assert(fout1.good()); // If we ever disable exceptions, catch issues ASAP
424430
}
425431

426432
static const char makefile[] = "Makefile";
@@ -762,5 +768,9 @@ int main(int argc, char **argv)
762768
compilefiles(fout, extfiles, emptyString);
763769
compilefiles(fout, toolsfiles, "${INCLUDE_FOR_LIB}");
764770

771+
772+
fout.close();
773+
assert(fout.good()); // If we ever disable exceptions, catch issues ASAP
774+
765775
return 0;
766776
}

0 commit comments

Comments
 (0)