Skip to content

Commit

Permalink
bail out when -rule-file input has an invalid severity
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Mar 30, 2024
1 parent a8f1f9e commit 5319f3a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
5 changes: 5 additions & 0 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,11 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
return Result::Fail;
}

if (rule.severity == Severity::none) {
mLogger.printError("unable to load rule-file '" + ruleFile + "' - a rule has an invalid severity.");
return Result::Fail;
}

rule.regex = std::make_shared<Regex>(rule.pattern);
const std::string regex_err = rule.regex->compile();
if (!regex_err.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
}

for (const Settings::Rule &rule : mSettings.rules) {
if (rule.severity == Severity::none || rule.tokenlist != tokenlist)
if (rule.tokenlist != tokenlist)
continue;

if (!mSettings.quiet) {
Expand Down
1 change: 1 addition & 0 deletions lib/errortypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ std::string severityToString(Severity severity)
throw InternalError(nullptr, "Unknown severity");
}

// TODO: bail out on invalid severity
Severity severityFromString(const std::string& severity)
{
if (severity.empty())
Expand Down
30 changes: 30 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(ruleFileUnknownTokenList);
TEST_CASE(ruleFileInvalidPattern);
TEST_CASE(ruleFileMissingId);
TEST_CASE(ruleFileInvalidSeverity1);
TEST_CASE(ruleFileInvalidSeverity2);
#else
TEST_CASE(ruleFileNotSupported);
#endif
Expand Down Expand Up @@ -2369,6 +2371,34 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: unable to load rule-file 'rule.xml' - a rule is lacking an id.\n", logger->str());
}

void ruleFileInvalidSeverity1() {
REDIRECT;
ScopedFile file("rule.xml",
"<rule>\n"
"<pattern>.+</pattern>\n"
"<message>\n"
"<severity/>"
"</message>\n"
"</rule>\n");
const char * const argv[] = {"cppcheck", "--rule-file=rule.xml", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: unable to load rule-file 'rule.xml' - a rule has an invalid severity.\n", logger->str());
}

void ruleFileInvalidSeverity2() {
REDIRECT;
ScopedFile file("rule.xml",
"<rule>\n"
"<pattern>.+</pattern>\n"
"<message>\n"
"<severity>none</severity>"
"</message>\n"
"</rule>\n");
const char * const argv[] = {"cppcheck", "--rule-file=rule.xml", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: unable to load rule-file 'rule.xml' - a rule has an invalid severity.\n", logger->str());
}
#else
void ruleFileNotSupported() {
REDIRECT;
Expand Down

0 comments on commit 5319f3a

Please sign in to comment.