Skip to content

Commit d11f18b

Browse files
committed
cmdlineparser: terminate with error message if --append=<filename> fails
Signed-off-by: Stefan Weil <[email protected]>
1 parent c317c89 commit d11f18b

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

cli/cmdlineparser.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,13 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
267267
_settings->_errorsOnly = true;
268268

269269
// Append userdefined code to checked source code
270-
else if (strncmp(argv[i], "--append=", 9) == 0)
271-
_settings->append(9 + argv[i]);
270+
else if (strncmp(argv[i], "--append=", 9) == 0) {
271+
const std::string filename = 9 + argv[i];
272+
if (!_settings->append(filename)) {
273+
PrintMessage("cppcheck: Couldn't open the file: \"" + filename + "\".");
274+
return false;
275+
}
276+
}
272277

273278
else if (strncmp(argv[i], "--enable=", 9) == 0) {
274279
const std::string errmsg = _settings->addEnabled(argv[i] + 9);

lib/settings.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,18 @@ bool Settings::isEnabled(const std::string &str) const
118118
}
119119

120120

121-
void Settings::append(const std::string &filename)
121+
bool Settings::append(const std::string &filename)
122122
{
123123
_append = "\n";
124124
std::ifstream fin(filename.c_str());
125+
if (!fin.is_open()) {
126+
return false;
127+
}
125128
std::string line;
126129
while (std::getline(fin, line)) {
127130
_append += line + "\n";
128131
}
132+
return true;
129133
}
130134

131135
std::string Settings::append() const

lib/settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class Settings {
116116
std::list<std::string> _includePaths;
117117

118118
/** @brief assign append code (--append) */
119-
void append(const std::string &filename);
119+
bool append(const std::string &filename);
120120

121121
/** @brief get append code (--append) */
122122
std::string append() const;

0 commit comments

Comments
 (0)