Skip to content

Commit 6017c25

Browse files
authored
Cppcheck: Remove dumpfile and filelist file using RAII class to avoid that these files are forgotten in early returns or exceptions. (#5369)
1 parent 3949965 commit 6017c25

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

lib/cppcheck.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ static TimerResults s_timerResults;
8888
// CWE ids used
8989
static const CWE CWE398(398U); // Indicator of Poor Code Quality
9090

91+
// File deleter
92+
namespace {
93+
class FilesDeleter {
94+
public:
95+
FilesDeleter() = default;
96+
~FilesDeleter() {
97+
for (const std::string& fileName: mFilenames)
98+
std::remove(fileName.c_str());
99+
}
100+
void addFile(const std::string& fileName) {
101+
mFilenames.push_back(fileName);
102+
}
103+
private:
104+
std::vector<std::string> mFilenames;
105+
};
106+
}
107+
91108
namespace {
92109
struct AddonInfo {
93110
std::string name;
@@ -631,6 +648,8 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
631648
{
632649
mExitCode = 0;
633650

651+
FilesDeleter filesDeleter;
652+
634653
if (Settings::terminated())
635654
return mExitCode;
636655

@@ -774,6 +793,8 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
774793
if (fdump.is_open()) {
775794
fdump << dumpProlog.str();
776795
dumpProlog.str("");
796+
if (!mSettings.dump)
797+
filesDeleter.addFile(dumpFile);
777798
}
778799

779800
// Get directives
@@ -1404,8 +1425,6 @@ void CppCheck::executeAddons(const std::string& dumpFile)
14041425
if (!dumpFile.empty()) {
14051426
std::vector<std::string> f{dumpFile};
14061427
executeAddons(f);
1407-
if (!mSettings.dump)
1408-
std::remove(dumpFile.c_str());
14091428
}
14101429
}
14111430

@@ -1414,10 +1433,13 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
14141433
if (mSettings.addons.empty() || files.empty())
14151434
return;
14161435

1436+
FilesDeleter filesDeleter;
1437+
14171438
std::string fileList;
14181439

14191440
if (files.size() >= 2 || endsWith(files[0], ".ctu-info")) {
14201441
fileList = Path::getPathFromFilename(files[0]) + FILELIST;
1442+
filesDeleter.addFile(fileList);
14211443
std::ofstream fout(fileList);
14221444
for (const std::string& f: files)
14231445
fout << f << std::endl;
@@ -1487,9 +1509,6 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
14871509
reportErr(errmsg);
14881510
}
14891511
}
1490-
1491-
if (!fileList.empty())
1492-
std::remove(fileList.c_str());
14931512
}
14941513

14951514
void CppCheck::executeAddonsWholeProgram(const std::map<std::string, std::size_t> &files)

0 commit comments

Comments
 (0)