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

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