Skip to content
Closed
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
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ if(ARGS_ENABLE_SECURITY_HARDENING)
args_add_compile_flag_if_supported(-Wcast-align)
args_add_compile_flag_if_supported(-Wunused)

# -Wunsafe-buffer-usage (clang) flags all raw pointer arithmetic,
# including the standard `argv + 1, argv + argc` idiom. It produces
# false positives on bounded argv handling, so it is not enabled.
# -Wunsafe-buffer-usage (clang) flags all raw pointer arithmetic.
# The library now avoids raw argv range arithmetic in security-sensitive
# code paths, so this warning is useful for catching unsafe buffer usage.
args_add_compile_flag_if_supported(-Wunsafe-buffer-usage)

if(ARGS_ENABLE_HARDENING_WERROR)
args_add_compile_flag_if_supported(-Werror)
Expand Down
9 changes: 8 additions & 1 deletion args.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -3504,7 +3504,14 @@ namespace args
std::vector<std::string> args;
if (argc > 1 && argv != nullptr)
{
args.assign(argv + 1, argv + argc);
args.reserve(static_cast<size_t>(argc - 1));
for (int idx = 1; idx < argc; ++idx)
{
if (argv[idx] != nullptr)
{
args.emplace_back(argv[idx]);
}
}
}

return ParseArgs(args) == std::end(args);
Expand Down
13 changes: 12 additions & 1 deletion examples/gitlike.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ int main(int argc, char **argv)
{"init", Init},
{"add", Add}};

const std::vector<std::string> args(argv + 1, argv + argc);
std::vector<std::string> args;
if (argc > 1)
{
args.reserve(static_cast<size_t>(argc - 1));
for (int idx = 1; idx < argc; ++idx)
{
if (argv[idx] != nullptr)
{
args.emplace_back(argv[idx]);
}
}
}
args::ArgumentParser parser("This is a git-like program", "Valid commands are init and add");
args::HelpFlag help(parser, "help", "Display this help menu", {'h', "help"});
parser.Prog(argv[0]);
Expand Down
26 changes: 26 additions & 0 deletions test/parse_cli_args.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* Copyright (c) Taylor Richberger <taylor@axfive.net>
* This code is released under the license described in the LICENSE file
*/

#include "test_common.hxx"

#include <args.hxx>

#include "test_helpers.hxx"

int main()
{
args::ArgumentParser parser("This is a test program.");
args::ValueFlag<std::string> foo(parser, "FOO", "test flag", {'f', "foo"});
args::Flag bar(parser, "BAR", "test flag", {'b', "bar"});

const char *argv[] = {"prog", "-f", "test", "--bar"};
const bool result = parser.ParseCLI(static_cast<int>(sizeof(argv) / sizeof(argv[0])), argv);

test::require(result);
test::require(foo);
test::require(*foo == "test");
test::require(bar);

return 0;
}