From ca754f851a45de7eb8a953bd45b0dfc0b1fa4a93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Thu, 29 Aug 2024 21:56:36 +0200 Subject: [PATCH] fix #13021: cmdline: validate option --std --- cli/cmdlineparser.cpp | 17 ++++++++++++++++- cli/cmdlineparser.h | 2 ++ test/testcmdlineparser.cpp | 4 ++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 924009ed018..6e01cd10116 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -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; } @@ -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"; +} diff --git a/cli/cmdlineparser.h b/cli/cmdlineparser.h index b1abdd1222b..f8023bfd6f9 100644 --- a/cli/cmdlineparser.h +++ b/cli/cmdlineparser.h @@ -156,6 +156,8 @@ class CmdLineParser { bool loadCppcheckCfg(); + static bool validateStandard(const std::string &std); + CmdLineLogger &mLogger; std::vector mPathNames; diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 46425aa161f..b7c46dd5a6f 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -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(CmdLineParser::Result::Fail), static_cast(CmdLineParser::Result::Success), static_cast(parser->parseFromArgs(3, argv))); - TODO_ASSERT_EQUALS("cppcheck: error: unknown --std value 'cplusplus11'\n", "", logger->str()); + ASSERT_EQUALS(static_cast(CmdLineParser::Result::Fail), static_cast(parser->parseFromArgs(3, argv))); + ASSERT_EQUALS("cppcheck: error: unknown --std value 'cplusplus11'\n", logger->str()); } void platformWin64() {