[2/4] GDB/MI: fix and simplify mi_valid_noargs utility function

Message ID 1444045617-14526-3-git-send-email-crosa@redhat.com
State New, archived
Headers

Commit Message

Cleber Rosa Oct. 5, 2015, 11:46 a.m. UTC
  This function was supposed to check if a command that takes no
arguments was called appropriately, that is, without any arguments.

Turns out that the implementation, relying on mi_getopt, would always
return -1, either because:

 * no argument was given, then the option index (*oind) would be equal
   to the argument count (argc)
 * the argument given did not start with a dash (arg[0] != '-')

If the command should take no arguments, then it should be OK to just
check the given argument count.

gdb/ChangeLog:
    2015-10-05  Cleber Rosa  <crosa@redhat.com>

    * mi/mi-getopt.c (mi_valid_noargs): Simplify/fix the function by
    looking at the argument count.
---
 gdb/mi/mi-getopt.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
  

Comments

Pedro Alves Oct. 21, 2015, 10:39 a.m. UTC | #1
On 10/05/2015 12:46 PM, Cleber Rosa wrote:

...

> If the command should take no arguments, then it should be OK to just
> check the given argument count.

...

> 
> diff --git a/gdb/mi/mi-getopt.c b/gdb/mi/mi-getopt.c
> index 0a07a39..574ccd2 100644
> --- a/gdb/mi/mi-getopt.c
> +++ b/gdb/mi/mi-getopt.c
> @@ -95,18 +95,14 @@ mi_getopt_allow_unknown (const char *prefix, int argc, char **argv,
>    return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 0);
>  }
>  
> +/* Tests if a command that takes no arguments was properly used.
> +   Returns 1 if no options were given, otherwise return 0.  */

This function is already documented in mi-getopt.h.  Any comment
here should just be "/* See mi-getopt.h.  */"

But the comment in mi-getopt.h says:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* mi_valid_noargs determines if ARGC/ARGV are a valid set of
   parameters to satisfy an MI function that is not supposed to
   recieve any arguments.

   An MI function that should not receive arguments can still be
   passed parameters after the special option '--' such as below.

   Example: The MI function -exec-run takes no args.
   However, the client may pass '-exec-run -- -a ...'
   See PR-783

   PREFIX is passed to mi_getopt for an error message.

   This function Returns 1 if the parameter pair ARGC/ARGV are valid
   for an MI function that takes no arguments. Otherwise, it returns 0
   and the appropriate error message is displayed by mi_getopt.  */

extern int mi_valid_noargs (const char *prefix, int argc, char **argv);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sounds like that conflicts with your patch?

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/mi/mi-getopt.c b/gdb/mi/mi-getopt.c
index 0a07a39..574ccd2 100644
--- a/gdb/mi/mi-getopt.c
+++ b/gdb/mi/mi-getopt.c
@@ -95,18 +95,14 @@  mi_getopt_allow_unknown (const char *prefix, int argc, char **argv,
   return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 0);
 }
 
+/* Tests if a command that takes no arguments was properly used.
+   Returns 1 if no options were given, otherwise return 0.  */
+
 int 
 mi_valid_noargs (const char *prefix, int argc, char **argv) 
 {
-  int oind = 0;
-  char *oarg;
-  static const struct mi_opt opts[] =
-    {
-      { 0, 0, 0 }
-    };
-
-  if (mi_getopt (prefix, argc, argv, opts, &oind, &oarg) == -1)
-    return 1;
-  else
+  if (argc > 0)
     return 0;
+
+  return 1;
 }