Skip to content

Commit

Permalink
fix #13021: cmdline: validate option --std
Browse files Browse the repository at this point in the history
  • Loading branch information
ludviggunne committed Aug 29, 2024
1 parent 5b5e43e commit ca754f8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
17 changes: 16 additions & 1 deletion cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
else if (std::strncmp(std.c_str(), "c", 1) == 0) {
mSettings.standards.c = Standards::getC(std);
}
else {
if (!validateStandard(std)) {
mLogger.printError("unknown --std value '" + std + "'");
return Result::Fail;
}
Expand Down Expand Up @@ -1943,3 +1943,18 @@ bool CmdLineParser::loadCppcheckCfg()
return true;
}

bool CmdLineParser::validateStandard(const std::string &std)
{
return std == "c++03"
|| std == "c++11"
|| std == "c++14"
|| std == "c++17"
|| std == "c++20"
|| std == "c++23"
|| std == "c++26"
|| std == "c89"
|| std == "c99"
|| std == "c11"
|| std == "c17"
|| std == "c23";
}
2 changes: 2 additions & 0 deletions cli/cmdlineparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class CmdLineParser {

bool loadCppcheckCfg();

static bool validateStandard(const std::string &std);

CmdLineLogger &mLogger;

std::vector<std::string> mPathNames;
Expand Down
4 changes: 2 additions & 2 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1369,8 +1369,8 @@ class TestCmdlineParser : public TestFixture {
void stdunknown2() {
REDIRECT;
const char *const argv[] = {"cppcheck", "--std=cplusplus11", "file.cpp"};
TODO_ASSERT_EQUALS(static_cast<int>(CmdLineParser::Result::Fail), static_cast<int>(CmdLineParser::Result::Success), static_cast<int>(parser->parseFromArgs(3, argv)));
TODO_ASSERT_EQUALS("cppcheck: error: unknown --std value 'cplusplus11'\n", "", logger->str());
ASSERT_EQUALS(static_cast<int>(CmdLineParser::Result::Fail), static_cast<int>(parser->parseFromArgs(3, argv)));
ASSERT_EQUALS("cppcheck: error: unknown --std value 'cplusplus11'\n", logger->str());
}

void platformWin64() {
Expand Down

0 comments on commit ca754f8

Please sign in to comment.