Skip to content

Commit 5319f3a

Browse files
committed
bail out when -rule-file input has an invalid severity
1 parent a8f1f9e commit 5319f3a

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

cli/cmdlineparser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,11 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11481148
return Result::Fail;
11491149
}
11501150

1151+
if (rule.severity == Severity::none) {
1152+
mLogger.printError("unable to load rule-file '" + ruleFile + "' - a rule has an invalid severity.");
1153+
return Result::Fail;
1154+
}
1155+
11511156
rule.regex = std::make_shared<Regex>(rule.pattern);
11521157
const std::string regex_err = rule.regex->compile();
11531158
if (!regex_err.empty()) {

lib/cppcheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
11691169
}
11701170

11711171
for (const Settings::Rule &rule : mSettings.rules) {
1172-
if (rule.severity == Severity::none || rule.tokenlist != tokenlist)
1172+
if (rule.tokenlist != tokenlist)
11731173
continue;
11741174

11751175
if (!mSettings.quiet) {

lib/errortypes.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ std::string severityToString(Severity severity)
7272
throw InternalError(nullptr, "Unknown severity");
7373
}
7474

75+
// TODO: bail out on invalid severity
7576
Severity severityFromString(const std::string& severity)
7677
{
7778
if (severity.empty())

test/testcmdlineparser.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ class TestCmdlineParser : public TestFixture {
350350
TEST_CASE(ruleFileUnknownTokenList);
351351
TEST_CASE(ruleFileInvalidPattern);
352352
TEST_CASE(ruleFileMissingId);
353+
TEST_CASE(ruleFileInvalidSeverity1);
354+
TEST_CASE(ruleFileInvalidSeverity2);
353355
#else
354356
TEST_CASE(ruleFileNotSupported);
355357
#endif
@@ -2369,6 +2371,34 @@ class TestCmdlineParser : public TestFixture {
23692371
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parser->parseFromArgs(3, argv));
23702372
ASSERT_EQUALS("cppcheck: error: unable to load rule-file 'rule.xml' - a rule is lacking an id.\n", logger->str());
23712373
}
2374+
2375+
void ruleFileInvalidSeverity1() {
2376+
REDIRECT;
2377+
ScopedFile file("rule.xml",
2378+
"<rule>\n"
2379+
"<pattern>.+</pattern>\n"
2380+
"<message>\n"
2381+
"<severity/>"
2382+
"</message>\n"
2383+
"</rule>\n");
2384+
const char * const argv[] = {"cppcheck", "--rule-file=rule.xml", "file.cpp"};
2385+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parser->parseFromArgs(3, argv));
2386+
ASSERT_EQUALS("cppcheck: error: unable to load rule-file 'rule.xml' - a rule has an invalid severity.\n", logger->str());
2387+
}
2388+
2389+
void ruleFileInvalidSeverity2() {
2390+
REDIRECT;
2391+
ScopedFile file("rule.xml",
2392+
"<rule>\n"
2393+
"<pattern>.+</pattern>\n"
2394+
"<message>\n"
2395+
"<severity>none</severity>"
2396+
"</message>\n"
2397+
"</rule>\n");
2398+
const char * const argv[] = {"cppcheck", "--rule-file=rule.xml", "file.cpp"};
2399+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parser->parseFromArgs(3, argv));
2400+
ASSERT_EQUALS("cppcheck: error: unable to load rule-file 'rule.xml' - a rule has an invalid severity.\n", logger->str());
2401+
}
23722402
#else
23732403
void ruleFileNotSupported() {
23742404
REDIRECT;

0 commit comments

Comments
 (0)