From 37ff119908955d0a2b6fd81859aaf25ece7d283a Mon Sep 17 00:00:00 2001 From: jmestwa-coder Date: Sun, 24 May 2026 20:00:03 +0530 Subject: [PATCH] Validate argv entries before std::string construction in ParseCLI --- args.hxx | 16 +++++++++++++++- test/parsecli_null_argv.cxx | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/parsecli_null_argv.cxx diff --git a/args.hxx b/args.hxx index 8eba1f2..7e0211f 100644 --- a/args.hxx +++ b/args.hxx @@ -3502,7 +3502,21 @@ namespace args std::vector args; if (argc > 1 && argv != nullptr) { - args.assign(argv + 1, argv + argc); + args.reserve(static_cast::size_type>(argc - 1)); + for (int i = 1; i < argc; ++i) + { + if (argv[i] == nullptr) + { +#ifdef ARGS_NOEXCEPT + error = Error::Parse; + errorMsg = "argv contains a null argument"; + return false; +#else + throw ParseError("argv contains a null argument"); +#endif + } + args.emplace_back(argv[i]); + } } return ParseArgs(args) == std::end(args); diff --git a/test/parsecli_null_argv.cxx b/test/parsecli_null_argv.cxx new file mode 100644 index 0000000..6bc9b32 --- /dev/null +++ b/test/parsecli_null_argv.cxx @@ -0,0 +1,16 @@ +#include "test_helpers.hxx" +#include "args.hxx" + +int main() +{ + args::ArgumentParser parser("parser"); + + const char *argv[] = {"prog", "--flag", nullptr}; + + test::require_throws_as([&] + { + parser.ParseCLI(3, argv); + }); + + return 0; +}