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

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