Blame SOURCES/0021-cli-Unpack-command-line-argument-parsing-logic.patch

4188e0
From 683364e35ef2da8cd646fe1e29e57a17c6e71c36 Mon Sep 17 00:00:00 2001
4188e0
From: Ernestas Kulik <ekulik@redhat.com>
4188e0
Date: Tue, 28 May 2019 15:51:05 +0200
4188e0
Subject: [PATCH] cli: Unpack command-line argument parsing logic
4188e0
4188e0
Currently, checking for invalid command line needlessly involves
4188e0
convoluted bitwise operations, which can be simplified drastically.
4188e0
4188e0
First, argc is reduced by optind, which points to the next argument to
4188e0
be processed. If everything goes well, argc should be 1, since the only
4188e0
remaining argument to be processed is the problem directory. If that
4188e0
does not hold, we want to bail. Another point at which we want to bail
4188e0
is when an option is passed that operates on the positional argument
4188e0
(anything but -L, which just lists available events). Checking for that
4188e0
involves ANDing the current option mask with the mask of all such
4188e0
options. The result is NOTed for comparison, since argc is 0 in such
4188e0
cases.
4188e0
4188e0
Signed-off-by: Ernestas Kulik <ekulik@redhat.com>
4188e0
---
4188e0
 src/cli/cli.c | 17 ++++++++---------
4188e0
 1 file changed, 8 insertions(+), 9 deletions(-)
4188e0
4188e0
diff --git a/src/cli/cli.c b/src/cli/cli.c
4188e0
index ff5b29f4..67ce7dde 100644
4188e0
--- a/src/cli/cli.c
4188e0
+++ b/src/cli/cli.c
4188e0
@@ -39,6 +39,9 @@ static char *steal_directory_if_needed(char *dump_dir_name)
4188e0
 
4188e0
 int main(int argc, char** argv)
4188e0
 {
4188e0
+    bool runaway_arguments;
4188e0
+    bool missing_positional_argument;
4188e0
+
4188e0
     abrt_init(argv);
4188e0
 
4188e0
     setlocale(LC_ALL, "");
4188e0
@@ -108,16 +111,12 @@ int main(int argc, char** argv)
4188e0
     argv += optind;
4188e0
     argc -= optind;
4188e0
 
4188e0
+    runaway_arguments = argc > 1;
4188e0
+    missing_positional_argument = (opts & OPTMASK_need_arg) && (argc == 0);
4188e0
+
4188e0
     /* Check for bad usage */
4188e0
-    if (argc > 1 /* more than one arg? */
4188e0
-        ||
4188e0
-        /* dont_need_arg == have_arg? bad in both cases:
4188e0
-         * TRUE == TRUE (dont need arg but have) or
4188e0
-         * FALSE == FALSE (need arg but havent).
4188e0
-         * OPT_list_events is an exception, it can be used in both cases.
4188e0
-         */
4188e0
-        (((!(opts & OPTMASK_need_arg)) == argc) && (op != OPT_list_events))
4188e0
-    ) {
4188e0
+    if (runaway_arguments || (missing_positional_argument && op != OPT_list_events))
4188e0
+    {
4188e0
         show_usage_and_die(program_usage_string, program_options);
4188e0
     }
4188e0
 
4188e0
-- 
4188e0
2.21.0
4188e0