]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: fix --args handling when inferior argument have dash
authorAndrew Burgess <aburgess@redhat.com>
Tue, 16 Sep 2025 16:02:01 +0000 (17:02 +0100)
committerAndrew Burgess <aburgess@redhat.com>
Wed, 17 Sep 2025 08:55:57 +0000 (09:55 +0100)
After the commit:

  commit e5e76451fa82e0bc00599af96382b361c3d6ac32
  Date:   Fri Oct 22 07:19:29 2021 +0000

      gdb/gdbserver: add a '--no-escape-args' command line option

Inferior argument handling on the GDB command line was broken:

  $ gdb --args /bin/ls --foo
  ./gdb/gdb: unrecognized option '--foo'
  ./gdb/gdb: `--args' specified but no program specified

Before the above patch the definition of the '--args' argument in the
long_options array (in captured_main_1) was such that the
getopt_long_only call would directly set the 'set_args' variable to
true if '--args' was seen.

This meant that, immediately after the getopt_long_only call, we could
inspect set_args and break out of the argument processing loop if
needed.

After the above patch '--args' (and the new '--no-escape-args') no
longer set set_args directly via the getopt_long_only call.  Instead
the getopt_long_only call returns an OPT_* enum value, which we then
use in the following switch statement in order to set the set_args
variable.

What this means is that, immediately after the getopt_long_only call,
set_args no longer (immediately) indicates if --args was seen.  After
the switch statement, when set_args has been updated, we go around the
argument processing loop again and call getopt_long_only once more.

This extra getopt_long_only call will, if it finds another argument
that starts with a dash, update the global optind to point to this
option.  At this point things have gone wrong, GDB has now lost track
of the argument containing the program name the user wanted us to
start.  This leads to GDB exiting with the above error.

The solution is to move the check of set_args to either before the
getopt_long_only call, or to after the switch statement.  I chose to
move it earlier as this keeps all the loop exiting checks near the
beginning.

I've added more tests that cover this issue.

Approved-By: Luis Machado <luis.machado.foss@gmail.com>
Tested-By: Luis Machado <luis.machado.foss@gmail.com>
gdb/main.c
gdb/testsuite/gdb.base/args.exp

index 04c33638bec13a33526676697a1a7910824ebe32..f3049600a06a86a075e237ff026df8a622f6406c 100644 (file)
@@ -866,9 +866,14 @@ captured_main_1 (struct captured_main_args *context)
       {
        int option_index;
 
+       /* If the previous argument was --args or --no-escape-args, then
+          stop argument processing.  */
+       if (set_args != NO_ARGS)
+         break;
+
        c = getopt_long_only (argc, argv, "",
                              long_options, &option_index);
-       if (c == EOF || set_args != NO_ARGS)
+       if (c == EOF)
          break;
 
        /* Long option that takes an argument.  */
index 573543cc1f8623e76607ab73ee501d6a99516896..39300594773895f688174b3e1b52eff3ad59fabb 100644 (file)
@@ -186,6 +186,19 @@ proc run_all_tests {} {
        set ::env(TEST) "ABCD"
        args_test "shell variable" {{$TEST}} {\\$TEST} {{ABCD}}
     }
+
+    # At one point we had a bug where, if the last inferior argument,
+    # appearing after --args, looked like an option GDB might be able
+    # to process, e.g. started with a dash, then GDB would try to
+    # process it.  This would leave GDB in a broken state, and so GDB
+    # would fail to start.  A example of a failing GDB command line:
+    # $ gdb --args /bin/ls --all
+    foreach_with_prefix flag { args no-escape-args } {
+       args_test "with trailing GDB flag" [list "--${flag}"]
+    }
+    args_test "with trailing GDB option and value" [list "--ex" "start"]
+    args_test "with trailing double dash option" [list "--foo"]
+    args_test "with trailing single dash option" [list "-foo"]
 }
 
 run_all_tests