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

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