Skip to content

Commit

Permalink
bail out when -rule-file input lacks an ID
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Mar 30, 2024
1 parent 472c0ad commit a8f1f9e
Show file tree
Hide file tree
Showing 3 changed files with 21 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 @@ -1133,6 +1133,11 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
return Result::Fail;
}

if (rule.id.empty()) {
mLogger.printError("unable to load rule-file '" + ruleFile + "' - a rule is lacking an id.");
return Result::Fail;
}

if (rule.tokenlist.empty()) {
mLogger.printError("unable to load rule-file '" + ruleFile + "' - a rule is lacking a tokenlist.");
return Result::Fail;
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.id.empty() || rule.severity == Severity::none || rule.tokenlist != tokenlist)
if (rule.severity == Severity::none || rule.tokenlist != tokenlist)
continue;

if (!mSettings.quiet) {
Expand Down
15 changes: 15 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(ruleFileMissingTokenList);
TEST_CASE(ruleFileUnknownTokenList);
TEST_CASE(ruleFileInvalidPattern);
TEST_CASE(ruleFileMissingId);
#else
TEST_CASE(ruleFileNotSupported);
#endif
Expand Down Expand Up @@ -2354,6 +2355,20 @@ 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' - pattern '.+\\' failed to compile (pcre_compile failed: \\ at end of pattern).\n", logger->str());
}

void ruleFileMissingId() {
REDIRECT;
ScopedFile file("rule.xml",
"<rule>\n"
"<pattern>.+</pattern>\n"
"<message>\n"
"<id/>"
"</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 is lacking an id.\n", logger->str());
}
#else
void ruleFileNotSupported() {
REDIRECT;
Expand Down

0 comments on commit a8f1f9e

Please sign in to comment.