Skip to content

Commit b018dc1

Browse files
committed
catch open issues earlier and add asserts
1 parent 684d5fc commit b018dc1

File tree

7 files changed

+83
-20
lines changed

7 files changed

+83
-20
lines changed

cli/cppcheckexecutor.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#endif
4444

4545
#include <algorithm>
46+
#include <cassert>
4647
#include <cstdio>
4748
#include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE
4849
#include <functional>
@@ -75,7 +76,20 @@ CppCheckExecutor::CppCheckExecutor()
7576

7677
CppCheckExecutor::~CppCheckExecutor()
7778
{
78-
delete mErrorOutput;
79+
if (mErrorOutput) {
80+
try
81+
{
82+
mErrorOutput->close();
83+
}
84+
catch (const std::ios_base::failure&)
85+
{
86+
assert(false);
87+
88+
// TODO report error
89+
}
90+
91+
delete mErrorOutput;
92+
}
7993
}
8094

8195
bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* const argv[])
@@ -266,8 +280,10 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck)
266280
mLatestProgressOutputTime = std::time(nullptr);
267281

268282
if (!settings.outputFile.empty()) {
269-
mErrorOutput = new std::ofstream(settings.outputFile);
283+
mErrorOutput = new std::ofstream();
270284
mErrorOutput->exceptions(std::ios_base::failbit | std::ios_base::badbit);
285+
mErrorOutput->open(settings.outputFile);
286+
assert(mErrorOutput->is_open()); // If we ever disable exceptions, catch issues ASAP
271287
}
272288

273289
if (settings.xml) {

gui/checkthread.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ 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(redirect.substr(3));
79+
std::ofstream fout();
8080
fout.exceptions(std::ios_base::failbit | std::ios_base::badbit);
81+
fout.open(redirect.substr(3));
82+
assert(fout.is_open()); // If we ever disable exceptions, catch issues ASAP
8183
fout << process.readAllStandardError().toStdString();
8284
}
8385
return process.exitCode() == 0;

lib/analyzerinfo.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ void AnalyzerInformation::writeFilesTxt(const std::string &buildDir, const std::
6464
std::map<std::string, unsigned int> fileCount;
6565

6666
const std::string filesTxt(buildDir + "/files.txt");
67-
std::ofstream fout(filesTxt);
67+
std::ofstream fout;
6868
fout.exceptions(std::ios_base::failbit | std::ios_base::badbit);
69+
fout.open(filesTxt);
70+
assert(fout.is_open()); // If we ever disable exceptions, catch issues ASAP
71+
6972
for (const std::string &f : sourcefiles) {
7073
const std::string afile = getFilename(f);
7174
fout << afile << ".a" << (++fileCount[afile]) << "::" << Path::simplifyPath(Path::fromNativeSeparators(f)) << '\n';
@@ -154,9 +157,7 @@ bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::st
154157
return false;
155158

156159
mOutputStream.open(mAnalyzerInfoFile);
157-
// If we ever disable exceptions on mOutputStream
158-
// TODO: add this assert to all ofstreams
159-
assert(mOutputStream.is_open());
160+
assert(mOutputStream.is_open()); // If we ever disable exceptions on mOutputStream, catch issues ASAP
160161

161162
mOutputStream << "<?xml version=\"1.0\"?>\n";
162163
mOutputStream << "<analyzerinfo hash=\"" << hash << "\">\n";

lib/cppcheck.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

404410
CppCheck::~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

lib/summaries.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ std::string Summaries::create(const Tokenizer *tokenizer, const std::string &cfg
8686
const std::string::size_type pos = filename.rfind(".a");
8787
if (pos != std::string::npos) {
8888
filename[pos+1] = 's';
89-
std::ofstream fout(filename);
89+
std::ofstream fout;
9090
fout.exceptions(std::ios_base::failbit | std::ios_base::badbit);
91+
fout.open(filename);
92+
assert(fout.is_open()); // If we ever disable exceptions, catch issues ASAP
93+
9194
fout << ostr.str();
9295
}
9396
}

test/helpers.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ ScopedFile::ScopedFile(std::string name, const std::string &content, std::string
5454
#endif
5555
}
5656

57-
std::ofstream of(mFullPath);
57+
std::ofstream of;
5858
of.exceptions(std::ios_base::failbit | std::ios_base::badbit);
59+
of.open(mFullPath);
60+
assert(of.is_open()); // If we ever disable exceptions, catch issues ASAP
61+
5962
of << content;
6063
}
6164

tools/dmake.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <algorithm>
2222
#include <array>
23+
#include <cassert>
2324
#include <cstdlib>
2425
#include <fstream> // IWYU pragma: keep
2526
#include <functional>
@@ -214,9 +215,12 @@ static int write_vcxproj(const std::string &proj_name, const std::function<void(
214215
outstr.resize(pos+1, '\0');
215216
}
216217

217-
// treat as binary to prevent implicit line ending conversions
218-
std::ofstream out(proj_name, std::ios::binary|std::ios::trunc);
218+
std::ofstream out;
219219
out.exceptions(std::ios_base::failbit | std::ios_base::badbit);
220+
// treat as binary to prevent implicit line ending conversions
221+
out.open(proj_name, std::ios::binary|std::ios::trunc);
222+
assert(out.is_open()); // If we ever disable exceptions, catch issues ASAP
223+
220224
out << outstr;
221225

222226
return EXIT_SUCCESS;
@@ -395,8 +399,10 @@ int main(int argc, char **argv)
395399

396400
// QMAKE - lib/lib.pri
397401
{
398-
std::ofstream fout1("lib/lib.pri");
402+
std::ofstream fout1;
399403
fout1.exceptions(std::ios_base::failbit | std::ios_base::badbit);
404+
fout1.open("lib/lib.pri");
405+
assert(fout1.is_open()); // If we ever disable exceptions, catch issues ASAP
400406

401407
fout1 << "# no manual edits - this file is autogenerated by dmake\n\n";
402408
fout1 << "include($$PWD/pcrerules.pri)\n";
@@ -418,8 +424,10 @@ int main(int argc, char **argv)
418424
}
419425

420426
static const char makefile[] = "Makefile";
421-
std::ofstream fout(makefile, std::ios_base::trunc);
427+
std::ofstream fout;
422428
fout.exceptions(std::ios_base::failbit | std::ios_base::badbit);
429+
fout.open(makefile, std::ios_base::trunc);
430+
assert(fout.is_open()); // If we ever disable exceptions, catch issues ASAP
423431

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

0 commit comments

Comments
 (0)