From 7bc0fb809dc07a3989ceb165c0da6d3728232466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B6ppner?= Date: Wed, 14 May 2025 12:19:51 +0200 Subject: [PATCH] zpcictl: Fix command line parsing for invalid options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently zpcictl silently accepts incorrect command line options while it should be displaying an error message for invalid ones. There is a check for the case when no arguments are supplied, but invalid arguments or only specifying a device without an action does not display an error nor give a failure exit code. Fix this by changing parse_cmdline() to return a boolean indicating if any arguments were supplied while exiting with an error and message when invalid options are detected. Reviewed-by: Niklas Schnelle Signed-off-by: Jan Höppner --- zpcictl/zpcictl.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/zpcictl/zpcictl.c b/zpcictl/zpcictl.c index d693f737..45afac8d 100644 --- a/zpcictl/zpcictl.c +++ b/zpcictl/zpcictl.c @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -139,6 +140,13 @@ static void fwrite_err(FILE *fp, char *path) exit(EXIT_FAILURE); } +static void missing_option_err(void) +{ + warnx("No option specified"); + util_prg_print_parse_error(); + exit(EXIT_FAILURE); +} + #define READ_CHUNK_SIZE 512 static char *collect_smart_data(struct zpci_device *pdev) @@ -357,15 +365,20 @@ static void sclp_report_error(struct zpci_device *pdev) sclp_issue_action(pdev, SCLP_ERRNOTIFY_AQ_REPORT_ERR); } -static void parse_cmdline(int argc, char *argv[], struct options *opts) +static bool parse_cmdline(int argc, char *argv[], struct options *opts) { + bool opt_specified = false; int cmd; util_prg_init(&prg); util_opt_init(opt_vec, NULL); - do { + while (1) { cmd = util_opt_getopt_long(argc, argv); + if (cmd == -1) + break; + + opt_specified = true; switch (cmd) { case OPT_RESET: @@ -387,16 +400,13 @@ static void parse_cmdline(int argc, char *argv[], struct options *opts) case 'v': util_prg_print_version(); exit(EXIT_SUCCESS); - case -1: - /* End of options string */ - if (argc == 1) { - errx(EXIT_FAILURE, - "Use '%s --help' for more information", - argv[0]); - } - break; + default: + util_opt_print_parse_error(cmd, argv); + exit(EXIT_FAILURE); } - } while (cmd != -1); + } + + return opt_specified; } int main(int argc, char *argv[]) @@ -404,7 +414,8 @@ int main(int argc, char *argv[]) struct zpci_device pdev = { 0 }; struct options opts = { 0 }; - parse_cmdline(argc, argv, &opts); + if (!parse_cmdline(argc, argv, &opts)) + missing_option_err(); if (optind >= argc) errx(EXIT_FAILURE, "No device specified");