From 8ba3db8bbdbbee8517bd92e462d20a51c62dc649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano-Tarumi?= Date: Sat, 12 Jan 2019 16:09:18 -0500 Subject: [PATCH] t: safer argument casting for getopt --- t/cli.cc | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/t/cli.cc b/t/cli.cc index a0877c1..f51bd35 100644 --- a/t/cli.cc +++ b/t/cli.cc @@ -35,11 +35,27 @@ void check_read_comments() } } +/** + * Wrap #ot::parse_options with a higher-level interface much more convenient for testing. + * In practice, the argc/argv combo are enough though for the current state of opustags. + */ +static ot::status parse_options(const std::vector& args, ot::options& opt) +{ + int argc = args.size(); + char* argv[argc]; + for (size_t i = 0; i < argc; ++i) + argv[i] = strdup(args[i]); + ot::status rc = ot::parse_options(argc, argv, opt); + for (size_t i = 0; i < argc; ++i) + free(argv[i]); + return rc; +} + void check_good_arguments() { auto parse = [](std::vector args) { ot::options opt; - ot::status rc = ot::parse_options(args.size(), const_cast(args.data()), opt); + ot::status rc = parse_options(args, opt); if (rc.code != ot::st::ok) throw failure("unexpected option parsing error"); return opt; @@ -66,7 +82,7 @@ void check_bad_arguments() { auto error_case = [](std::vector args, const char* message, const std::string& name) { ot::options opt; - ot::status rc = ot::parse_options(args.size(), const_cast(args.data()), opt); + ot::status rc = parse_options(args, opt); if (rc.code != ot::st::bad_arguments) throw failure("bad error code for case " + name); if (rc.message != message)