Merge pull request #31312 from saanvimundra/fix/commandlineparser-key-validation-5x

core: report malformed CommandLineParser key descriptions
This commit is contained in:
Abhishek Gola
2026-12-02 20:44:04 +05:30
committed by GitHub
2 changed files with 11 additions and 0 deletions

View File

@@ -230,6 +230,9 @@ CommandLineParser::CommandLineParser(int argc, const char* const argv[], const S
for (size_t i = 0; i < k.size(); i++)
{
std::vector<String> l = impl->split_string(k[i], '|', true);
if (l.size() < 3)
CV_Error_(Error::StsParseError,
("invalid key description '{%s}': expected 'names | default | help'", k[i].c_str()));
CommandLineParserParams p;
p.keys = impl->split_string(l[0]);
p.def_value = l[1];

View File

@@ -36,6 +36,14 @@ TEST(CommandLineParser, testFailure)
EXPECT_FALSE(parser.check());
}
TEST(CommandLineParser, testMalformedKeyDescription)
{
const char* argv[] = {"<bin>"};
const int argc = 1;
EXPECT_THROW(cv::CommandLineParser(argc, argv, "{ a }"), cv::Exception);
EXPECT_THROW(cv::CommandLineParser(argc, argv, "{ a | 1 }"), cv::Exception);
}
TEST(CommandLineParser, testHas_noValues)
{
const char* argv[] = {"<bin>", "-h", "--info"};