Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12344 (cmdline: better validation of premium options) #5875

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,26 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a

// Special Cppcheck Premium options
else if (std::strncmp(argv[i], "--premium=", 10) == 0 && isCppcheckPremium()) {
const std::set<std::string> valid{
"autosar",
"cert-c-2016",
"cert-c++-2016",
"misra-c-2012",
"misra-c-2023",
"misra-c++-2008",
"misra-c++-2023",
"bughunting",
"safety"};

if (std::strcmp(argv[i], "--premium=safety") == 0)
mSettings.safety = true;
if (!mSettings.premiumArgs.empty())
mSettings.premiumArgs += " ";
const std::string p(argv[i] + 10);
if (!valid.count(p) && !startsWith(p, "cert-c-int-precision=")) {
mLogger.printError("invalid --premium option '" + p + "'.");
return Result::Fail;
}
mSettings.premiumArgs += "--" + p;
if (p == "misra-c-2012" || p == "misra-c-2023")
mSettings.addons.emplace("misra");
Expand Down Expand Up @@ -1516,10 +1531,11 @@ void CmdLineParser::printHelp() const
" * cert-c++-2016 Cert C++ 2016 checking\n"
" * misra-c-2012 Misra C 2012\n"
" * misra-c-2023 Misra C 2023\n"
" * misra-c++-2008 Misra C++ 2008 (partial)\n"
" * misra-c++-2008 Misra C++ 2008\n"
" Other:\n"
" * bughunting Soundy analysis\n"
" * cert-c-int-precision=BITS Integer precision to use in Cert C analysis.\n";
" * cert-c-int-precision=BITS Integer precision to use in Cert C analysis.\n"
" * safety Safe mode\n";
}

oss <<
Expand Down
31 changes: 31 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(maxConfigsMissingCount);
TEST_CASE(maxConfigsInvalid);
TEST_CASE(maxConfigsTooSmall);
TEST_CASE(premiumOptions);
TEST_CASE(premiumSafety);
TEST_CASE(reportProgress1);
TEST_CASE(reportProgress2);
Expand Down Expand Up @@ -1187,6 +1188,36 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS("cppcheck: error: argument to '--max-configs=' must be greater than 0.\n", logger->str());
}

void premiumOptions() {
REDIRECT;
settings->cppcheckCfgProductName = "Cppcheck Premium 0.0.0";
{
const char * const argv[] = {"cppcheck", "--premium=misra-c-2012", "file.c"};
ASSERT_EQUALS(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
}
{
const char * const argv[] = {"cppcheck", "--premium=misra-c++-2023", "file.c"};
ASSERT_EQUALS(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
}
{
const char * const argv[] = {"cppcheck", "--premium=cert-c++-2016", "file.c"};
ASSERT_EQUALS(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
}
// invalid options
{
const char * const argv[] = {"cppcheck", "--premium=misra", "file.c"};
ASSERT_EQUALS(CmdLineParser::Result::Fail, parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: invalid --premium option 'misra'.\n", logger->str());
}
{
const char * const argv[] = {"cppcheck", "--premium=cert", "file.c"};
ASSERT_EQUALS(CmdLineParser::Result::Fail, parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: invalid --premium option 'cert'.\n", logger->str());
}
settings->cppcheckCfgProductName.clear();
settings->premiumArgs.clear();
}

void premiumSafety() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--premium=safety", "file.cpp"};
Expand Down
Loading